[pypy-svn] r48525 - in pypy/branch/unicode-objspace/pypy/annotation: . test

fijal at codespeak.net fijal at codespeak.net
Sat Nov 10 19:45:34 CET 2007


Author: fijal
Date: Sat Nov 10 19:45:32 2007
New Revision: 48525

Modified:
   pypy/branch/unicode-objspace/pypy/annotation/binaryop.py
   pypy/branch/unicode-objspace/pypy/annotation/test/test_annrpython.py
Log:
Support for reporting negative slices early, as it sounds unlikely that support
will happen soon.


Modified: pypy/branch/unicode-objspace/pypy/annotation/binaryop.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/annotation/binaryop.py	(original)
+++ pypy/branch/unicode-objspace/pypy/annotation/binaryop.py	Sat Nov 10 19:45:32 2007
@@ -599,19 +599,28 @@
         lst1.listdef.resize()
     delitem.can_only_throw = [IndexError]
 
+def check_negative_slice(s_slice):
+    if isinstance(s_slice.start, SomeInteger) and not s_slice.start.nonneg:
+        raise TypeError("%s not proven to have negative start" % s_slice)
+    if isinstance(s_slice.stop, SomeInteger) and not s_slice.stop.nonneg:
+        raise TypeError("%s not proven to have negative stop" % s_slice)
+
 class __extend__(pairtype(SomeList, SomeSlice)):
 
     def getitem((lst, slic)):
+        check_negative_slice(slic)
         return lst.listdef.offspring()
     getitem.can_only_throw = []
 
     def setitem((lst, slic), s_iterable):
+        check_negative_slice(slic)
         # we need the same unifying effect as the extend() method for
         # the case lst1[x:y] = lst2.
         lst.method_extend(s_iterable)
     setitem.can_only_throw = []
 
     def delitem((lst1, slic)):
+        check_negative_slice(slic)
         lst1.listdef.resize()
     delitem.can_only_throw = []
 
@@ -619,6 +628,7 @@
                  pairtype(SomeUnicodeString, SomeSlice)):
 
     def getitem((str1, slic)):
+        check_negative_slice(slic)
         return str1.basestringclass()
     getitem.can_only_throw = []
 

Modified: pypy/branch/unicode-objspace/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/annotation/test/test_annrpython.py	(original)
+++ pypy/branch/unicode-objspace/pypy/annotation/test/test_annrpython.py	Sat Nov 10 19:45:32 2007
@@ -2962,6 +2962,15 @@
         s = a.build_types(f, [unicode, str])
         assert isinstance(s, annmodel.SomeUnicodeCodePoint)
 
+    def test_negative_slice(self):
+        def f(s, e):
+            return [1, 2, 3][s:e]
+
+        a = self.RPythonAnnotator()
+        py.test.raises(TypeError, "a.build_types(f, [int, int])")
+        a.build_types(f, [annmodel.SomeInteger(nonneg=True),
+                          annmodel.SomeInteger(nonneg=True)])
+
 def g(n):
     return [0,1,2,n]
 



More information about the Pypy-commit mailing list