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

arigo at codespeak.net arigo at codespeak.net
Wed Apr 27 18:36:56 CEST 2005


Author: arigo
Date: Wed Apr 27 18:36:56 2005
New Revision: 11538

Modified:
   pypy/dist/pypy/objspace/descroperation.py
   pypy/dist/pypy/objspace/test/test_descroperation.py
Log:
__getslice__()&co should not receive None arguments, but 0 or sys.maxint.
Added tests and fixed.



Modified: pypy/dist/pypy/objspace/descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/descroperation.py	(original)
+++ pypy/dist/pypy/objspace/descroperation.py	Wed Apr 27 18:36:56 2005
@@ -1,4 +1,4 @@
-import operator
+import operator, sys
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root, BaseWrappable
 from pypy.interpreter.function import Function
@@ -180,14 +180,23 @@
                    space.wrap("iterator has no next() method"))
         return space.get_and_call_function(w_descr, w_obj)
 
+    def _oldstyle_slice_range(space, w_key):
+        w_start = space.getattr(w_key, space.wrap('start'))
+        w_stop  = space.getattr(w_key, space.wrap('stop'))
+        if space.is_w(w_start, space.w_None):
+            w_start = space.wrap(0)
+        if space.is_w(w_stop, space.w_None):
+            w_stop = space.wrap(sys.maxint)
+        return w_start, w_stop
+
     def getitem(space, w_obj, w_key):
         if space.is_true(space.isinstance(w_key, space.w_slice)):
             if space.is_w(space.getattr(w_key, space.wrap('step')), space.w_None):
                 w_descr = space.lookup(w_obj, '__getslice__')
                 if w_descr is not None:
+                    w_start, w_stop = space._oldstyle_slice_range(w_key)
                     return space.get_and_call_function(w_descr, w_obj,
-                                                       space.getattr(w_key, space.wrap('start')),
-                                                       space.getattr(w_key, space.wrap('stop')))
+                                                       w_start, w_stop)
         w_descr = space.lookup(w_obj, '__getitem__')
         if w_descr is None:
             raise OperationError(space.w_TypeError,
@@ -199,9 +208,9 @@
             if space.is_w(space.getattr(w_key, space.wrap('step')), space.w_None):
                 w_descr = space.lookup(w_obj, '__setslice__')
                 if w_descr is not None:
+                    w_start, w_stop = space._oldstyle_slice_range(w_key)
                     return space.get_and_call_function(w_descr, w_obj,
-                                                       space.getattr(w_key, space.wrap('start')),
-                                                       space.getattr(w_key, space.wrap('stop')),
+                                                       w_start, w_stop,
                                                        w_val)                    
         w_descr = space.lookup(w_obj, '__setitem__')
         if w_descr is None:
@@ -214,9 +223,9 @@
             if space.is_w(space.getattr(w_key, space.wrap('step')), space.w_None):
                 w_descr = space.lookup(w_obj, '__delslice__')
                 if w_descr is not None:
+                    w_start, w_stop = space._oldstyle_slice_range(w_key)
                     return space.get_and_call_function(w_descr, w_obj,
-                                                       space.getattr(w_key, space.wrap('start')),
-                                                       space.getattr(w_key, space.wrap('stop')))
+                                                       w_start, w_stop)
         w_descr = space.lookup(w_obj, '__delitem__')
         if w_descr is None:
             raise OperationError(space.w_TypeError,

Modified: pypy/dist/pypy/objspace/test/test_descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/test/test_descroperation.py	(original)
+++ pypy/dist/pypy/objspace/test/test_descroperation.py	Wed Apr 27 18:36:56 2005
@@ -13,6 +13,56 @@
         sq = Sq()
 
         assert sq[1:3] == (1,3)
+        import sys
+        assert sq[1:] == (1, sys.maxint)
+        assert sq[:3] == (0, 3)
+        assert sq[:] == (0, sys.maxint)
+
+    def test_setslice(self):
+        class Sq(object):
+            def __setslice__(self, start, stop, sequence):
+                ops.append((start, stop, sequence))
+
+            def __setitem__(self, key, value):
+                raise AssertionError, key
+
+        sq = Sq()
+        ops = []
+        sq[-5:3] = 'hello'
+        sq[12:] = 'world'
+        sq[:-1] = 'spam'
+        sq[:] = 'egg'
+
+        import sys
+        assert ops == [
+            (-5, 3,          'hello'),
+            (12, sys.maxint, 'world'),
+            (0,  -1,         'spam'),
+            (0,  sys.maxint, 'egg'),
+            ]
+
+    def test_delslice(self):
+        class Sq(object):
+            def __delslice__(self, start, stop):
+                ops.append((start, stop))
+
+            def __delitem__(self, key):
+                raise AssertionError, key
+
+        sq = Sq()
+        ops = []
+        del sq[5:-3]
+        del sq[-12:]
+        del sq[:1]
+        del sq[:]
+
+        import sys
+        assert ops == [
+            (5,   -3),
+            (-12, sys.maxint),
+            (0,   1),
+            (0,   sys.maxint),
+            ]
 
     def test_ipow(self):
         x = 2



More information about the Pypy-commit mailing list