This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steveire
Recipients steveire
Date 2017-04-12.18:01:10
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1492020071.31.0.759008537591.issue30060@psf.upfronthosting.co.za>
In-reply-to
Content
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();
}
History
Date User Action Args
2017-04-12 18:01:11steveiresetrecipients: + steveire
2017-04-12 18:01:11steveiresetmessageid: <1492020071.31.0.759008537591.issue30060@psf.upfronthosting.co.za>
2017-04-12 18:01:11steveirelinkissue30060 messages
2017-04-12 18:01:10steveirecreate