[C++-sig] using extract with embedded boost::python

Stefan Seefeld seefeld at sympatico.ca
Sun Jun 3 21:21:23 CEST 2007


Simon Pickles wrote:
> Sorry, A cut-n-paste error, the code which works is the boost example:
> 
> object main_module((
>      handle<>(borrowed(PyImport_AddModule("__main__")))));
> 
> object main_namespace = main_module.attr("__dict__");
> 
> handle<> ignored((PyRun_String(
> 
>     "result = 5 ** 2"
> 
>     , Py_file_input
>     , main_namespace.ptr()
>     , main_namespace.ptr())
> ));
> 
> int five_squared = extract<int>(main_namespace["result"]);

Please have a look at the tests that are part of the boost source distribution.
Notably, libs/python/tests/exec.cpp contains functionality similar to the above,
and is shown to work correctly on all tested platforms.

Also, it would be good to figure out what (python) exception is raised (indicated
by the exception_already_set in C++). You can query this using PyErr_Fetch().
(I'm hoping I will get around to wrap that and related functions in boost.python
in time for boost 1.35.)

Regards,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list