[C++-sig] Exception using boost::python and PyRun_file

Stefan Seefeld seefeld at sympatico.ca
Fri Jun 1 23:06:49 CEST 2007


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



More information about the Cplusplus-sig mailing list