我想打電話給使用Py_CompileString()
和PyEval_EvalCode()
一些Python代碼。它工作正常,但是當Python代碼包含錯誤Py_Finalize()
craches。Py_Finalize()錯誤後崩潰在Python
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();
的輸出爲預期:
Traceback (most recent call last):
File "", line 1, in <module>
NameError: name 'pprint' is no defined
但隨後在調用Py_Finalize()
crashes.If我改線3
PyObject* code = Py_CompileString("print('Hello World')", "", Py_file_input);
程序運行和終止罰款程序。這裏有什麼問題?
如果我在gdb運行程序我得到這個輸出:
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
你爲什麼不檢查每個步驟的結果? –
我沒有檢查,因爲我認爲m&d不會改變,因爲他們沒有連接到代碼。這是真的:它們都不會返回錯誤。 –