[pypy-svn] r14640 - in pypy/dist/pypy/rpython: . test

pedronis at codespeak.net pedronis at codespeak.net
Wed Jul 13 20:31:20 CEST 2005


Author: pedronis
Date: Wed Jul 13 20:31:18 2005
New Revision: 14640

Modified:
   pypy/dist/pypy/rpython/rpbc.py
   pypy/dist/pypy/rpython/test/test_rpbc.py
Log:
implemented widening conversions for ClassesPBCRepr with tests.



Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Wed Jul 13 20:31:18 2005
@@ -567,6 +567,17 @@
             return NotImplemented   # good enough for now
         return v
 
+class __extend__(pairtype(ClassesPBCRepr, ClassesPBCRepr)):
+        def convert_from_to((r_clspbc1, r_clspbc2), v, llops):
+            # this check makes sense because both source and dest repr are ClassesPBCRepr
+            if r_clspbc1.lowleveltype == r_clspbc2.lowleveltype:
+                return v
+            if r_clspbc1.lowleveltype == Void:
+                return inputconst(r_clspbc2, r_clspbc1.s_pbc.const)
+            return NotImplemented
+            
+
+
 # ____________________________________________________________
 
 def rtype_call_memo(hop): 

Modified: pypy/dist/pypy/rpython/test/test_rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rpbc.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rpbc.py	Wed Jul 13 20:31:18 2005
@@ -441,3 +441,47 @@
     assert res.super.typeptr.name[0] == 'A'
 
     
+def test_conv_from_classpbcset_to_larger():
+    class A(object): pass
+    class B(A): pass
+    class C(A): pass
+
+    def a():
+        return A
+    def b():
+        return B
+    
+
+    def g(i):
+        if i == 1:
+            cls = a()
+        else:
+            cls = b()
+        return cls()
+
+    res = interpret(g, [0])
+    assert res.super.typeptr.name[0] == 'B'
+    res = interpret(g, [1])
+    assert res.super.typeptr.name[0] == 'A'
+
+    def bc(j):
+        if j == 1:
+            return B
+        else:
+            return C
+
+    def g(i, j):
+        if i == 1:
+            cls = a()
+        else:
+            cls = bc(j)
+        return cls()
+
+    res = interpret(g, [0, 0])
+    assert res.super.typeptr.name[0] == 'C'
+    res = interpret(g, [0, 1])
+    assert res.super.typeptr.name[0] == 'B'    
+    res = interpret(g, [1, 0])
+    assert res.super.typeptr.name[0] == 'A'    
+    
+        



More information about the Pypy-commit mailing list