New submission from Stephen Kelly: When attempting to use PyImport_ImportModule("os") (or to import many other libraries), there is a crash on Py_Finalize if Py_NoSiteFlag is set. The issue appears to be the use of frozenset() as a result of importing the module.
I reproduced this on Windows after building 2.7.13 with VS 2015 by applying the following patch: Python changes. --- Include\\fileobject.h +++ Include\\fileobject.h @@ -70,7 +70,7 @@ */ int _PyFile_SanitizeMode(char *mode); -#if defined _MSC_VER && _MSC_VER >= 1400 +#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 /* A routine to check if a file descriptor is valid on Windows. Returns 0 * and sets errno to EBADF if it isn't. This is to avoid Assertions * from various functions in the Windows CRT beginning with --- Modules\\posixmodule.c +++ Modules\\posixmodule.c @@ -529,7 +529,7 @@ #endif -#if defined _MSC_VER && _MSC_VER >= 1400 +#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900 /* Microsoft CRT in VS2005 and higher will verify that a filehandle is * valid and raise an assertion if it isn't. * Normally, an invalid fd is likely to be a C program error and therefore --- Modules\\timemodule.c +++ Modules\\timemodule.c @@ -68,6 +70,9 @@ #if defined(MS_WINDOWS) && !defined(__BORLANDC__) /* Win32 has better clock replacement; we have our own version below. */ #undef HAVE_CLOCK +#define timezone _timezone +#define tzname _tzname +#define daylight _daylight #endif /* MS_WINDOWS && !defined(__BORLANDC__) */ #if defined(PYOS_OS2) Backtrace: KernelBase.dll!00007ff963466142() Unknown > python27_d.dll!Py_FatalError(const char * msg) Line 1700 C python27_d.dll!PyThreadState_Get() Line 332 C python27_d.dll!set_dealloc(_setobject * so) Line 553 C python27_d.dll!_Py_Dealloc(_object * op) Line 2263 C python27_d.dll!PySet_Fini() Line 1084 C python27_d.dll!Py_Finalize() Line 526 C mn.exe!main(int argc, char * * argv) Line 40 C [External Code] Reproducing code: #include <Python.h> int main(int argc, char** argv) { // http://www.awasu.com/weblog/embedding-python/threads // #### Comment this to avoid crash Py_NoSiteFlag = 1; Py_Initialize(); PyEval_InitThreads(); // nb: creates and locks the GIL // NOTE: We save the current thread state, and restore it when we unload, // so that we can clean up properly. PyThreadState* pMainThreadState = PyEval_SaveThread(); // nb: this also releases the GIL PyEval_AcquireLock(); // nb: get the GIL PyThreadState* pThreadState = Py_NewInterpreter(); assert(pThreadState != NULL); PyEval_ReleaseThread(pThreadState); // nb: this also releases the GIL PyEval_AcquireThread(pThreadState); // Can reproduce by importing the os module, but the issue actually appears // because of the use of frozenset, so simplify to that. #if 0 PyObject* osModule = PyImport_ImportModule("os"); Py_DECREF(osModule); #endif // As in abc.py ABCMeta class PyRun_SimpleString("abstractmethods = frozenset(set())"); PyEval_ReleaseThread(pThreadState); // release the interpreter PyEval_AcquireThread(pThreadState); // nb: this also locks the GIL Py_EndInterpreter(pThreadState); PyEval_ReleaseLock(); // nb: release the GIL // clean up PyEval_RestoreThread(pMainThreadState); // nb: this also locks the GIL Py_Finalize(); } ---------- components: Interpreter Core messages: 291568 nosy: steveire priority: normal severity: normal status: open title: Crash on Py_Finalize if Py_NoSiteFlag is used type: crash versions: Python 2.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30060> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com