By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

embedding python -- windows specify problems

P: n/a
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/a...johnpye/extpy/

Any suggestions very much appreciated!

Cheers
JP
Oct 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John Pye wrote:
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>

Oct 5 '06 #2

P: n/a
the contents and the layout of the FILE structure isn't defined

"isn't standardized", that is.

</F>

Oct 5 '06 #3

P: n/a
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

Fredrik Lundh wrote:
John Pye wrote:

>>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>
Oct 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.