[pypy-svn] r7484 - in pypy/trunk/src/pypy/annotation: . test

hpk at codespeak.net hpk at codespeak.net
Sat Nov 20 10:13:56 CET 2004


Author: hpk
Date: Sat Nov 20 10:13:56 2004
New Revision: 7484

Modified:
   pypy/trunk/src/pypy/annotation/model.py
   pypy/trunk/src/pypy/annotation/test/test_model.py
Log:
improve the commonbase() helper for annotation,
it now uses MRO and tries a bit harder to find
the commonbase, assuming 'object' to be the
common base if it can't otherwise find one 

added a few tests for this



Modified: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/model.py	(original)
+++ pypy/trunk/src/pypy/annotation/model.py	Sat Nov 20 10:13:56 2004
@@ -34,6 +34,7 @@
 from pypy.annotation.pairtype import pair, extendabletype
 from pypy.objspace.flow.model import Constant
 from pypy.tool.cache import Cache 
+import inspect
 
 class SomeObject:
     """The set of all objects.  Each instance stands
@@ -315,10 +316,16 @@
     return d
 
 def commonbase(cls1, cls2):   # XXX single inheritance only  XXX hum
-    assert issubclass(cls1, object)
-    while not issubclass(cls1, cls2):
-        cls2, = [x for x in cls2.__bases__ if x is not object] or [object]
-    return cls2
+    l1 = inspect.getmro(cls1) 
+    l2 = inspect.getmro(cls2) 
+    if l1[-1] != object: 
+        l1 = l1 + (object,) 
+    if l2[-1] != object: 
+        l2 = l2 + (object,) 
+    for x in l1: 
+        if x in l2: 
+            return x 
+    assert 0, "couldn't get to commonbase of %r and %r" % (cls1, cls2))
 
 def missing_operation(cls, name):
     def default_op(*args):

Modified: pypy/trunk/src/pypy/annotation/test/test_model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/test/test_model.py	(original)
+++ pypy/trunk/src/pypy/annotation/test/test_model.py	Sat Nov 20 10:13:56 2004
@@ -38,6 +38,24 @@
              s1, s1, s1, s5, s5, s5,
              s1, s2, s3, s4, s5, s6])
 
+def test_commonbase_simple():
+    class A0: 
+        pass
+    class A1(A0): 
+        pass
+    class A2(A0): 
+        pass
+    class B1(object):
+        pass
+    class B2(object):
+        pass
+    class B3(object, A0):
+        pass
+    assert commonbase(A1,A2) is A0 
+    assert commonbase(A1,A0) is A0
+    assert commonbase(A1,A1) is A1
+    assert commonbase(A2,B2) is object 
+    assert commonbase(A2,B3) is A0 
 
 if __name__ == '__main__':
     for name, value in globals().items():



More information about the Pypy-commit mailing list