def foo()的PyRun_SimpleString失败:

时间:2013-02-19 21:48:54

标签: c++ python python-embedding

对于在名称/路径中包含unicode(widechar)的文件,我无法使PyRun_SimpleFile工作(FILE *兼容性问题),因此这个问题!

所以,我决定自己打开python脚本&然后使用PyRun_SimpleString执行每一行。

此处显示的伪代码。

wchar_t* pWScriptName=NULL;
// pWScriptName malloced & populated here
FILE* fp = _wfopen(pWScriptName, L"r");
while (fgets(line, BUFSIZ, fp) != NULL) {
    int ret = PyRun_SimpleString(line);
    if(ret != 0) {
        ... error at lineNum ...
    }
    lineNum++;
}

Above在下面的def语句中给出了错误(< - 如下所示)

Python版本是2.7

import os
print "hello"
def foo():  # <-- PyRun_SimpleString fails here
    print "hello again"

我想用它来显示脚本的行号,其中if / it失败。许多其他人似乎都成功使用PyRun_SimpleString!

提前致谢。

1 个答案:

答案 0 :(得分:3)

在这种情况下你不会使用PyRun_SimpleString,因为它希望在一行中读取整个程序,你将它分成多行。

您应该使用PyRun_SimpleFile(fileReference, scriptPath)

注意:您需要创建全局和局部对象才能使上述对象生效。

See this