embedding python -- windows specify problems

John Pye john+news at curioussymbols.com
Thu Oct 5 09:01:50 EDT 2006


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> 
> 
> 
> 



More information about the Python-list mailing list