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

arigo at codespeak.net arigo at codespeak.net
Tue Jul 12 22:14:03 CEST 2005


Author: arigo
Date: Tue Jul 12 22:14:01 2005
New Revision: 14573

Modified:
   pypy/dist/pypy/rpython/rclass.py
   pypy/dist/pypy/rpython/test/test_rclass.py
Log:
Implement is_ for two instances (which can have a slightly different
annotation).



Modified: pypy/dist/pypy/rpython/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/rclass.py	(original)
+++ pypy/dist/pypy/rpython/rclass.py	Tue Jul 12 22:14:01 2005
@@ -1,5 +1,5 @@
 import types
-from pypy.annotation.pairtype import pairtype
+from pypy.annotation.pairtype import pairtype, pair
 from pypy.annotation import model as annmodel
 from pypy.annotation.classdef import isclassdef
 from pypy.rpython.rmodel import Repr, TyperError, inputconst, warning
@@ -591,6 +591,14 @@
         else:
             return NotImplemented
 
+    def rtype_is_((r_ins1, r_ins2), hop):
+        if r_ins1.classdef is None or r_ins2.classdef is None:
+            basedef = None
+        else:
+            basedef = r_ins1.classdef.commonbase(r_ins2.classdef)
+        r_ins = getinstancerepr(r_ins1.rtyper, basedef)
+        return pairtype(Repr, Repr).rtype_is_(pair(r_ins, r_ins), hop)
+
 # ____________________________________________________________
 
 def rtype_new_instance(rtyper, cls, llops):

Modified: pypy/dist/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rclass.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rclass.py	Tue Jul 12 22:14:01 2005
@@ -177,3 +177,31 @@
     res = interpret(f, [])
     assert res == 42
 
+def test_is():
+    class A: pass
+    class B(A): pass
+    class C: pass
+    def f(i):
+        a = A()
+        b = B()
+        c = C()
+        d = None
+        e = None
+        if i == 0:
+            d = a
+        elif i == 1:
+            d = b
+        elif i == 2:
+            e = c
+        return (0x0001*(a is b) | 0x0002*(a is c) | 0x0004*(a is d) |
+                0x0008*(a is e) | 0x0010*(b is c) | 0x0020*(b is d) |
+                0x0040*(b is e) | 0x0080*(c is d) | 0x0100*(c is e) |
+                0x0200*(d is e))
+    res = interpret(f, [0])
+    assert res == 0x0004
+    res = interpret(f, [1])
+    assert res == 0x0020
+    res = interpret(f, [2])
+    assert res == 0x0100
+    res = interpret(f, [3])
+    assert res == 0x0200



More information about the Pypy-commit mailing list