Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: var-in-Some
Changeset: r71569:1a302091ddcf
Date: 2014-05-18 20:12 +0100
http://bitbucket.org/pypy/pypy/changeset/1a302091ddcf/

Log:    create pairmro()

diff --git a/rpython/tool/pairtype.py b/rpython/tool/pairtype.py
--- a/rpython/tool/pairtype.py
+++ b/rpython/tool/pairtype.py
@@ -61,3 +61,13 @@
         bases = tuple(bases1 + bases2) or (tuple,)  # 'tuple': ultimate base
         pair = pairtypecache[cls1, cls2] = extendabletype(name, bases, {})
     return pair
+
+def pairmro(cls1, cls2):
+    """
+    Return the resolution order on pairs of types for double dispatch.
+
+    This order is compatible with the mro of pairtype(cls1, cls2).
+    """
+    for base2 in cls2.__mro__:
+        for base1 in cls1.__mro__:
+            yield (base1, base2)
diff --git a/rpython/tool/test/test_pairtype.py 
b/rpython/tool/test/test_pairtype.py
--- a/rpython/tool/test/test_pairtype.py
+++ b/rpython/tool/test/test_pairtype.py
@@ -1,7 +1,6 @@
+from rpython.tool.pairtype import pairtype, pair, extendabletype, pairmro
 
-from rpython.tool.pairtype import pairtype, pair, extendabletype
-
-def test_binop(): 
+def test_binop():
     ### Binary operation example
     class __extend__(pairtype(int, int)):
         def add((x, y)):
@@ -13,16 +12,16 @@
         def add((x, y)):
             return 'bool: %s+%s' % (x, y)
 
-    assert pair(3,4).add() == 'integer: 3+4'
-    assert pair(3,4).sub() == 'integer: 3-4'
-    assert pair(3,True).add() == 'integer: 3+True'
-    assert pair(3,True).sub() == 'integer: 3-True'
-    assert pair(False,4).add() == 'integer: False+4'
-    assert pair(False,4).sub() == 'integer: False-4'
-    assert pair(False,True).add() == 'bool: False+True'
-    assert pair(False,True).sub() == 'integer: False-True'
+    assert pair(3, 4).add() == 'integer: 3+4'
+    assert pair(3, 4).sub() == 'integer: 3-4'
+    assert pair(3, True).add() == 'integer: 3+True'
+    assert pair(3, True).sub() == 'integer: 3-True'
+    assert pair(False, 4).add() == 'integer: False+4'
+    assert pair(False, 4).sub() == 'integer: False-4'
+    assert pair(False, True).add() == 'bool: False+True'
+    assert pair(False, True).sub() == 'integer: False-True'
 
-def test_somebuiltin(): 
+def test_somebuiltin():
     ### Operation on built-in types
     class MiniPickler:
         def __init__(self):
@@ -48,7 +47,7 @@
     pair(p, [1, 2, ['hello', 3]]).write()
     assert p.data == ['I1', 'I2', 'Shello', 'I3', 'L2', 'L3']
 
-def test_some_multimethod(): 
+def test_some_multimethod():
     ### Another multimethod example
     class Block:
         def __init__(self, exit):
@@ -57,7 +56,7 @@
         pass
     class Switch:
         pass
-    
+
     class C_Generator:
         def __init__(self):
             self.lines = []
@@ -78,7 +77,7 @@
 
     g = C_Generator()
     pair(g, Block(Switch())).emit(['v1', 'v2'])
-    assert g.lines == ["C code for block", "switch (v5) { ... }"] 
+    assert g.lines == ["C code for block", "switch (v5) { ... }"]
 
     class Lisp_Generator:
         def __init__(self):
@@ -95,16 +94,22 @@
 def test_multiple_extend():
     class A:
         __metaclass__ = extendabletype
+
     class B:
         __metaclass__ = extendabletype
 
-    class __extend__(A,B):
-
+    class __extend__(A, B):
         def f(self):
             pass
 
     assert hasattr(A, 'f')
     assert hasattr(B, 'f')
-    
 
-        
+def test_pairmro():
+    class A(object): pass
+    class A2(A): pass
+    class A3(A2): pass
+    class B(object): pass
+    class B2(B): pass
+    parent_pairtypes = pairtype(A3, B2).__mro__[:-2]
+    assert (tuple(pairtype(a, b) for a, b in pairmro(A3, B2)) == 
parent_pairtypes)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to