[C++-sig] [boost.python] convert swig-wrapped c++-return values to python with boost.python

Maximilian Matthe Maxi.Matthe at web.de
Sun Jul 20 11:53:27 CEST 2008


Hi!

I still have the problem, that I have described below. I found out, that 
the same problem appears with any class that is not registered with 
boost.python but only with a custom converter. Returning values of those 
custom objects works fine, but returning pointers to existing objects 
gives the conversion error.
Traceback (most recent call last):
     File "test.py", line 31, in testit
       w = getWindow()
  TypeError: No Python class registered for C++ class class wxWindow.

I read about to_python_indirect, and ResultConverterGenrator and so on, 
but the documentation remains unclear to me. I dont know, which 
operator/converter I have to add, that boost.python can convert pointers 
to existing objects, using the return value policiy 
reference_existing_object or something similar.

Passing the pointers via shared_ptr also seems to work, but that is no 
possibility to me, cause my whole app is without shared_ptr and my 
functions only return pointers to always existing objects, like the main 
application window.
It must be possible anyway... Max

Ralf W. Grosse-Kunstleve schrieb:
> That looks interesting...
> Do you have a long list of boost::python::to_python_converter<> instantiations?
> Ralf
> 
> 
> 
> 
> ----- Original Message ----
> From: Maximilian Matthe <Maxi.Matthe at web.de>
> To: c++-sig at python.org
> Sent: Wednesday, July 16, 2008 4:11:02 PM
> Subject: [C++-sig] [boost.python] convert swig-wrapped c++-return values to python with boost.python
> 
> Hello!
> 
> I know, this question is somehow often asked, but I did not find the 
> solution for my problem.
> 
> I managed to implement a simple to_python converter for any wxObject:
> 
> template <class Obj>
> struct convert_wxObject
> {
>     static PyObject* convert(Obj const& o)
>     {
>         PyObject* arg = wxPyMake_wxObject(const_cast<Obj*>(&o), false);
>         if(!arg)
>             throw python_error("Object is not convertible to wxPython!");
>         return arg;
>     }
> };
> 
> This struct works for any wxWidgets object. At least if I want to
> pass them from c++ to python.
> 
> What does not work is the following:
> 
> // c++:
> wxWindow* getWindow();
> 
> // python:
> getWindow().Hide()
> 
> I get the following error:
> Traceback (most recent call last):
>    File "test.py", line 31, in testit
>      getWindow().Hide()
> TypeError: No Python class registered for C++ class class wxWindow.
> 
> How can I tell boost.python to use my conversion above? Practically it 
> seems to be the same to me, if I convert a return value from a 
> c++-function to python or convert a c++-parameter for a python function. 
> Both is converting from c++ to python. so why does that not work?
> 
> I appreciate any help :-)
> Max
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list