Skip to content

Commit de2715f

Browse files
aiskned-deily
andauthored
gh-59703: use the system dladdr function in getpath.c for macOS framework builds (GH-111546)
Co-authored-by: Ned Deily <[email protected]>
1 parent 6a00a58 commit de2715f

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
For macOS framework builds, in ``getpath.c`` use the system ``dladdr``
2+
function to find the path to the shared library rather than depending
3+
on deprecated macOS APIs.
4+

Modules/getpath.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#endif
1818

1919
#ifdef __APPLE__
20-
# include <mach-o/dyld.h>
20+
# include <dlfcn.h>
2121
#endif
2222

2323
/* Reference the precompiled getpath.py */
@@ -768,16 +768,11 @@ library_to_dict(PyObject *dict, const char *key)
768768
which is in the framework, not relative to the executable, which may
769769
be outside of the framework. Except when we're in the build
770770
directory... */
771-
NSSymbol symbol = NSLookupAndBindSymbol("_Py_Initialize");
772-
if (symbol != NULL) {
773-
NSModule pythonModule = NSModuleForSymbol(symbol);
774-
if (pythonModule != NULL) {
775-
/* Use dylib functions to find out where the framework was loaded from */
776-
const char *path = NSLibraryNameForModule(pythonModule);
777-
if (path) {
778-
strncpy(modPath, path, MAXPATHLEN);
779-
modPathInitialized = 1;
780-
}
771+
Dl_info pythonInfo;
772+
if (dladdr(&Py_Initialize, &pythonInfo)) {
773+
if (pythonInfo.dli_fname) {
774+
strncpy(modPath, pythonInfo.dli_fname, MAXPATHLEN);
775+
modPathInitialized = 1;
781776
}
782777
}
783778
}

0 commit comments

Comments
 (0)