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

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Jul 29 23:30:20 CEST 2005


Author: cfbolz
Date: Fri Jul 29 23:30:19 2005
New Revision: 15387

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/translator/c/test/test_exception.py
Log:
(arigo, cfbolz, pedronis):
added implicit IndexError handling for lists + test

Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Fri Jul 29 23:30:19 2005
@@ -180,26 +180,44 @@
 
     def rtype_getitem((r_lst, r_int), hop):
         v_lst, v_index = hop.inputargs(r_lst, Signed)
-        if hop.args_s[1].nonneg:
-            llfn = ll_getitem_nonneg
+        if hop.has_implicit_exception(IndexError):
+            if hop.args_s[1].nonneg:
+                llfn = ll_getitem_nonneg_checked
+            else:
+                llfn = ll_getitem_checked
         else:
-            llfn = ll_getitem
+            if hop.args_s[1].nonneg:
+                llfn = ll_getitem_nonneg
+            else:
+                llfn = ll_getitem
         return hop.gendirectcall(llfn, v_lst, v_index)
     
     def rtype_setitem((r_lst, r_int), hop):
         v_lst, v_index, v_item = hop.inputargs(r_lst, Signed, r_lst.item_repr)
-        if hop.args_s[1].nonneg:
-            llfn = ll_setitem_nonneg
+        if hop.has_implicit_exception(IndexError):
+            if hop.args_s[1].nonneg:
+                llfn = ll_setitem_nonneg_checked
+            else:
+                llfn = ll_setitem_checked
         else:
-            llfn = ll_setitem
+            if hop.args_s[1].nonneg:
+                llfn = ll_setitem_nonneg
+            else:
+                llfn = ll_setitem
         return hop.gendirectcall(llfn, v_lst, v_index, v_item)
 
     def rtype_delitem((r_lst, r_int), hop):
         v_lst, v_index = hop.inputargs(r_lst, Signed)
-        if hop.args_s[1].nonneg:
-            llfn = ll_delitem_nonneg
+        if hop.has_implicit_exception(IndexError):
+            if hop.args_s[1].nonneg:
+                llfn = ll_delitem_nonneg_checked
+            else:
+                llfn = ll_delitem_checked
         else:
-            llfn = ll_delitem
+            if hop.args_s[1].nonneg:
+                llfn = ll_delitem_nonneg
+            else:
+                llfn = ll_delitem
         return hop.gendirectcall(llfn, v_lst, v_index)
 
     def rtype_mul((r_lst, r_int), hop):
@@ -386,14 +404,46 @@
         i += len(l.items)
     return l.items[i]
 
+def ll_getitem_nonneg_checked(l, i):
+    if i >= len(l.items):
+        raise IndexError
+    else:
+        return l.items[i]
+
+def ll_getitem_checked(l, i):
+    if i < 0:
+        i += len(l.items)
+    if i >= len(l.items) or i < 0:
+        raise IndexError
+    else:
+        return l.items[i]
+
 def ll_setitem_nonneg(l, i, newitem):
     l.items[i] = newitem
 
+def ll_setitem_nonneg_checked(l, i, newitem):
+    if i >= len(l.items):
+        raise IndexError
+    l.items[i] = newitem
+
 def ll_setitem(l, i, newitem):
     if i < 0:
         i += len(l.items)
     l.items[i] = newitem
 
+def ll_setitem_checked(l, i, newitem):
+    if i < 0:
+        i += len(l.items)
+    if i >= len(l.items) or i < 0:
+        raise IndexError
+    else:
+        l.items[i] = newitem
+
+def ll_delitem_nonneg_checked(l, i):
+    if i >= len(l.items):
+        raise IndexError
+    ll_delitem_nonneg(l, i)
+
 def ll_delitem_nonneg(l, i):
     newlength = len(l.items) - 1
     newitems = malloc(typeOf(l).TO.items.TO, newlength)
@@ -411,6 +461,13 @@
         i += len(l.items)
     ll_delitem_nonneg(l, i)
 
+def ll_delitem_checked(l, i):
+    if i < 0:
+        i += len(l.items)
+    if i >= len(l.items) or i < 0:
+        raise IndexErrror
+    ll_delitem_nonneg(l, i)
+
 def ll_concat(l1, l2):
     len1 = len(l1.items)
     len2 = len(l2.items)

Modified: pypy/dist/pypy/translator/c/test/test_exception.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_exception.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_exception.py	Fri Jul 29 23:30:19 2005
@@ -38,15 +38,13 @@
     assert f(1) == fn(1)
     assert f(2) == fn(2)
 
-def test_simple2(): #taken from ../../llvm2/test/test_exception.py 
-    py.test.skip("decided whethe we want to support IndexError on [] at interp-level")
+def test_implicit_index_error_lists():
     def fn(n):
-        lst = range(10)
+        lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
         try:
-            lst[n]
+            return lst[n]
         except:
             return 2
-        return 4
     t = Translator(fn)
     t.annotate([int]).simplify()
     t.specialize()



More information about the Pypy-commit mailing list