]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-127970: find the runtime library when dladdr is available (#127972)
authorFilipe Laíns 🇵🇸 <lains@riseup.net>
Wed, 8 Jan 2025 12:03:21 +0000 (12:03 +0000)
committerGitHub <noreply@github.com>
Wed, 8 Jan 2025 12:03:21 +0000 (12:03 +0000)
Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-19-51-54.gh-issue-127970.vdUp-y.rst [new file with mode: 0644]
Modules/getpath.c
configure
configure.ac
pyconfig.h.in

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-19-51-54.gh-issue-127970.vdUp-y.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-19-51-54.gh-issue-127970.vdUp-y.rst
new file mode 100644 (file)
index 0000000..e4dc7b5
--- /dev/null
@@ -0,0 +1,6 @@
+We now use the location of the ``libpython`` runtime library used in the current
+proccess to determine :data:`sys.base_prefix` on all platforms implementing the
+`dladdr <https://pubs.opengroup.org/onlinepubs/9799919799/functions/dladdr.html>`_
+function defined by the UNIX standard — this includes Linux, Android, macOS,
+iOS, FreeBSD, etc. This was already the case on Windows and macOS Framework
+builds.
index 18ddfaf8dbce1a47a8148192729e19b8580046c5..2d3c9757298d16704862d8e4242104f0c22ff471 100644 (file)
 #endif
 
 #ifdef __APPLE__
-#  include <dlfcn.h>
 #  include <mach-o/dyld.h>
 #endif
 
+#ifdef HAVE_DLFCN_H
+#  include <dlfcn.h>
+#endif
+
 /* Reference the precompiled getpath.py */
 #include "Python/frozen_modules/getpath.h"
 
@@ -803,36 +806,25 @@ progname_to_dict(PyObject *dict, const char *key)
 static int
 library_to_dict(PyObject *dict, const char *key)
 {
+/* macOS framework builds do not link against a libpython dynamic library, but
+   instead link against a macOS Framework. */
+#if defined(Py_ENABLE_SHARED) || defined(WITH_NEXT_FRAMEWORK)
+
 #ifdef MS_WINDOWS
-#ifdef Py_ENABLE_SHARED
     extern HMODULE PyWin_DLLhModule;
     if (PyWin_DLLhModule) {
         return winmodule_to_dict(dict, key, PyWin_DLLhModule);
     }
 #endif
-#elif defined(WITH_NEXT_FRAMEWORK)
-    static char modPath[MAXPATHLEN + 1];
-    static int modPathInitialized = -1;
-    if (modPathInitialized < 0) {
-        modPathInitialized = 0;
-
-        /* On Mac OS X we have a special case if we're running from a framework.
-           This is because the python home should be set relative to the library,
-           which is in the framework, not relative to the executable, which may
-           be outside of the framework. Except when we're in the build
-           directory... */
-        Dl_info pythonInfo;
-        if (dladdr(&Py_Initialize, &pythonInfo)) {
-            if (pythonInfo.dli_fname) {
-                strncpy(modPath, pythonInfo.dli_fname, MAXPATHLEN);
-                modPathInitialized = 1;
-            }
-        }
-    }
-    if (modPathInitialized > 0) {
-        return decode_to_dict(dict, key, modPath);
+
+#if HAVE_DLADDR
+    Dl_info libpython_info;
+    if (dladdr(&Py_Initialize, &libpython_info) && libpython_info.dli_fname) {
+        return decode_to_dict(dict, key, libpython_info.dli_fname);
     }
 #endif
+#endif
+
     return PyDict_SetItemString(dict, key, Py_None) == 0;
 }
 
index 6e1b393a3ece6805953f139191e0db476842a6ba..bb77c558abda5a7ad235906a2465785c5f235d74 100755 (executable)
--- a/configure
+++ b/configure
@@ -19001,6 +19001,12 @@ if test "x$ac_cv_func_ctermid" = xyes
 then :
   printf "%s\n" "#define HAVE_CTERMID 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "dladdr" "ac_cv_func_dladdr"
+if test "x$ac_cv_func_dladdr" = xyes
+then :
+  printf "%s\n" "#define HAVE_DLADDR 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "dup" "ac_cv_func_dup"
 if test "x$ac_cv_func_dup" = xyes
index 6d44be8959865de45b939fcc4c5c51e779849ad3..653cd3f6c531b69f22f099bdbb66615a03907ae2 100644 (file)
@@ -5128,7 +5128,7 @@ fi
 # checks for library functions
 AC_CHECK_FUNCS([ \
   accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \
-  copy_file_range ctermid dup dup3 execv explicit_bzero explicit_memset \
+  copy_file_range ctermid dladdr dup dup3 execv explicit_bzero explicit_memset \
   faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
   fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
   gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \
index 874b98dc96585a170e2cac8cfff1f754ea5eb25e..aaf52168c3d39d9946d8bff0c935f0f294b6800e 100644 (file)
 /* Define if you have the 'dirfd' function or macro. */
 #undef HAVE_DIRFD
 
+/* Define to 1 if you have the 'dladdr' function. */
+#undef HAVE_DLADDR
+
 /* Define to 1 if you have the <dlfcn.h> header file. */
 #undef HAVE_DLFCN_H