[C++-sig] Re: Boost.Python scope

Nicolas Fleury nidoizo at yahoo.com
Fri Nov 7 19:06:16 CET 2003


Stefan Seefeld wrote:

> Nicolas Fleury wrote:
>> Hi Stefan,
>>     I guess I miss something in the doc... So how do I execute 
>> something like "import pickle; pickle.load(open('myfile'))" and store 
>> the result in a boost::python::object?  (or any better way)
> 
> 
> python provides a couple of interfaces to deal with objects in general,
> and specific types (dicts, strings, tuples, etc.) in particular. These
> are mapped to C++ in boost.python, and the docs are here:
> 
> http://www.boost.org/libs/python/doc/tutorial/doc/basic_interface.html
> http://www.boost.org/libs/python/doc/tutorial/doc/derived_object_types.html
> 
> the only missing thing is the execution of a little python snippet
> (file or string), which is described at
> 
> http://www.boost.org/libs/python/doc/tutorial/doc/using_the_interpreter.html 
> 
> 
> hope this helps,
>         Stefan

Thx Stefan,
If I undertand correctly, the way of doing it is something like:

handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
main_namespace dict(handle<>(borrowed(
    PyModule_GetDict(main_module.get()) )));
object result(handle<>(
    PyRun_String("import pickle; pickle.load(open('myfile'))",
                 Py_eval_input, main_namespace.ptr(),
                 main_namespace.ptr())))

This is very nice, since I have a way of working with a 
boost::python::object.  However, my question remain, should Boost.Python 
scope/goal be enlarged to have no need for Py... C functions, providing 
a full C++ API for Python/C++ interaction?

Regards,

Nicolas






More information about the Cplusplus-sig mailing list