Subject: [bug#75688] [PATCH v3 2/4] gnu: python: Set GUIX_INTERPRETER_PATH and GUIX_MAIN_SCRIPT_PATH.

From: [email-masked]
Date: Mon, 27 Jan 2025 21:21:21 +0800

From: 宋文武 <[email-masked]>

This is used by 'g_build_guix_search_path_dirs' in our patched GLIB.

* gnu/packages/patches/python-3-set-GUIX_INTERPRETER_PATH.patch: New patch.
* gnu/local.mk (dist_patch_DATA): Register it.
* gnu/packages/python.scm (python-3.10)[source]<patches>: Add it.

Change-Id: I4588cbd087a783da1ad8c94fccda7ebf5e9f39ad
---
 gnu/local.mk                                  |  1 +
 .../python-3-set-GUIX_INTERPRETER_PATH.patch  | 28 +++++++++++++++++++
 gnu/packages/python.scm                       |  3 +-
 3 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 gnu/packages/patches/python-3-set-GUIX_INTERPRETER_PATH.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 7ae66dd57d..ba355dabf8 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -2085,6 +2085,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/python-3-arm-alignment.patch		\
   %D%/packages/patches/python-3-deterministic-build-info.patch	\
   %D%/packages/patches/python-3-search-paths.patch		\
+  %D%/packages/patches/python-3-set-GUIX_INTERPRETER_PATH.patch	\
   %D%/packages/patches/python-3-fix-tests.patch			\
   %D%/packages/patches/python-3-hurd-configure.patch		\
   %D%/packages/patches/python-angr-check-exec-deps.patch	\
diff --git a/gnu/packages/patches/python-3-set-GUIX_INTERPRETER_PATH.patch b/gnu/packages/patches/python-3-set-GUIX_INTERPRETER_PATH.patch
new file mode 100644
index 0000000000..2f173c68c8
--- /dev/null
+++ b/gnu/packages/patches/python-3-set-GUIX_INTERPRETER_PATH.patch
@@ -0,0 +1,28 @@
+The 'g_build_guix_search_path_dirs' function in our patched GLIB requires
+2 environment variables (GUIX_INTERPRETER_PATH and GUIX_MAIN_SCRIPT_PATH) to
+check if the current executable is a script launched by an interpreter, and
+find the script path if it is.
+---
+diff --git a/Modules/main.c b/Modules/main.c
+index 5bb1de2..83ada3d 100644
+--- a/Modules/main.c
++++ b/Modules/main.c
+@@ -636,6 +636,18 @@ pymain_run_python(int *exitcode)
+            prepended to sys.path.
+
+            Otherwise, main_importer_path is left unchanged. */
++
++        /* Set environment variables to support 'search-paths.d'. */
++        char *exe_path = realpath("/proc/self/exe", NULL);
++        PyObject *filename = PyUnicode_FromWideChar(config->run_filename, -1);
++        const char *main_script_path = PyUnicode_AsUTF8(filename);
++        if (exe_path != NULL && main_script_path != NULL) {
++            setenv("GUIX_INTERPRETER_PATH", exe_path, 1);
++            setenv("GUIX_MAIN_SCRIPT_PATH", main_script_path, 1);
++        }
++        free(exe_path);
++        Py_DECREF(filename);
++
+         if (pymain_get_importer(config->run_filename, &main_importer_path,
+                                 exitcode)) {
+             return;
diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index c5f98c3a46..7701d111c4 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -463,7 +463,8 @@ (define-public python-3.10
                         "python-3-fix-tests.patch"
                         "python-3-hurd-configure.patch"
                         "python-3-reproducible-build.patch"
-                        "python-3-search-paths.patch"))
+                        "python-3-search-paths.patch"
+                        "python-3-set-GUIX_INTERPRETER_PATH.patch"))
               (sha256
                (base32
                 "0j6wvh2ad5jjq5n7sjmj1k66mh6lipabavchc3rb4vsinwaq9vbf"))
-- 
2.47.1