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

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Jul 29 23:46:51 CEST 2005


Author: cfbolz
Date: Fri Jul 29 23:46:49 2005
New Revision: 15388

Modified:
   pypy/dist/pypy/rpython/rrange.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
(pedronis, arigo, cfbolz):
added implicit IndexError handling (plus test) to strs.
raising a TyperError for range for a situation that basically cannot
happen at the moment.



Modified: pypy/dist/pypy/rpython/rrange.py
==============================================================================
--- pypy/dist/pypy/rpython/rrange.py	(original)
+++ pypy/dist/pypy/rpython/rrange.py	Fri Jul 29 23:46:49 2005
@@ -34,6 +34,9 @@
 class __extend__(pairtype(RangeRepr, IntegerRepr)):
 
     def rtype_getitem((r_rng, r_int), hop):
+        if hop.has_implicit_exception(IndexError):
+            s = "getitem on range with try, except: block not supported."
+            raise TyperError, s
         v_lst, v_index = hop.inputargs(r_rng, Signed)
         cstep = hop.inputconst(Signed, r_rng.step)
         if hop.args_s[1].nonneg:

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Fri Jul 29 23:46:49 2005
@@ -179,10 +179,16 @@
 class __extend__(pairtype(StringRepr, IntegerRepr)):
     def rtype_getitem(_, hop):
         v_str, v_index = hop.inputargs(string_repr, Signed)
-        if hop.args_s[1].nonneg:
-            llfn = ll_stritem_nonneg
+        if hop.has_implicit_exception(IndexError):
+            if hop.args_s[1].nonneg:
+                llfn = ll_stritem_nonneg_checked
+            else:
+                llfn = ll_stritem_checked
         else:
-            llfn = ll_stritem
+            if hop.args_s[1].nonneg:
+                llfn = ll_stritem_nonneg
+            else:
+                llfn = ll_stritem
         return hop.gendirectcall(llfn, v_str, v_index)
 
     def rtype_mod(_, hop):
@@ -499,11 +505,23 @@
 def ll_stritem_nonneg(s, i):
     return s.chars[i]
 
+def ll_stritem_nonneg_checked(s, i):
+    if i >= len(s.chars):
+        raise IndexError
+    return s.chars[i]
+
 def ll_stritem(s, i):
     if i < 0:
         i += len(s.chars)
     return s.chars[i]
 
+def ll_stritem_checked(s, i):
+    if i < 0:
+        i += len(s.chars)
+    if i >= len(s.chars) or i < 0:
+        raise IndexError
+    return s.chars[i]
+
 def ll_str_is_true(s):
     # check if a string is True, allowing for None
     return bool(s) and len(s.chars) != 0

Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Fri Jul 29 23:46:49 2005
@@ -13,6 +13,23 @@
         res = interpret(fn, [i])
         assert res == 'hello'[i]
 
+def test_implicit_index_error():
+    def fn(i):
+        s = 'hello'
+        try:
+            return s[i]
+        except IndexError:
+            return '*'
+    for i in range(-5, 5):
+        res = interpret(fn, [i])
+        assert res == 'hello'[i]
+    res = interpret(fn, [5])
+    assert res == '*'
+    res = interpret(fn, [6])
+    assert res == '*'
+    res = interpret(fn, [-42])
+    assert res == '*'
+    
 
 def test_nonzero():
     def fn(i, j):



More information about the Pypy-commit mailing list