[pypy-svn] rev 875 - in pypy/trunk/src/pypy/objspace/std: . test

tismer at codespeak.net tismer at codespeak.net
Sat Jun 21 18:53:34 CEST 2003


Author: tismer
Date: Sat Jun 21 18:53:34 2003
New Revision: 875

Modified:
   pypy/trunk/src/pypy/objspace/std/sliceobject.py
   pypy/trunk/src/pypy/objspace/std/slicetype.py
   pypy/trunk/src/pypy/objspace/std/test/test_sliceobject.py
Log:
checked, added/refined tests, seems complete

Modified: pypy/trunk/src/pypy/objspace/std/sliceobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/sliceobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/sliceobject.py	Sat Jun 21 18:53:34 2003
@@ -1,3 +1,10 @@
+"""
+Reviewed 03-06-21
+
+slice object construction   tested, OK
+indices method              tested, OK
+"""
+
 from pypy.objspace.std.objspace import *
 from pypy.interpreter.appfile import AppFile
 from pypy.interpreter.extmodule import make_builtin_func
@@ -16,10 +23,12 @@
         w_self.w_stop = w_stop
         w_self.w_step = w_step
     def indices(w_self, w_length):
+        # this is used internally, analogous to CPython's PySlice_GetIndicesEx
         w_ret = w_self.space.gethelper(appfile).call("sliceindices", [w_self, w_length])
         w_start, w_stop, w_step, w_slicelength = w_self.space.unpackiterable(w_ret, 4)
         return w_start, w_stop, w_step, w_slicelength
     def indices2(w_self, w_length):
+        # this is used to implement the user-visible method 'indices' of slice objects
         return w_self.space.newtuple(w_self.indices(w_length)[:-1])
 
 

Modified: pypy/trunk/src/pypy/objspace/std/slicetype.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/slicetype.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/slicetype.py	Sat Jun 21 18:53:34 2003
@@ -20,6 +20,9 @@
         start, stop = args
     elif len(args) == 3:
         start, stop, step = args        
+    elif len(args) > 3:
+        raise OperationError(space.w_TypeError,
+                             space.wrap("slice() takes at most 3 arguments"))
     else:
         raise OperationError(space.w_TypeError,
                              space.wrap("slice() takes at least 1 argument"))

Modified: pypy/trunk/src/pypy/objspace/std/test/test_sliceobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_sliceobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_sliceobject.py	Sat Jun 21 18:53:34 2003
@@ -10,19 +10,21 @@
         pass
 
     def equal_indices(self, got, expected):
+        got = self.space.unwrap(got)
+        self.assertEqual(len(got), len(expected))
         for g, e in zip(got, expected):
-            self.assertEqual_w(g, self.space.wrap(e))
+            self.assertEqual(g, e)
 
     def test_indices(self):
         space = self.space
         w = space.wrap
         w_None = space.w_None
         w_slice = space.newslice(w_None, w_None, w_None)
-        self.equal_indices(w_slice.indices(w(6)), (0, 6, 1, 6))
+        self.equal_indices(w_slice.indices2(w(6)), (0, 6, 1))
         w_slice = space.newslice(w(0), w(6), w(1))
-        self.equal_indices(w_slice.indices(w(6)), (0, 6, 1, 6))
+        self.equal_indices(w_slice.indices2(w(6)), (0, 6, 1))
         w_slice = space.newslice(w_None, w_None, w(-1))
-        self.equal_indices(w_slice.indices(w(6)), (5, -1, -1, 6))
+        self.equal_indices(w_slice.indices2(w(6)), (5, -1, -1))
 
     def test_indices_fail(self):
         space = self.space
@@ -32,5 +34,17 @@
         self.assertRaises_w(space.w_ValueError,
                             w_slice.indices, w(10))
 
+class Test_SliceObject(test.AppTestCase):
+    def test_new(self):
+        def cmp_slice(sl1, sl2):
+            for attr in "start", "stop", "step":
+                if getattr(sl1, attr) != getattr(sl2, attr):
+                    return False
+            return True
+        self.assertRaises(TypeError, slice)
+        self.assertRaises(TypeError, slice, 1, 2, 3, 4)
+        self.failUnless(cmp_slice(slice(23), slice(None, 23, None)))
+        self.failUnless(cmp_slice(slice(23, 45), slice(23, 45, None)))
+
 if __name__ == '__main__':
     test.main()


More information about the Pypy-commit mailing list