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

Raoul Gough RaoulGough at yahoo.co.uk
Mon Sep 15 13:02:02 CEST 2003


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

>>If you catch a boost::python::error_already_set in C++, I guess you
>>can get more information by calling PyErr_Print(), which should dump a
>>standard Python traceback on stderr. At a guess, you're getting a
>>Python TypeError from the object constructor, because it doesn't have
>>a converter for booga *. BTW, why are you passing a pointer to b,
>>instead of a reference?
>
> PyErr_Print says "TypeError: No Python class registered for C++ class class
> booga",
> which confirms what you thought.

Well, not really - I would have thought that there is a problem with
booga * (i.e. pointer to booga), but from your error message, that is
not the case. I don't suppose it makes any difference to use b instead
of &b then?

>
> The part that confuses me is the converter part; I'm not quite sure how to
> go about creating one. All of the examples I can find go about the creation
> of the python types manually, rather than using the class_ definition, so
> I'm really unsure of where to go from here.

Well, we've now snipped the original code, but looking back at it from
your original post:

> 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", &amp;booga::looga 
> );}
> int main(){    Py_Initialize();    
>  
>     PyImport_AppendInittab( 
> "wrap", initwrap );
>     booga b;    
> object obj( &amp;b );

the class_<booga> constructor should handle all of the converter
registration. However, I know that at least *some* of the registration
is done at run-time. I'm not sure whether that applies to the
to-python converters or not (I would guess not, actually). Anyway, why
not add some std::cout in the BOOST_PYTHON_MODULE function to make
sure that it is actually being run (especially given the doubt about
using PyImport_AppendInittab)

>
>
>>I don't have any experience embedding Python in C++, because I work
>>the other way around (i.e. using C++ extensions from within Python).
>>However, the documentation for PyImport_AppendInittab says that you
>>should call it *before* Py_Initialize. I would have thought you want a
>>normal import of your module anyway, rather than trying to make it act
>>like a built-in.
>
> It works either way. For the time being I'm just playing around trying to
> see what can be done.

You mean it runs initwrap either way? If that is the case, I'm really
not sure what else I can suggest. I don't actually understand how the
object constructor is supposed to know what to do with a raw C++
reference (or pointer for that matter) since it doesn't know whether
to copy the C++ object or wrap a refrence to it. This is normally all
handled by the "def" function, where you pass it a return policy
specifying the copy or reference behaviour.

-- 
Raoul Gough.
(setq dabbrev-case-fold-search nil)





More information about the Cplusplus-sig mailing list