Author Martin.Teichmann
Recipients Martin.Teichmann, eric.snow
Date 2014-02-08.14:41:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1391870496.87.0.77192372443.issue20518@psf.upfronthosting.co.za>
In-reply-to
Content
I've been working a bit on a solution to this issue, one proposal
is in the attached patch. The patch adds a new flag to tp_flags,
called Py_TPFLAGS_SOLID, which marks a class as being solid, i.e.
its memory layout is incompatible with its parent layout. C classes
can then set this flag, and cpython will assure that no incompatible
classes are in the same inheritance graph.

Other solutions are certainly thinkable: Eric proposed one should
use the MRO instead of __base__. This is a great idea, why is 
cpython itself not doing it? Instead of traversing the inheritance
graph trying to find the solid base, one could simply iterate over
the MRO.

In order to illustrate where the actual problem lies, here another
example:

=========================================
rom PyQt4.QtCore import QObject, QTimer, QTimerEvent
# QObject is the parent of QTimer

class Mixin(QObject):
    # this overwrites QObject.timerEvent
    def timerEvent(self, e):
        print('mixed in')
        super().timerEvent(e)

class B(Mixin, QTimer):
    pass

class C(QTimer, Mixin):
    pass

event = QTimerEvent(0)

b = B()
try:
    b.timerEvent(event)
except Exception as e:
    print(e)
print('---------')
c = C()
c.timerEvent(event)
=========================================

I'm writing a mixin class, that overwrites a method from QObject.
In the end I am calling this method (normally that's done from
within PyQt4). For class B, this mixing in works properly, my
code is executed, but then I am getting an exception: PyQt4 had
made b and instance of QObject, not QTimer, since this is where
the __base__ is pointing to. For class C, my code is never called
because PyQt4 is not cooperating. Sure, I could write wrapper
classes for every Qt class that I want to mix into, but what
would be the point of a mixin class then?

I hope I made my problem a bit clearer.
History
Date User Action Args
2014-02-08 14:41:36Martin.Teichmannsetrecipients: + Martin.Teichmann, eric.snow
2014-02-08 14:41:36Martin.Teichmannsetmessageid: <1391870496.87.0.77192372443.issue20518@psf.upfronthosting.co.za>
2014-02-08 14:41:36Martin.Teichmannlinkissue20518 messages
2014-02-08 14:41:36Martin.Teichmanncreate