[C++-sig] Wrapping classes that inherit from classes already wrapped

Jim Bosch talljimbo at gmail.com
Fri Feb 17 23:42:18 CET 2012


On 02/14/2012 01:56 PM, Alex Olivas wrote:
> Hey All,
> I was working on a project a few months ago where I
> was wrapping a lib ( using Boost.Python ) that inherited
> from classes that were already wrapped ( using SIP ).
> Boost.Python however couldn't find the base classes. I
> hacked something up ( that I'm not very proud of ) in class.cpp :
>
> type_handle get_class(type_info id)
> {
> type_handle result(query_class(id));
>
> if (result.get() == 0)
> {
> PyObject* globals = PyEval_GetGlobals() ;
> PyObject* key = object( id.name() ).ptr() ;
> PyObject* obj = PyDict_GetItem( globals, key );
> if( obj ){
> return type_handle( obj->ob_type );
> }else{
> object report("extension class wrapper for base class ");
> report = report + id.name() + " has not been created yet";
> PyErr_SetObject(PyExc_RuntimeError, report.ptr());
> throw_error_already_set();
> }
> }
> return result;
> }
>
>
> Surely there has to be a better way. Does anyone have a
> better solution than this?
>

SWIG at least doesn't provide any way to go from C++ RTTI information to 
a Python type object, and that leads me to guess that there probably 
isn't a way to do it with SIP either (though I really don't know that 
much about SIP).

I agree that your solution above is far from ideal, but I have a hard 
time imagining anything that's clearly better.

> This was also followed by metaclass and layout conflicts,
> but that's another issue I think.
>

It is, but I suspect it's going to be even more difficult to work around 
in this case, especially layouts.

> To give a little background I was trying to wrap a lib that
> inherits from Qt and I really would prefer to use the
> bindings from PyQt rather than re-wrap ( which also seems
> more difficult than I thought it would be ).

I think your best bet here is definitely going to be trying to use all 
SIP or all Boost.Python, at least for classes within one hierarchy.  You 
might be able to write Boost.Python converters for SIP classes so you 
can pass them to and return them from Boost.Python-wrapped functions, 
but I think it's going to be very difficult (probably impossible) to 
have a Boost.Python class inherit from a SIP-wrapped class.


Good luck!

Jim


More information about the Cplusplus-sig mailing list