This is most probably due to incorrect linkage. The crash is common when the File*
in your code is different from the File*
in the python lib you linked against. This can happen when the libraries liked against have been compiled by a different compiler or different version of the compiler, that uses a different runtime.
Why is PyObject_Print crashing?
-
09-03-2022 - |
Question
I've been playing with the Python 3.3 C-API to see if I could possibly use it in a planned upcoming project, and almost immediately ran into problems.
Even code this simple crashes with 0xc0000005 as the return:
#include <Python.h>
#include <cstdio>
int main(){
Py_Initialize();
Py_IncRef(Py_True); //just in case?
PyObject_Print(Py_True,stdout,Py_PRINT_RAW);
Py_DecRef(Py_True);
Py_Finalize();
return 0;
}
Testing shows PyObject_Print is generating the crash. What's wrong with this code and/or my setup?
Solution
OTHER TIPS
I've had PyObject_Print() crashing when i moved a C module from Pyhton 2.x -> 3.x
First off, check the MS VS version (MSC) in your C module matches the version reported by Python.
For example, running python.exe reports: Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Add an fprintf to your module's init function:
fprintf(stderr, "C - MSC_VER %d\n", _MSC_VER);
giving:
C - MSC_VER 1600
Secondly, check your command line options. Again, the commands for my simple test module were:
cl.exe /Fosmod.obj /c /I "%INCLUDE%" /I c:\python33\include smod.c link.exe /dll /out:smod.pyd smod.obj /LIBPATH:c:\python33\libs
These caused my module to crash in PyObject_Print()!
A quick read of the 'cl' command line options and I added '/MD':
cl.exe /MD /Fosmod.obj /c /I "%INCLUDE%" /I c:\python33\include smod.c link.exe /dll /out:smod.pyd smod.obj /LIBPATH:c:\python33\libs
Fixed!