[Python-Dev] PyThreadState_GET() returns NULL from within PyImport_GetModuleDict()

Patrick Rutkowski patrick.rutkowski at gmail.com
Mon Aug 7 20:11:13 EDT 2017


I'm working on Windows. I have the following dead simple embedding
code that I'm using to test out the python interpreter:

============================================================

Py_SetProgramName(L"MyApp");

Py_SetPath(
    L"C:\\Users\\rutski\\Documents\\python\\PCBuild\\amd64\\python36.zip;"
    L"C:\\Users\\rutski\\Documents\\python\\DLLs;"
    L"C:\\Users\\rutski\\Documents\\python\\lib;"
    L"C:\\Users\\rutski\\Documents\\python\\PCBuild\\amd64;"
    L"C:\\Users\\rutski\\Documents\\python;"
    L"C:\\Users\\rutski\\Documents\\python\\lib\\site-packages");

Py_Initialize();

PyRun_SimpleString(
    "from time import time,ctime\n"
    "print('Today is', ctime(time()))\n");

============================================================

This code crashes trying to access address 0x00000010 from within
PyRun_SimpleString(). The sequence of event's is this:

1) PyRun_SimpleString() tries to do AddModule("__main__")
2) AddModule tries to do PyImport_GetModuleDict()
3) PyImport_GetModuleDict() tries to doPyThreadState_GET()->interp
4) PyThreadState_GET() returns NULL, so the ->interp part crashes.

The weird thing is that calling PyImport_GetModuleDict() from within
my application directly works just fine. Weirder still is that the
whole thing actually executes fine if I build a windows command line
application with the embed code in main(), and execute it from a
terminal. The crash only happens when building a Windows GUI
application and calling the embed code in WinMain().

This is a python interpreter that I built from source on windows using
PCbuild\build.bat, so that I could track the crash. However, the exact
same crash was happening with the stock interpreter provided by the
python windows installer.

Does anyone have any ideas here?
-Patrick


More information about the Python-Dev mailing list