对于def foo(),PyRun_SimpleString失败:

PyRun_SimpleString fails for def foo():

本文关键字:SimpleString 失败 PyRun def foo 对于      更新时间:2023-10-16

对于名称/路径中包含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++;
}

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

Python版本为2.7

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

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

提前谢谢。

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

你应该只使用PyRun_SimpleFile(fileReference, scriptPath)

注意:您需要创建globals和locals对象,以上操作才能正常工作。

请参阅此