[PyKDE] Bug with super and QObject

Phil Thompson phil at riverbankcomputing.co.uk
Tue Apr 12 17:23:48 BST 2005

> Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
>> It's to do with how SIP implements lazy methods (to save on memory and
>> startup time). super seems to be bypassing the hook (maybe it uses
>> tp_getattro rather than tp_getattr). Should be fixable - I'll have a
>> look.
> Thanks.
> Can you elaborate on this "lazy methods" thing which SIP does? Is it
> documented anywhere? I found also some references to it some days ago,
> reading up an old thread in a mailing list that was comparing Boost.Python
> with SIP.

It's not documented as it is an internal implementation detail. When you
implement a new Python type in C/C++ the methods are defined in a table
that links the method name with the corresponding C/C++ implementation.

When Python initialises the type, it creates a wrapper object for each
method and sticks it in the type's dictionary keyed on the method name
(another Python object). This doesn't scale well (at least it was an issue
in 1998 when PyQt was first released). For something the size of PyQt it
causes slow startup times and large memory usage - and 90% of the methods
won't be used by a particular application.

SIP generates the method tables, but doesn't tell Python about them. In
does install a hook in the type's tp_getattr slot. When this hook is
called it first executes the old slot function. If that returns an
AttributeError it then searches it's private method tables. If the method
isn't found it propogate's the AttributeError. If it is found it creates
the correct Python object and returns it. It will also try and stash the
object in the correct dictionary so that next time the old slot function
will find it straight away.

Enums and class variables are handled in the same way. There is also
special handling so that dir() works as expected.


More information about the PyQt mailing list