[pypy-svn] r13629 - in pypy/dist/pypy: rpython rpython/test translator/c translator/c/test

ac at codespeak.net ac at codespeak.net
Mon Jun 20 18:05:44 CEST 2005


Author: ac
Date: Mon Jun 20 18:05:43 2005
New Revision: 13629

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/rmodel.py
   pypy/dist/pypy/rpython/rpbc.py
   pypy/dist/pypy/rpython/test/test_rlist.py
   pypy/dist/pypy/translator/c/funcgen.py
   pypy/dist/pypy/translator/c/test/test_typed.py
Log:
Implement 'is'.

Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Mon Jun 20 18:05:43 2005
@@ -209,6 +209,24 @@
         # well, actually this is what's now in the globals.
         return cast_pointer(tp, obj)
 
+    def op_ptr_eq(self, ptr1, ptr2):
+        assert isinstance(ptr1, _ptr)
+        assert isinstance(ptr2, _ptr)
+        return ptr1 == ptr2
+
+    def op_ptr_ne(self, ptr1, ptr2):
+        assert isinstance(ptr1, _ptr)
+        assert isinstance(ptr2, _ptr)
+        return ptr1 != ptr2
+
+    def op_ptr_nonzero(self, ptr1):
+        assert isinstance(ptr1, _ptr)
+        return bool(ptr1)
+
+    def op_ptr_iszero(self, ptr1):
+        assert isinstance(ptr1, _ptr)
+        return not bool(ptr1)
+
     def op_cast_int_to_float(self, i):
         assert type(i) is int
         return float(i)
@@ -224,6 +242,7 @@
     def op_cast_bool_to_float(self, b):
         assert type(b) is bool
         return float(b)
+    
 # __________________________________________________________
 # primitive operations
 from pypy.objspace.flow.operation import FunctionByName

Modified: pypy/dist/pypy/rpython/rmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/rmodel.py	(original)
+++ pypy/dist/pypy/rpython/rmodel.py	Mon Jun 20 18:05:43 2005
@@ -101,6 +101,23 @@
     def rtyper_makekey(self):
         return None
 
+# ____ generic binary operations _____________________________
+
+
+class __extend__(pairtype(Repr, Repr)):
+    
+    def rtype_is_((robj1, robj2), hop):
+        if (not isinstance(robj1.lowleveltype, Ptr) or
+            not isinstance(robj2.lowleveltype, Ptr)):
+            raise TyperError('is of instances of the non-pointers: %r, %r' % (
+                robj1, robj2))
+        if robj1.lowleveltype != robj2.lowleveltype:
+            raise TyperError('is of instances of different pointer types: %r, %r' % (
+                robj1, robj2))
+            
+        v_list = hop.inputargs(robj1, robj2)
+        return hop.genop('ptr_eq', v_list, resulttype=Bool)
+
 # ____________________________________________________________
 
 class TyperError(Exception):

Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Mon Jun 20 18:05:43 2005
@@ -1,9 +1,9 @@
 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.objspace.flow.model import Constant
-from pypy.rpython.lltype import typeOf, Void, ForwardReference, Struct
+from pypy.rpython.lltype import typeOf, Void, ForwardReference, Struct, Bool
 from pypy.rpython.lltype import Ptr, malloc, nullptr
 from pypy.rpython.rmodel import Repr, TyperError
 from pypy.rpython import rclass
@@ -67,6 +67,8 @@
 
 def getFrozenPBCRepr(rtyper, s_pbc):
     if len(s_pbc.prebuiltinstances) <= 1:
+        if s_pbc.const is None:
+            return none_frozen_pbc_repr
         return single_frozen_pbc_repr
     else:
         pbcs = [pbc for pbc in s_pbc.prebuiltinstances.keys()
@@ -95,6 +97,31 @@
 
 single_frozen_pbc_repr = SingleFrozenPBCRepr()
 
+# __ None ____________________________________________________
+class NoneFrozenPBCRepr(SingleFrozenPBCRepr):
+    pass
+
+none_frozen_pbc_repr = NoneFrozenPBCRepr()
+
+
+def rtype_is_None(robj1, rnone2, hop, pos=0):
+        if not isinstance(robj1.lowleveltype, Ptr):
+            raise TyperError('is None of instance of the non-pointer: %r' % (robj1))           
+        v1 = hop.inputarg(robj1, pos)
+        return hop.genop('ptr_iszero', [v1], resulttype=Bool)
+    
+class __extend__(pairtype(Repr, NoneFrozenPBCRepr)):
+    
+    def rtype_is_((robj1, rnone2), hop):
+        return rtype_is_None(robj1, rnone2, hop)
+
+class __extend__(pairtype(NoneFrozenPBCRepr, Repr)):
+
+    def rtype_is_((rnone1, robj2), hop):
+        return rtype_is_None(robj2, rnone1, hop, pos=1)
+        
+
+# ____________________________________________________________
 
 class MultipleFrozenPBCRepr(Repr):
     """Representation selected for multiple non-callable pre-built constants."""

Modified: pypy/dist/pypy/rpython/test/test_rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rlist.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rlist.py	Mon Jun 20 18:05:43 2005
@@ -170,3 +170,34 @@
     def dummyfn(n):
         klist.extend([])
     interpret(dummyfn, [7])
+
+def test_list_is():
+    def dummyfn():
+        l1 = []
+        return l1 is l1
+    res = interpret(dummyfn, [])
+    assert res is True
+    def dummyfn():
+        l2 = [1, 2]
+        return l2 is l2
+    res = interpret(dummyfn, [])
+    assert res is True
+    def dummyfn():
+        l1 = [2]
+        l2 = [1, 2]
+        return l1 is l2
+    res = interpret(dummyfn, [])
+    assert res is False
+    def dummyfn():
+        l1 = [1, 2]
+        l2 = [1, 2]
+        return l1 is l2
+    res = interpret(dummyfn, [])
+    assert res is False
+
+    def dummyfn():
+        l1 = None
+        l2 = [1, 2]
+        return l1 is l2
+    res = interpret(dummyfn, [])
+    assert res is False

Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Mon Jun 20 18:05:43 2005
@@ -438,7 +438,10 @@
     def OP_PTR_NONZERO(self, op, err):
         return '%s = (%s != NULL);' % (self.expr(op.result),
                                        self.expr(op.args[0]))
-
+    def OP_PTR_ISZERO(self, op, err):
+        return '%s = (%s == NULL);' % (self.expr(op.result),
+                                       self.expr(op.args[0]))
+    
     def OP_PTR_EQ(self, op, err):
         return '%s = (%s == %s);' % (self.expr(op.result),
                                      self.expr(op.args[0]),

Modified: pypy/dist/pypy/translator/c/test/test_typed.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_typed.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_typed.py	Mon Jun 20 18:05:43 2005
@@ -64,6 +64,20 @@
                           1, 9,
                           2, 11, 'h')
 
+    def test_is(self):
+        def testfn():
+            l1 = []
+            return l1 is l1
+        fn = self.getcompiled(testfn)
+        result = fn()
+        assert result is True
+        def testfn():
+            l1 = []
+            return l1 is None
+        fn = self.getcompiled(testfn)
+        result = fn()
+        assert result is False
+        
     def test_slice_long(self):
         "the parent's test_slice_long() makes no sense here"
 



More information about the Pypy-commit mailing list