8000 GH-127970: find the runtime library when dladdr is available (#127972) · python/cpython@95cd9c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95cd9c6

Browse files
authored
GH-127970: find the runtime library when dladdr is available (#127972)
1 parent eb26e17 commit 95cd9c6

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
We now use the location of the ``libpython`` runtime library used in the current
2+
proccess to determine :data:`sys.base_prefix` on all platforms implementing the
3+
`dladdr <https://pubs.opengroup.org/onlinepubs/9799919799/functions/dladdr.html>`_
4+
function defined by the UNIX standard — this includes Linux, Android, macOS,
5+
iOS, FreeBSD, etc. This was already the case on Windows and macOS Framework
6+
builds.

Modules/getpath.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
#endif
1818

1919
#ifdef __APPLE__
20-
# include <dlfcn.h>
2120
# include <mach-o/dyld.h>
2221
#endif
2322

23+
#ifdef HAVE_DLFCN_H
24+
# include <dlfcn.h>
25+
#endif
26+
2427
/* Reference the precompiled getpath.py */
2528
#include "Python/frozen_modules/getpath.h"
2629

@@ -803,36 +806,25 @@ progname_to_dict(PyObject *dict, const char *key)
803806
static int
804807
library_to_dict(PyObject *dict, const char *key)
805808
{
809+
/* macOS framework builds do not link against a libpython dynamic library, but
810+
instead link against a macOS Framework. */
811+
#if defined(Py_ENABLE_SHARED) || defined(WITH_NEXT_FRAMEWORK)
812+
806813
#ifdef MS_WINDOWS
807-
#ifdef Py_ENABLE_SHARED
808814
extern HMODULE PyWin_DLLhModule;
809815
if (PyWin_DLLhModule) {
810816
return winmodule_to_dict(dict, key, PyWin_DLLhModule);
811817
}
812818
#endif
813-
#elif defined(WITH_NEXT_FRAMEWORK)
814-
static char modPath[MAXPATHLEN + 1];
815-
static int modPathInitialized = -1;
816-
if (modPathInitialized < 0) {
817-
modPathInitialized = 0;
818-
819-
/* On Mac OS X we have a special case if we're running from a framework.
820-
This is because the python home should be set relative to the library,
821-
which is in the framework, not relative to the executable, which may
822-
be outside of the framework. Except when we're in the build
823-
directory... */
824-
Dl_info pythonInfo;
825-
if (dladdr(&Py_Initialize, &pythonInfo)) {
826-
if (pythonInfo.dli_fname) {
827-
strncpy(modPath, pythonInfo.dli_fname, MAXPATHLEN);
828-
modPathInitialized = 1;
829-
}
830-
}
831-
}
832-
if (modPathInitialized > 0) {
833-
return decode_to_dict(dict, key, modPath);
819+
820+
#if HAVE_DLADDR
821+
Dl_info libpython_info;
822+
if (dladdr(&Py_Initialize, &libpython_info) && libpython_info.dli_fname) {
823+
return decode_to_dict(dict, key, libpython_info.dli_fname);
834824
}
835825
#endif
826+
#endif
827+
836828
return PyDict_SetItemString(dict, key, Py_None) == 0;
837829
}
838830

configure

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5128,7 +5128,7 @@ fi
51285128
# checks for library functions
51295129
AC_CHECK_FUNCS([ \
51305130
accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \
5131-
copy_file_range ctermid dup dup3 execv explicit_bzero explicit_memset \
5131+
copy_file_range ctermid dladdr dup dup3 execv explicit_bzero explicit_memset \
51325132
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
51335133
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
51345134
gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@
286286
/* Define if you have the 'dirfd' function or macro. */
287287
#undef HAVE_DIRFD
288288

289+
/* Define to 1 if you have the 'dladdr' function. */
290+
#undef HAVE_DLADDR
291+
289292
/* Define to 1 if you have the <dlfcn.h> header file. */
290293
#undef HAVE_DLFCN_H
291294

0 commit comments

Comments
 (0)
0