[C++-sig] Help with custom converters

Lloyd Weehuizen lloyd at fusion.net.nz
Tue Jan 1 02:40:57 CET 2008


Hi

I'm having a strange problem when using custom converters, they seem to 
work fine when I have a function/method that returns an object type but 
fails if I try use the object as a boost::python::object.

The error I get is:

TypeError: No to_python (by-value) converter found for C++ type: class 
Interface

For example:

Interface* TestConverter(Interface* interface)
{
    return interface;
}

Works correctly, the python class instance that inherits from Interface 
passes correctly through the TestConverter method when called from python.

However, if I try this:
Interface* TestConverter(Interface* interface)
{
    boost::python::object obj(interface);
    return interface;
}

Then the TypeError is thrown. I would have thought the converter would 
be called for this case as well?

Here's the converter code:

template < typename T >
struct ConverterScriptObject
{
   ConverterScriptObject()
   {
     // Register custom the to python converter
     boost::python::to_python_converter< T*, ConverterScriptObject< T > >();
   }

   static PyObject* convert(T* object)
   {
     PyObject* pyobject = Py_None;
     if (object)
     {
       PyObject* script_object = object->GetScriptObject();
       if (script_object)
       {
         pyobject = script_object;
       }
     }
     Py_INCREF(pyobject);
     return pyobject;
    }
};
ConverterScriptObject< Interface >();


Thanks,
Lloyd



More information about the Cplusplus-sig mailing list