c++ - 从 C++ 调用 python eval

标签 c++ python embedding

我基本上是在尝试做与以下问题相同的事情,但我在调用 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/

相关文章:

python - GitHub 上的手语手套项目 : Help in understanding code

python - 如何高效地逐行处理python中的字符串?

azure - 将项目上传到 Azure 认知搜索索引时出现 'Request Entity Too Large' 错误 - 如何解决?

python - youtube-dl下载带有formatid的视频

c++ - Alpine 图像 standard_init_linux.go :207: exec user process caused "no such file or directory"

c++ - vector :初始化还是保留?

python - 我应该使用 GET 还是 POST 来检索结果 Django

jquery - 由于嵌入的视频太多,我的网站加载缓慢

c++ - 类模板特化中的成员变量别名

c++ - 将文本文件创建到 node.js 的 C++ 插件中