我基本上是在尝试做与以下问题相同的事情,但我在调用 PyObject_Print
时遇到运行时错误,提示“toupper() at 0x776e0226”中的错误
Python C API: Using PyEval_EvalCode
我的代码是:
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()"
并且 pModule 是早些时候从脚本初始化的,没有错误,并且按照以下代码工作:http://docs.python.org/2/extending/embedding.html#pure-embedding .
在加载的模块中有函数:
def setCurrentFileDir():
print "setCurrentFileDir"
return "5"
我在 eval 函数调用中错过了什么/做错了什么。请注意,我不能通过 python API“直接”调用函数 setCurrentFileDir,我必须使用 eval。
最佳答案
实际上这很好用。但是,我在目录中有一个 .pyd 文件,python 必须从较旧的 python 脚本运行中构建(并且 eclipse 对我隐藏),该脚本没有定义函数,导致了问题。
从这里我了解到问题可能出在 .pyd 文件上。
python NameError: name '<anything>' is not defined (but it is!)
关于c++ - 从 C++ 调用 python eval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14483804/