[PEAK] dispatch_by_mro bug

Radek Kanovsky rk at dat.cz
Sun Nov 21 12:25:03 EST 2004


Hi all,

there is probably bug in dispatch_by_mro function. Try the following
example:

    from peak.core import *

    class Classic : pass

    [dispatch.generic()]
    def typeDescriptor (arg) :
        pass

    [typeDescriptor.when("arg in Classic")]
    def typeDescriptorForClassic(arg) :
        return "Classic"

    print typeDescriptor(Classic())
    print typeDescriptor(Classic)

The last print statement raises AttributeError instead of
NoApplicableMethod (is it intended behaviour?). When using
single-dispatch approach, the bug doesn't appear because of other
dispatching mechanism. I hope that the patch will correct the bug. Old
python classes (and maybe some C extension types) have no __class__
attribute, so we should try 'type(ob)' when 'ob.__class__' failed.
But 'ob.__class__' should be used first because 'type(Classic())' is
'types.InstanceType' not 'Classic'.


--- strategy.py (revision 25)
+++ strategy.py (working copy)
@@ -125,7 +125,10 @@
 
     """Lookup '__class__' of 'ob' in 'table' using its MRO order"""
 
-    klass = ob.__class__
+    try :
+        klass = ob.__class__
+    except AttributeError :
+        klass = type(ob)
 
     while True:
         if klass in table:



RadekK



More information about the PEAK mailing list