I'm trying to call some python code using Py_CompileString() and PyEval_EvalCode(). It works fine, but when the Python Code contains an error Py_Finalize() craches.
Py_Initialize();
PyObject* code = Py_CompileString("pprint('Hello World')", "", Py_file_input);
PyObject* m = PyImport_AddModule("__main__");
PyObject* d = PyModule_GetDict(m);
Py_DECREF(m);
PyObject* r = PyEval_EvalCode(code, d, d);
Py_DECREF(d);
if (!r)
PyErr_Print();
Py_DECREF(code);
Py_Finalize();
The Output is as expected:
Traceback (most recent call last):
File "", line 1, in <module>
NameError: name 'pprint' is no defined
But then at the call to Py_Finalize() the program crashes.If I change line 3 to
PyObject* code = Py_CompileString("print('Hello World')", "", Py_file_input);
the program runs and terminates fine. Whats going wrong here?
If i run the program in gdb i get this output:
Windows:
Program received signal SIGSEGV, Segmentation fault.
0x1e01a030 in python32!PyType_IsSubtype () from C:\Windows\SysWOW64\python32.dll
Linux:
Program received signal SIGSEGV, Segmentation fault.
0xb7ef17bb in visit_decref (op=0xb78c87ec, data=0x0) at Modules/gcmodule.c:321
321 Modules/gcmodule.c: File or Directory not found.
in Modules/gcmodule.c