Calling python eval from C ++
I am basically trying to do the same as the next question, but I get runtime errors when called PyObject_Print
, complaining about an error in "toupper () at 0x776e0226"
Python C API: Using PyEval_EvalCode
My code:
int callExecFunction(const char* evalStr)
{
PyCodeObject* code = (PyCodeObject*)Py_CompileString(evalStr, "pyscript", Py_eval_input);
PyObject* global_dict = PyModule_GetDict(pModule);
PyObject* local_dict = PyDict_New();
PyObject* obj = PyEval_EvalCode(code, global_dict, local_dict);
PyObject* result = PyObject_Str(obj);
PyObject_Print(result, stdout, 0);
}
evalStr "setCurrentFileDir()"
and pModule was initialized earlier from script without error and worked like this code: http://docs.python.org/2/extending/embedding.html#pure-embedding .
And inside the loaded module there is a function:
def setCurrentFileDir():
print "setCurrentFileDir"
return "5"
What I missed / did wrong in the call to the eval function. Please note that I cannot call the setCurrentFileDir function "directly" through the python API, I have to use eval.
source to share
It actually works great. However, I had a .pyd file in a directory that was supposed to build python (and eclipse from me) from an older python script run that didn't have the function causing the problem.
This is where I figured out that the problem might be a .pyd file.
python NameError: name '<anything>' is undefined (but it is!)
source to share