[pypy-svn] r59083 - in pypy/trunk/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Tue Oct 14 13:31:19 CEST 2008


Author: arigo
Date: Tue Oct 14 13:31:17 2008
New Revision: 59083

Modified:
   pypy/trunk/pypy/objspace/std/sliceobject.py
   pypy/trunk/pypy/objspace/std/test/test_sliceobject.py
Log:
(antocuni, arigo)
Precise test, and fix, for normalize_simple_slice().
This should fix failing llassert's in pypy-c :-/


Modified: pypy/trunk/pypy/objspace/std/sliceobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/sliceobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/sliceobject.py	Tue Oct 14 13:31:17 2008
@@ -81,12 +81,17 @@
 
 def normalize_simple_slice(space, length, w_start, w_stop):
     """Helper for the {get,set,del}slice multimethod implementations."""
+    # this returns a pair (start, stop) which is usable for slicing
+    # a sequence of the given length in an RPython-friendly way, i.e.
+    # guaranteeing that:
+    #   * 0 <= start <= length
+    #   * start <= stop
     start = space.int_w(w_start)
     stop = space.int_w(w_stop)
     if start < 0:
         start = 0
-    if stop > length:
-        stop = length
+    if start > length:
+        start = length
     if stop < start:
         stop = start
     return start, stop

Modified: pypy/trunk/pypy/objspace/std/test/test_sliceobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_sliceobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_sliceobject.py	Tue Oct 14 13:31:17 2008
@@ -1,3 +1,4 @@
+from pypy.objspace.std.sliceobject import normalize_simple_slice
 
 
 class TestW_SliceObject:
@@ -20,6 +21,27 @@
         w_slice = space.newslice(w_None, w_None, w(0))
         self.space.raises_w(space.w_ValueError, w_slice.indices3, space, 10)
 
+    def test_normalize_simple_slice(self):
+        space = self.space
+        w = space.wrap
+
+        def getslice(length, start, stop):
+            # returns range(length)[start:stop] but without special
+            # support for negative start or stop
+            return [i for i in range(length) if start <= i < stop]
+
+        assert getslice(10, 2, 5) == [2, 3, 4]
+
+        for length in range(5):
+            for start in range(-2*length-2, 2*length+3):
+                for stop in range(-2*length-2, 2*length+3):
+                    mystart, mystop = normalize_simple_slice(space, length,
+                                                             w(start), w(stop))
+                    assert 0 <= mystart <= length
+                    assert mystart <= mystop
+                    assert (getslice(length, start, stop) ==
+                            getslice(length, mystart, mystop))
+
 
 class AppTest_SliceObject:
     def test_new(self):



More information about the Pypy-commit mailing list