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

tismer at codespeak.net tismer at codespeak.net
Thu Jun 23 20:03:23 CEST 2005


Author: tismer
Date: Thu Jun 23 20:03:22 2005
New Revision: 13736

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/rtyper.py
   pypy/dist/pypy/rpython/test/test_rlist.py
Log:
implemented __contains__ and index
augmented rtyper to support __contains__

Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Thu Jun 23 20:03:22 2005
@@ -94,6 +94,10 @@
         v_lst, v_value = hop.inputargs(self, self.item_repr)
         hop.gendirectcall(ll_append, v_lst, v_value)
 
+    def rtype_method_index(self, hop):
+        v_lst, v_value = hop.inputargs(self, self.item_repr)
+        return hop.gendirectcall(ll_listindex, v_lst, v_value, self.get_eqfunc())
+
     def rtype_method_insert(self, hop):
         v_lst, v_index, v_value = hop.inputargs(self, Signed, self.item_repr)
         arg1 = hop.args_s[1]
@@ -136,6 +140,13 @@
         return ListIteratorRepr(self)
 
 
+class __extend__(pairtype(ListRepr, Repr)):
+
+    def rtype_contains((r_lst, _), hop):
+        v_lst, v_any = hop.inputargs(r_lst, r_lst.item_repr)
+        return hop.gendirectcall(ll_listcontains, v_lst, v_any, r_lst.get_eqfunc())
+
+
 class __extend__(pairtype(ListRepr, IntegerRepr)):
 
     def rtype_getitem((r_lst, r_int), hop):
@@ -441,6 +452,33 @@
         j += 1
     return True
 
+def ll_listcontains(lst, obj, eqfn):
+    items = lst.items
+    lng = len(items)
+    j = 0
+    while j < lng:
+        if eqfn is None:
+            if items[j] == obj:
+                return True
+        else:
+            if eqfn(items[j], obj):
+                return True
+        j += 1
+    return False
+
+def ll_listindex(lst, obj, eqfn):
+    items = lst.items
+    lng = len(items)
+    j = 0
+    while j < lng:
+        if eqfn is None:
+            if items[j] == obj:
+                return j
+        else:
+            if eqfn(items[j], obj):
+                return j
+        j += 1
+    raise ValueError # can't say 'list.index(x): x not in list'
 
 # ____________________________________________________________
 #

Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Thu Jun 23 20:03:22 2005
@@ -324,6 +324,12 @@
     _registeroperations(locals())
     del _registeroperations
 
+    # this one is not in BINARY_OPERATIONS
+    def translate_op_contains(self, hop):
+        r_arg1 = hop.args_r[0]
+        r_arg2 = hop.args_r[1]
+        return pair(r_arg1, r_arg2).rtype_contains(hop)
+
     # __________ irregular operations __________
 
     def translate_op_newlist(self, hop):

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	Thu Jun 23 20:03:22 2005
@@ -5,6 +5,7 @@
 from pypy.rpython.rslice import ll_newslice
 from pypy.rpython.rint import signed_repr
 from pypy.rpython.test.test_llinterp import interpret, make_interpreter
+from pypy.rpython.test.test_llinterp import find_exception
 
 
 def sample_list():
@@ -269,3 +270,40 @@
             for case in False, True:
                 res = ev_fn(i, j, case)
                 assert res is fn(i, j, case)
+
+def test_list_contains():
+    def fn(i, neg=False):
+        foo1 = Foo()
+        foo2 = Foo()
+        bar1 = Bar()
+        bar2 = Bar()
+        lis = [foo1, foo2, bar1]
+        args = lis + [bar2]
+        if neg : return args[i] not in lis
+        return args[i] in lis
+    ev_fn = make_interpreter(fn, [0, False])
+    for i in range(4):
+        for case in False, True:
+            res = ev_fn(i, case)
+            assert res is fn(i, case)
+
+def test_list_index():
+    def fn(i):
+        foo1 = Foo()
+        foo2 = Foo()
+        bar1 = Bar()
+        bar2 = Bar()
+        lis = [foo1, foo2, bar1]
+        args = lis + [bar2]
+        return lis.index(args[i])
+    ev_fn = make_interpreter(fn, [0])
+    for i in range(4):
+        try:
+            res = ev_fn(i)
+        except Exception, e:
+            res = find_exception(e)
+        try:
+            res2 = fn(i)
+        except Exception, e:
+            res2 = e.__class__
+        assert res == res2



More information about the Pypy-commit mailing list