Skip to content

Commit

Permalink
pythongh-59703: use the system dladdr function in getpath.c for macOS…
Browse files Browse the repository at this point in the history
… framework builds (pythonGH-111546)

Co-authored-by: Ned Deily <[email protected]>
  • Loading branch information
2 people authored and Glyphack committed Jan 27, 2024
1 parent c64d740 commit bfdbf72
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
For macOS framework builds, in ``getpath.c`` use the system ``dladdr``
function to find the path to the shared library rather than depending
on deprecated macOS APIs.

17 changes: 6 additions & 11 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#endif

#ifdef __APPLE__
# include <mach-o/dyld.h>
# include <dlfcn.h>
#endif

/* Reference the precompiled getpath.py */
Expand Down Expand Up @@ -768,16 +768,11 @@ library_to_dict(PyObject *dict, const char *key)
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... */
NSSymbol symbol = NSLookupAndBindSymbol("_Py_Initialize");
if (symbol != NULL) {
NSModule pythonModule = NSModuleForSymbol(symbol);
if (pythonModule != NULL) {
/* Use dylib functions to find out where the framework was loaded from */
const char *path = NSLibraryNameForModule(pythonModule);
if (path) {
strncpy(modPath, path, MAXPATHLEN);
modPathInitialized = 1;
}
Dl_info pythonInfo;
if (dladdr(&Py_Initialize, &pythonInfo)) {
if (pythonInfo.dli_fname) {
strncpy(modPath, pythonInfo.dli_fname, MAXPATHLEN);
modPathInitialized = 1;
}
}
}
Expand Down

0 comments on commit bfdbf72

Please sign in to comment.