Skip to content

Commit d277e9e

Browse files
committed
pythonGH-127970: find the loaded libpython when dladdr is available
Signed-off-by: Filipe Laíns <[email protected]
1 parent 7b8bd3b commit d277e9e

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

Modules/getpath.c

Lines changed: 16 additions & 22 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,27 @@ progname_to_dict(PyObject *dict, const char *key)
803806
static int
804807
library_to_dict(PyObject *dict, const char *key)
805808
{
806-
#ifdef MS_WINDOWS
807809
#ifdef Py_ENABLE_SHARED
810+
static char path[MAXPATHLEN + 1] = {0};
811+
812+
#ifdef MS_WINDOWS
808813
extern HMODULE PyWin_DLLhModule;
809814
if (PyWin_DLLhModule) {
810815
return winmodule_to_dict(dict, key, PyWin_DLLhModule);
811816
}
812817
#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-
}
818+
819+
#if HAVE_DLADDR
820+
Dl_info libpython_info;
821+
if (dladdr(&Py_Initialize, &libpython_info) && libpython_info.dli_fname) {
822+
strncpy(path, pythonInfo.dli_fname, MAXPATHLEN);
823+
if (path[sizeof(path)-1] == '\0') {
824+
return decode_to_dict(dict, key, path);
830825
}
831826
}
832-
if (modPathInitialized > 0) {
833-
return decode_to_dict(dict, key, modPath);
834-
}
835827
#endif
828+
#endif
829+
836830
return PyDict_SetItemString(dict, key, Py_None) == 0;
837831
}
838832

configure.ac

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

0 commit comments

Comments
 (0)