[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 16:15:20 CEST 2008


David Abrahams schrieb:
> on Sun Jul 20 2008, Maximilian Matthe <Maxi.Matthe-AT-web.de> wrote:
> 
>> David Abrahams schrieb:
>>> on Sun Jul 20 2008, Maximilian Matthe <Maxi.Matthe-AT-web.de> wrote:
>>>
>>>> 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.
>>> You probably need to register a converter for the pointer type
>>> (wxWindow*) as well as the class type.  Actually I'd be a little
>>> surprised if you were actually returning wxWindow by value.
>>>
>> I have already tried to add a converter for wxWindow*, but there were
>> compiler errors that convert needs by value of const reference
>> parameter.
> 
>   wxWindow* const&
> 
> ?
> 
>> I dont want to return wxWindow by value, but by reference/adress. Thus
>> the bp::reference_existing_object call policy.
> 
> 
Well, i checked that, but the problem remains:
Heres the code:

template <class Ptr>
struct convert_wxObjectPtr
{
   static PyObject* convert(Ptr const& o)
   {
     PyObject* arg = wxPyMake_wxObject(const_cast<Ptr>(o), false);
     if(!arg)
       throw python_error("Object is not convertible to wxPython!");
     return arg;
   }
};
// code generated by Py++
struct IApp_wrapper : IApp, bp::wrapper< IApp > {
   virtual wxWindow * GetWndMain(  ){
     bp::override func_GetWndMain = this->get_override( "GetWndMain");
     return func_GetWndMain(  );
   }
};

// generated by Py++
BOOST_PYTHON_MODULE(ai) {
bp::class_< IApp_wrapper, boost::noncopyable >( "IApp" )
.def("GetWndMain", bp::pure_virtual( 
(::wxWindow*(IApp::*)(())(&::IApp::GetWndMain) ), 
bp::return_value_policy< bp::reference_existing_object >() );

// provide custom converters.
to_python_converter<wxWindow, convert_wxObject<wxWindow>, false>();
to_python_converter<wxWindow*, convert_wxObjectPtr<wxWindow*>, false>();
}

////// python:
////////////////
# a is a pointer to IApp_wrapper
a.GetWndMain().Hide()
Traceback (most recent call last):
   File "PythonPlugin.py", line 34, in testit
     a.GetWndMain().Hide()
TypeError: No Python class registered for C++ class class wxWindow




More information about the Cplusplus-sig mailing list