Wrapping classes that inherit from classes already wrapped

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrapping classes that inherit from classes already wrapped

Alex Olivas
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?

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

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 ).
Thanks,
Alex.

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Wrapping classes that inherit from classes already wrapped

Jim Bosch-2
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
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig