Exception using boost::python and PyRun_file
Hi, [newbie alert] I am trying to embed python in c++ with boost::python. I'm using MSVC++ 2005 Express Boost 1.34 Python 2.52 Unfortunately, I've come unstuck already. Here's my code object main_module( ( handle<>( borrowed( PyImport_AddModule( "__main__" ) ) ) ) ); object main_namespace = main_module.attr("__dict__"); FILE* myPython = fopen( "myPython.py", "r" ); handle<> ignored( ( PyRun_File( myPython, "myPython.py", Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) ) ); fclose( myPython ); and heres the exception: First-chance exception at 0x7c918fea in Server 20070601.exe: 0xC0000005: Access violation writing location 0x00000010. Unhandled exception at 0x7c918fea in Server 20070601.exe: 0xC0000005: Access violation writing location 0x00000010. What am I doing wrong? must be something simple :) Thanks! Simon http://www.simonpickles.com --- http://www.squirtualreality.com _________________________________________________________________ Play your part in making history - Email Britain! http://www.emailbritain.co.uk/
Simon Pickles wrote:
Here's my code
object main_module( ( handle<>( borrowed( PyImport_AddModule( "__main__" ) ) ) ) );
object main_namespace = main_module.attr("__dict__");
FILE* myPython = fopen( "myPython.py", "r" );
handle<> ignored( ( PyRun_File( myPython, "myPython.py", Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) ) );
fclose( myPython );
Please try this instead: python::object main = python::import("__main__"); python::object global(main.attr("__dict__")); python::object result = python::exec_file("myPython.py", global, global); (This API is new in Boost.Python 1.34.) I'm not sure what may have caused the error. One likely cause is that boost and your own application have not been compiled with the same compiler (version), leading to ABI incompatibilities such as different FILE definitions. But that's just a guess, of course. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...
participants (2)
-
Simon Pickles -
Stefan Seefeld