I'm trying to figure out how to use the Python interpreter from C, and I'm having trouble with PyEval_EvalCode
. Basically, I'm writing a C function which takes in an arbitrary string of Python code, compiles it, executes it, and then prints out the result.
The problem is that when I print out the result, I always get None
, even if the expression obviously doesn't evaluate to None
.
Here is the code (with error checking and reference counting removed for clarity):
void eval(const char* s)
{
PyCodeObject* code = (PyCodeObject*) Py_CompileString(s, "test", Py_file_input);
PyObject* main_module = PyImport_AddModule("__main__");
PyObject* global_dict = PyModule_GetDict(main_module);
PyObject* local_dict = PyDict_New();
PyObject* obj = PyEval_EvalCode(code, global_dict, local_dict);
PyObject* result = PyObject_Str(obj);
PyObject_Print(result, stdout, 0);
}
I tried calling this function with "5 + 5"
as the input, and it displayed None
. Am I using PyEval_EvalCode
incorrectly?