Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2003-11-11 06:59:57


"Paul Grenyer" <paul_at_[hidden]> 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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net