Skip to content

Commit 84e68b1

Browse files
committed
bpo-45582: Fix framework path and bootstrap build
* Check NS API return values for NULL to prevent segfault in ``_bootstrap_python``. * Set modPathInitialized to 1 so the ``decode_to_dict`` path is used. Signed-off-by: Christian Heimes <[email protected]>
1 parent a310fd8 commit 84e68b1

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

Makefile.pre.in

+2-14
Original file line numberDiff line numberDiff line change
@@ -954,9 +954,9 @@ BOOTSTRAP_HEADERS = \
954954

955955
Programs/_bootstrap_python.o: Programs/_bootstrap_python.c $(BOOTSTRAP_HEADERS) $(PYTHON_HEADERS)
956956

957-
_bootstrap_python: $(LIBRARY_OBJS_OMIT_FROZEN) Programs/_bootstrap_python.o Modules/getpath_bootstrap.o Modules/Setup.local
957+
_bootstrap_python: $(LIBRARY_OBJS_OMIT_FROZEN) Programs/_bootstrap_python.o Modules/getpath.o Modules/Setup.local
958958
$(LINKCC) $(PY_LDFLAGS_NOLTO) -o $@ $(LIBRARY_OBJS_OMIT_FROZEN) \
959-
Programs/_bootstrap_python.o Modules/getpath_bootstrap.o $(LIBS) $(MODLIBS) $(SYSLIBS)
959+
Programs/_bootstrap_python.o Modules/getpath.o $(LIBS) $(MODLIBS) $(SYSLIBS)
960960

961961
############################################################################
962962
# Deepfreeze targets
@@ -1205,18 +1205,6 @@ Modules/getpath.o: $(srcdir)/Modules/getpath.c Python/frozen_modules/getpath.h M
12051205
-DPLATLIBDIR='"$(PLATLIBDIR)"' \
12061206
-o $@ $(srcdir)/Modules/getpath.c
12071207

1208-
# like getpath.o with additional -DPY_BOOTSTRAP_PYTHON=1
1209-
Modules/getpath_bootstrap.o: $(srcdir)/Modules/getpath.c Python/frozen_modules/getpath.h Makefile $(PYTHON_HEADERS)
1210-
$(CC) -c $(PY_CORE_CFLAGS) -DPYTHONPATH='"$(PYTHONPATH)"' \
1211-
-DPREFIX='"$(prefix)"' \
1212-
-DEXEC_PREFIX='"$(exec_prefix)"' \
1213-
-DVERSION='"$(VERSION)"' \
1214-
-DVPATH='"$(VPATH)"' \
1215-
-DPLATLIBDIR='"$(PLATLIBDIR)"' \
1216-
-DPY_BOOTSTRAP_PYTHON=1 \
1217-
-o $@ $(srcdir)/Modules/getpath.c
1218-
1219-
12201208
Programs/python.o: $(srcdir)/Programs/python.c
12211209
$(MAINCC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Programs/python.c
12221210

Modules/getpath.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -754,25 +754,28 @@ library_to_dict(PyObject *dict, const char *key)
754754
if (PyWin_DLLhModule) {
755755
return winmodule_to_dict(dict, key, PyWin_DLLhModule);
756756
}
757-
#elif defined(WITH_NEXT_FRAMEWORK) && !defined(PY_BOOTSTRAP_PYTHON)
758-
// _bootstrap_python does not use framework and crashes
757+
#elif defined(WITH_NEXT_FRAMEWORK)
759758
static char modPath[MAXPATHLEN + 1];
760759
static int modPathInitialized = -1;
761760
if (modPathInitialized < 0) {
762-
NSModule pythonModule;
763761
modPathInitialized = 0;
764762

765763
/* On Mac OS X we have a special case if we're running from a framework.
766764
This is because the python home should be set relative to the library,
767765
which is in the framework, not relative to the executable, which may
768766
be outside of the framework. Except when we're in the build
769767
directory... */
770-
pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
771-
772-
/* Use dylib functions to find out where the framework was loaded from */
773-
const char *path = NSLibraryNameForModule(pythonModule);
774-
if (path) {
775-
strncpy(modPath, path, MAXPATHLEN);
768+
NSSymbol symbol = NSLookupAndBindSymbol("_Py_Initialize");
769+
if (symbol != NULL) {
770+
NSModule pythonModule = NSModuleForSymbol(symbol);
771+
if (pythonModule != NULL) {
772+
/* Use dylib functions to find out where the framework was loaded from */
773+
const char *path = NSLibraryNameForModule(pythonModule);
774+
if (path) {
775+
strncpy(modPath, path, MAXPATHLEN);
776+
modPathInitialized = 1;
777+
}
778+
}
776779
}
777780
}
778781
if (modPathInitialized > 0) {

0 commit comments

Comments
 (0)