1

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
2
  • Why aren't you checking the result of each step? Commented Jul 20, 2011 at 9:12
  • I didn't check because I thought m & d wouldn't change because they have no connection to the code. And this is true: None of them returns an error. Commented Jul 21, 2011 at 13:30

3 Answers 3

6

The actual reason why this fails is not that you're calling Py_DECREF too early. It's that you're calling at at all!

PyImport_AddModule returns a borrowed reference. Which means you're not allowed to call Py_DECREF at all unless you have actually taken control over it (for example by increasing the reference count via Py_INCREF).

Python will automatically garbage collect the module reference on Py_Finalize. No additional action necessary.

1
if (!r) {
    PyErr_Print();
    PyErr_Clear(); 
}

Will clear the error and allow you to call Py_Finalize() successfully.

3
  • What happens if you add PyErr_Print(0); on the line after PyErr_Print();? If that doesn't work, please edit your post to provide the actual crash information.
    – agf
    Commented Jul 20, 2011 at 6:57
  • Too many arguments to funciton 'void PyErr_Print()'. I'm running Windows how do i obtain actual crash information? The program just closes with a dialog box "test.exe is not working anymore". Commented Jul 20, 2011 at 7:06
  • That's a big question. How are you compiling? If it's with gcc, try Googling "gdb windows", if it's Visual Studio or similar, Google "debugging with Visual Studio" or something like that.
    – agf
    Commented Jul 20, 2011 at 7:10
0

Ok I have found the error myself now. You can only call Py_DECREF(m) after evalizing the code and checking it for errors. So the working program is this:

Py_Initialize();

PyObject* code = Py_CompileString("pprint('Hello World')", "", Py_file_input);
PyObject* m = PyImport_AddModule("__main__");
PyObject* d = PyModule_GetDict(m);
PyObject* r = PyEval_EvalCode(code, d, d);
Py_DECREF(d);
if (!r)
    PyErr_Print();
Py_DECREF(m);
Py_DECREF(code);

Py_Finalize();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.