[C++-sig] Re: Boost Python: Beyond handles

David Abrahams dave at boost-consulting.com
Tue Nov 11 12:59:57 CET 2003


The following message is a courtesy copy of an article
that has been posted to gmane.comp.lib.boost.user as well.

"Paul Grenyer" <paul at paulgrenyer.co.uk> writes:

> Hi All
>
> I'm trying to embed the python interpreter in a C++ program so that 
> users can write simple scripts that can be executed when the program 
> runs. I've been following the "Using the the Interpretter" examples an 
> dhave had quite a lot of success, except with this one:
>
> handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
>
> handle<> main_namespace(borrowed( PyModule_GetDict(main_module.get
> ()) ));
>
> handle<> main_module( borrowed( PyImport_AddModule("__main__") ));
>
> main_namespace dict(handle<>(borrowed( PyModule_GetDict(main_module.get
> ()) )));
>
> handle<>( PyRun_String(" result = 5 ** 2", 
>                          Py_file_input,
>                          main_namespace.ptr(), 
>                          main_namespace.ptr()) );
>
> int five_squared = extract<int>( main_namespace["result"] );
>
> I'm using boost 1.30.0 and MSVC 7.1, along with the following warning
> (?!!):
>
> builtin_converters.hpp(114) : warning C4267: 'argument' : conversion 
> from 'size_t' to 'int', possible loss of data
>
> I get the following errors:
>
>  error C2146: syntax error : missing ';' before identifier 'dict'
>  error C2039: 'ptr' : is not a member of 'boost::python::handle<>'
>  error C2039: 'ptr' : is not a member of 'boost::python::handle<>'
>  error C2676: binary '[' : 'boost::python::handle<>' does not define 
> this operator or a conversion to a type acceptable to the predefined 
> operator
>
> The "missing ';' before identifier 'dict'" error I can understand and I 
> ppear to be able to fix by swapping "main_namespace" for "handle<>". I 
> can also fix the "'ptr' : is not a member of 'boost::python::handle<>'" 
> error by changing ptr to get. However, the final error has me stumped.

I suggest posting Boost.Python questions to the C++-sig at
http://www.boost.org/more/mailing_lists.htm#cplussig.  I also suggest
looking at the CVS version of the tutorial

  http://tinyurl.com/uiis

or at:

  http://tinyurl.com/uiiu

Which shows a working example.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list