embedding python -- windows specify problems

J

John Pye

Hi all

I have been working on some new code that embeds python in an C
application. The embedding is working fine under Linux but crashing
under Windows (XP) when I reach the following step.

PyRun_AnyFile(f,name);

If there's some python exception being thrown by the PyRun_AnyFile call,
how can I retrieve it from C?

Even when the file (f) being run by Python contains only a 'print'
statement, the application still crashes on WinXP. The linking to Python
seems to be working OK, and the following C code that precedes the above
PyRun_AnyFile statement seems to be working fine:

PyRun_SimpleString("import ascpy");
PyRun_SimpleString("L = ascpy.Library()");
PyRun_SimpleString("print L");

Can anyone suggest what some good diagnostic steps would be?

My embedding code is at the following link. I'm happy to explain the
architecture in more detail if that helps.
https://pse.cheme.cmu.edu/svn-view/ascend/code/branches/extfn/models/johnpye/extpy/

Any suggestions very much appreciated!

Cheers
JP
 
F

Fredrik Lundh

John said:
I have been working on some new code that embeds python in an C
application. The embedding is working fine under Linux but crashing
under Windows (XP) when I reach the following step.

PyRun_AnyFile(f,name);

If there's some python exception being thrown by the PyRun_AnyFile call,
how can I retrieve it from C?

Even when the file (f) being run by Python contains only a 'print'
statement, the application still crashes on WinXP.

the contents and the layout of the FILE structure isn't defined, so PyRun_AnyFile()
only works if you make sure to open the file using *exactly* the same run time library
as Python itself uses.

it's usually easier to hand the problem over to the interpreter:

PyRun_SimpleString("execfile('myscript.py')\n");

the same approach can be used to implement custom error handling, stdout/stderr
redirects, etc.

</F>
 
F

Fredrik Lundh

the contents and the layout of the FILE structure isn't defined

"isn't standardized", that is.

</F>
 
J

John Pye

Hi Fredrik,

Thanks very much for that reply. Your suggestion sounds feasible, I
guess. Taking what you said, and thinking about how I could avoid adding
an additional intepreter step, I thought that I could try the following:

pyfile = PyFile_FromString(name,"r");
if(pyfile==NULL){
return 1;
}
f = PyFile_AsFile(pyfile);
PyRun_AnyFileEx(f,name,1);

This seems to work on Linux at least (I'll test it tomorrow on my
Windows machine), and presumably it will work around the problem you
mentioned?

Cheers
JP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top