Reputation: 4050
I'm working on Windows with Python 3.6. 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 events is this:
PyRun_SimpleString() does AddModule("__main__")
AddModule does PyImport_GetModuleDict()
PyImport_GetModuleDict() tries to doPyThreadState_GET()->interp
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.er 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.
A related post seems to be here: Embedding Python, works in main() but not in WinMain()
Though that thread has not been resolved, and anyway it's not crashing in quite the same way. Perhaps this is just due to them being on 3.4 and my being on 3.6. Maybe the underlying issue the same.
Upvotes: 2
Views: 678
Reputation: 4050
After much stumbling around in the dark I have the answer. The following table shows the results of tests which I performed pairing different build configurations of my WinMain() app with different python libraries. The terms "Release" and "Debug" refer to the configuration type in Visual Studio. All builds were done in x64 mode.
Debug | python36.lib | Works
Debug | python36_d.lib | Works
Debug | python3_d.lib | Works
Debug | python3.lib | !!! CRASHES !!!
Release | python36.lib | Works
Release | python36_d.lib | Works
Release | python3_d.lib | !!! CRASHES !!!
Release | python3.lib | Works
So, it seems to be the case that picking a mismatched python binary causes the crash, but only with python3, not with python36. This makes me wonder what the differences is between the two in the first place. I was getting the crash to begin with because I was linking my Debug build with the release build python3.lib, since I thought it shouldn't matter.
My problem is fixed now, but if anyone could sheld light on the details of why exactly it happened then I would certainy be interested.
Upvotes: 1