Question

I could not get PyRun_SimpleFile to work (FILE* compatibility issue) for files that have unicode (widechar) in their name/path and hence this question!

So, I decided to open the python script myself & then execute each line using PyRun_SimpleString.

Pseudo code shown here.

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 gives error at the def statement below (<-- shown below)

Python version is 2.7

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

I would like to use this to show the line number of the script where if/it fails. Many other people seem to be successful with PyRun_SimpleString!

Thanks in advance.

Était-ce utile?

La solution

You wouldn't use PyRun_SimpleString in this case because it expects to read the entirety of the program in one line, you are breaking it into multiple lines.

You should just use PyRun_SimpleFile(fileReference, scriptPath)

Note: you'll need to create the globals and locals objects for the above to work.

See this

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top