[pypy-svn] r17620 - in pypy/dist/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sat Sep 17 18:28:36 CEST 2005


Author: arigo
Date: Sat Sep 17 18:28:32 2005
New Revision: 17620

Modified:
   pypy/dist/pypy/objspace/std/listobject.py
   pypy/dist/pypy/objspace/std/sliceobject.py
   pypy/dist/pypy/objspace/std/slicetype.py
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/objspace/std/test/test_sliceobject.py
   pypy/dist/pypy/objspace/std/tupleobject.py
   pypy/dist/pypy/objspace/std/unicodeobject.py
Log:
Moved slice indices as methods of W_SliceObject, now that they don't belong to
slicetype.py any more (they read attributes of W_SliceObjects).


Modified: pypy/dist/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/listobject.py	(original)
+++ pypy/dist/pypy/objspace/std/listobject.py	Sat Sep 17 18:28:32 2005
@@ -51,7 +51,7 @@
 def getitem__List_Slice(space, w_list, w_slice):
     # XXX consider to extend rlist's functionality?
     length = len(w_list.wrappeditems)
-    start, stop, step, slicelength = slicetype.indices4(space, w_slice, length)
+    start, stop, step, slicelength = w_slice.indices4(length)
     assert slicelength >= 0
     if step == 1 and stop >= start >= 0:
         assert stop >= 0
@@ -168,8 +168,7 @@
     return space.w_None
 
 def delitem__List_Slice(space, w_list, w_slice):
-    start, stop, step, slicelength = slicetype.indices4(space, w_slice,
-                                                        len(w_list.wrappeditems))
+    start, stop, step, slicelength = w_slice.indices4(len(w_list.wrappeditems))
 
     if slicelength==0:
         return
@@ -237,8 +236,7 @@
 
 def _setitem_slice_helper(space, w_list, w_slice, sequence2, len2):
     oldsize = len(w_list.wrappeditems)
-    start, stop, step, slicelength = slicetype.indices4(space, w_slice,
-                                                        oldsize)
+    start, stop, step, slicelength = w_slice.indices4(oldsize)
     assert slicelength >= 0
     items = w_list.wrappeditems
 

Modified: pypy/dist/pypy/objspace/std/sliceobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/sliceobject.py	(original)
+++ pypy/dist/pypy/objspace/std/sliceobject.py	Sat Sep 17 18:28:32 2005
@@ -7,6 +7,7 @@
 
 from pypy.objspace.std.objspace import *
 from pypy.interpreter import gateway
+from pypy.objspace.std.slicetype import _Eval_SliceIndex
 
 
 class W_SliceObject(W_Object):
@@ -25,8 +26,62 @@
         space = w_slice.space
         return slice(space.unwrap(w_slice.w_start), space.unwrap(w_slice.w_stop), space.unwrap(w_slice.w_step))
 
+    def indices3(w_slice, length):
+        space = w_slice.space
+        if space.is_true(space.is_(w_slice.w_step, space.w_None)):
+            step = 1
+        else:
+            step = _Eval_SliceIndex(space, w_slice.w_step)
+            if step == 0:
+                raise OperationError(space.w_ValueError,
+                                     space.wrap("slice step cannot be zero"))
+        if space.is_true(space.is_(w_slice.w_start, space.w_None)):
+            if step < 0:
+                start = length - 1
+            else:
+                start = 0
+        else:
+            start = _Eval_SliceIndex(space, w_slice.w_start)
+            if start < 0:
+                start += length
+                if start < 0:
+                    if step < 0:
+                        start = -1
+                    else:
+                        start = 0
+            elif start >= length:
+                if step < 0:
+                    start = length - 1
+                else:
+                    start = length
+        if space.is_true(space.is_(w_slice.w_stop, space.w_None)):
+            if step < 0:
+                stop = -1
+            else:
+                stop = length
+        else:
+            stop = _Eval_SliceIndex(space, w_slice.w_stop)
+            if stop < 0:
+                stop += length
+                if stop < 0:
+                    stop =-1
+            elif stop > length:
+                stop = length
+        return start, stop, step
+
+    def indices4(w_slice, length):
+        start, stop, step = w_slice.indices3(length)
+        if (step < 0 and stop >= start) or (step > 0 and start >= stop):
+            slicelength = 0
+        elif step < 0:
+            slicelength = (stop - start + 1) / step + 1
+        else:
+            slicelength = (stop - start - 1) / step + 1
+        return start, stop, step, slicelength
+
 registerimplementation(W_SliceObject)
 
+
 repr__Slice = gateway.applevel("""
     def repr__Slice(aslice):
         return 'slice(%r, %r, %r)' % (aslice.start, aslice.stop, aslice.step)
@@ -62,13 +117,12 @@
                          space.wrap("unhashable type"))
 # indices impl
 
-from pypy.objspace.std import slicetype
-
 def slice_indices__Slice_ANY(space, w_slice, w_length):
     length = space.int_w(w_length)
-    start, stop, step = slicetype.indices3(space, w_slice, length)
+    start, stop, step = w_slice.indices3(length)
     return space.newtuple([space.wrap(start), space.wrap(stop),
                            space.wrap(step)])
 
 # register all methods
+from pypy.objspace.std import slicetype
 register_all(vars(), slicetype)

Modified: pypy/dist/pypy/objspace/std/slicetype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/slicetype.py	(original)
+++ pypy/dist/pypy/objspace/std/slicetype.py	Sat Sep 17 18:28:32 2005
@@ -21,58 +21,6 @@
             x = -sys.maxint
     return x
 
-def indices3(space, w_slice, length):
-    if space.is_true(space.is_(w_slice.w_step, space.w_None)):
-        step = 1
-    else:
-        step = _Eval_SliceIndex(space, w_slice.w_step)
-        if step == 0:
-            raise OperationError(space.w_ValueError,
-                                 space.wrap("slice step cannot be zero"))
-    if space.is_true(space.is_(w_slice.w_start, space.w_None)):
-        if step < 0:
-            start = length - 1
-        else:
-            start = 0
-    else:
-        start = _Eval_SliceIndex(space, w_slice.w_start)
-        if start < 0:
-            start += length
-            if start < 0:
-                if step < 0:
-                    start = -1
-                else:
-                    start = 0
-        elif start >= length:
-            if step < 0:
-                start = length - 1
-            else:
-                start = length
-    if space.is_true(space.is_(w_slice.w_stop, space.w_None)):
-        if step < 0:
-            stop = -1
-        else:
-            stop = length
-    else:
-        stop = _Eval_SliceIndex(space, w_slice.w_stop)
-        if stop < 0:
-            stop += length
-            if stop < 0:
-                stop =-1
-        elif stop > length:
-            stop = length
-    return start, stop, step
-
-def indices4(space, w_slice, length):
-    start, stop, step = indices3(space, w_slice, length)
-    if (step < 0 and stop >= start) or (step > 0 and start >= stop):
-        slicelength = 0
-    elif step < 0:
-        slicelength = (stop - start + 1) / step + 1
-    else:
-        slicelength = (stop - start - 1) / step + 1
-    return start, stop, step, slicelength
-
 def adapt_bound(space, w_index, w_size):
     if not (space.is_true(space.isinstance(w_index, space.w_int)) or
             space.is_true(space.isinstance(w_index, space.w_long))):

Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Sat Sep 17 18:28:32 2005
@@ -941,7 +941,7 @@
     w = space.wrap
     s = w_str._value
     length = len(s)
-    start, stop, step, sl = slicetype.indices4(space, w_slice, length)
+    start, stop, step, sl = w_slice.indices4(length)
     if sl == 0:
         str = ""
     elif step == 1:

Modified: pypy/dist/pypy/objspace/std/test/test_sliceobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_sliceobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_sliceobject.py	Sat Sep 17 18:28:32 2005
@@ -4,25 +4,22 @@
 class TestW_SliceObject:
 
     def test_indices(self):
-        from pypy.objspace.std import slicetype
         space = self.space
         w = space.wrap
         w_None = space.w_None
         w_slice = space.newslice(w_None, w_None, w_None)
-        assert slicetype.indices3(space, w_slice, 6) == (0, 6, 1)
+        assert w_slice.indices3(6) == (0, 6, 1)
         w_slice = space.newslice(w(0), w(6), w(1))
-        assert slicetype.indices3(space, w_slice, 6) == (0, 6, 1)
+        assert w_slice.indices3(6) == (0, 6, 1)
         w_slice = space.newslice(w_None, w_None, w(-1))
-        assert slicetype.indices3(space, w_slice, 6) == (5, -1, -1)
+        assert w_slice.indices3(6) == (5, -1, -1)
 
     def test_indices_fail(self):
-        from pypy.objspace.std import slicetype
         space = self.space
         w = space.wrap
         w_None = space.w_None
         w_slice = space.newslice(w_None, w_None, w(0))
-        self.space.raises_w(space.w_ValueError,
-                            slicetype.indices3, space, w_slice, 10)
+        self.space.raises_w(space.w_ValueError, w_slice.indices3, 10)
 
 
 class AppTest_SliceObject:

Modified: pypy/dist/pypy/objspace/std/tupleobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/tupleobject.py	(original)
+++ pypy/dist/pypy/objspace/std/tupleobject.py	Sat Sep 17 18:28:32 2005
@@ -2,7 +2,6 @@
 from pypy.objspace.std.intobject import W_IntObject
 from pypy.rpython.rarithmetic import intmask
 from pypy.objspace.std.sliceobject import W_SliceObject
-from pypy.objspace.std import slicetype
 from pypy.interpreter import gateway
 
 class W_TupleObject(W_Object):
@@ -42,7 +41,7 @@
 def getitem__Tuple_Slice(space, w_tuple, w_slice):
     items = w_tuple.wrappeditems
     length = len(items)
-    start, stop, step, slicelength = slicetype.indices4(space, w_slice, length)
+    start, stop, step, slicelength = w_slice.indices4(length)
     assert slicelength >= 0
     subitems = [None] * slicelength
     for i in range(slicelength):

Modified: pypy/dist/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/unicodeobject.py	Sat Sep 17 18:28:32 2005
@@ -3,7 +3,6 @@
 from pypy.objspace.std.stringobject import W_StringObject
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.sliceobject import W_SliceObject
-from pypy.objspace.std import slicetype
 from pypy.rpython.rarithmetic import intmask
 from pypy.module.unicodedata import unicodedb
 
@@ -230,7 +229,7 @@
 def getitem__Unicode_Slice(space, w_uni, w_slice):
     uni = w_uni._value
     length = len(uni)
-    start, stop, step, sl = slicetype.indices4(space, w_slice, length)
+    start, stop, step, sl = w_slice.indices4(length)
     if sl == 0:
         r = []
     elif step == 1:



More information about the Pypy-commit mailing list