diff --git a/Modules/getpath.c b/Modules/getpath.c --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -8,6 +8,7 @@ #ifdef __APPLE__ #include +#include #endif /* Search in some common locations for the associated Python libraries. @@ -473,8 +474,7 @@ size_t prefixsz; wchar_t *defpath; #ifdef WITH_NEXT_FRAMEWORK - NSModule pythonModule; - const char* modPath; + Dl_info pythonInfo; #endif #ifdef __APPLE__ #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 @@ -567,38 +567,35 @@ ** 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... */ - pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); - /* Use dylib functions to find out where the framework was loaded from */ - modPath = NSLibraryNameForModule(pythonModule); - if (modPath != NULL) { - /* We're in a framework. */ - /* See if we might be in the build directory. The framework in the - ** build directory is incomplete, it only has the .dylib and a few - ** needed symlinks, it doesn't have the Lib directories and such. - ** If we're running with the framework from the build directory we must - ** be running the interpreter in the build directory, so we use the - ** build-directory-specific logic to find Lib and such. - */ - wchar_t* wbuf = _Py_char2wchar(modPath, NULL); - if (wbuf == NULL) { - Py_FatalError("Cannot decode framework location"); - } - + if (!dladdr(&Py_Initialize, &pythonInfo)) { + Py_FatalError("Cannot locate framework"); + } + /* See if we might be in the build directory. The framework in the + ** build directory is incomplete, it only has the .dylib and a few + ** needed symlinks, it doesn't have the Lib directories and such. + ** If we're running with the framework from the build directory we must + ** be running the interpreter in the build directory, so we use the + ** build-directory-specific logic to find Lib and such. + */ + wchar_t* wbuf = _Py_char2wchar(pythonInfo.dli_fname, NULL); + if (wbuf == NULL) { + Py_FatalError("Cannot decode framework location"); + } + + wcsncpy(argv0_path, wbuf, MAXPATHLEN); + reduce(argv0_path); + joinpath(argv0_path, lib_python); + joinpath(argv0_path, LANDMARK); + if (!ismodule(argv0_path)) { + /* We are in the build directory so use the name of the + executable - we know that the absolute path is passed */ + wcsncpy(argv0_path, progpath, MAXPATHLEN); + } + else { + /* Use the location of the library as the progpath */ wcsncpy(argv0_path, wbuf, MAXPATHLEN); - reduce(argv0_path); - joinpath(argv0_path, lib_python); - joinpath(argv0_path, LANDMARK); - if (!ismodule(argv0_path)) { - /* We are in the build directory so use the name of the - executable - we know that the absolute path is passed */ - wcsncpy(argv0_path, progpath, MAXPATHLEN); - } - else { - /* Use the location of the library as the progpath */ - wcsncpy(argv0_path, wbuf, MAXPATHLEN); - } - PyMem_Free(wbuf); } + PyMem_Free(wbuf); #endif #if HAVE_READLINK