[C++-sig] Re: Converting a C++ object to python

David Abrahams dave at boost-consulting.com
Mon Sep 15 17:25:12 CEST 2003


"Ron Penton" <rpenton at adelphia.net> writes:

> I'm trying to perform this sequence of events:
> 1 create a C++ object
> 2 convert it into a python object
> 3 pass it into python as a parameter to a function
>
>
> Part 2 is the part that is holding me up. Observe:
>
> class booga
> {
> public:
>     booga() : a( 0 ) {}
>     int a;
>     void looga() { a = a + 10; cout << a << endl; }
> };
>
> BOOST_PYTHON_MODULE( wrap )
> {
>     class_<booga>( "booga" )
>         .def( "looga", &booga::looga );
> }
>
> int main()
> {
>     Py_Initialize();    
>
>     PyImport_AppendInittab( "wrap", initwrap );
>
>     booga b;
>     object obj( &b );
>
>
> That last line of code always throws an exception of type
> error_already_set, from within the function "value_arg_to_python".
>
> My thoughts are that I am missing something about a custom converter,
> or similar, but I've read through the converter docs and I'm quite
> baffled; they don't explain much. Would the BOOST_PYTHON_MODULE create
> a custom converter for classes in there, or must I specify one
> manually, or what?

The basic problem is that you haven't done anything to import the
"wrap" module (and thus cause the "booga" converters to be registered)
at the point you are trying to do the conversion.

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





More information about the Cplusplus-sig mailing list