[PyKDE] Are Python extenstions built with SIP and Boost.Python in compatible?

Phil Thompson phil at river-bank.demon.co.uk
Sun Dec 8 03:18:01 GMT 2002

On Sunday 08 December 2002 1:48 am, Paul F. Kunz wrote:
> >>>>> On Sun, 08 Dec 2002 01:29:27 +0000, Phil Thompson
> >>>>> <phil at river-bank.demon.co.uk> said:
> >
> > I don't think it's a known problem because I'd be surprised if
> > anybody had tried it before.
>    Further investigation of my problem shows that SIP is doing
> something strange to Python.   Stepping forward in the trace back of
> my segmentation fault, I find...
> #6  0x0807705e in eval_frame (f=0x811a974) at Python/ceval.c:1784
> (gdb)
> #5  0x08056794 in PyObject_GetAttr (v=0x82cc4cc, name=0x815a8e0)
>     at Objects/object.c:1108
> (gdb)
> #4  0x401afa43 in instanceGetAttr ()
>    from /usr/local/lib/python2.2/site-packages/libsip.so
> Current directory is /usr/local/bin/
> This is caused by the second line in The following Python code that is
> using PyQt...
> wc = WindowController()
> cw = wc.newCanvas()
> The WindowController class is C++ class wrapped with Boost.Python.
> In the second line, it is trying to return another C++ class wrapped
> with Boost.Python that creates two Windows created by C++ classes
> derived from Qt.
> If these classes are using Boost.Python, then why does Python call a
> function in libsip.so on them?   The offending line in
> PyObject_GetAttr function appears to be...
> 	if (tp->tp_getattro != NULL)
> 		return (*tp->tp_getattro)(v, name);
> This returns something that is an error, and when python attempts to
> report the error messge, its thread state is null.

SIP takes over the class and instance getattr functions - it patches the class 
and instance type objects. It does this to implement a "lazy" getattr so that 
the Python object wrapping a C++ method is only created when that method is 
referenced. PyQt contains over 5000 methods - if this technique wasn't used 
then both the startup time of a PyQt application, and the memory it consumed, 
would make it unusable.

> > The obvious workaround is to re-wrap your widgets using SIP.
>    I've already learned on way to expose my C++ to Python, why do I
> need to learn another way.   SIP should not be doing things to Python
> that is incompatible with other Python extension modules.   Or is
> Boost.Python producing something incompatible with other Python
> extension modules?   Or is there something I'm doing wrong?   This is
> really hard to decyipher

The real problem is that SWIG/Boost/SIP have to agree on how they are going to 
represent a pointer to a C++ object - or at least you have to provide code 
that converts between the different representations.

Also, I don't believe (although I'd be happy to be proved wrong) that you can 
produce production quality bindings for Qt derived classes using anything 
other than SIP. For example, how do you handle object ownership issues?


More information about the PyQt mailing list