[pypy-svn] r58796 - in pypy/branch/2.5-merge/pypy/module/operator: . test

jlg at codespeak.net jlg at codespeak.net
Wed Oct 8 11:46:36 CEST 2008


Author: jlg
Date: Wed Oct  8 11:46:35 2008
New Revision: 58796

Modified:
   pypy/branch/2.5-merge/pypy/module/operator/interp_operator.py
   pypy/branch/2.5-merge/pypy/module/operator/test/test_operator.py
Log:
irepeat() checks for type of arguments

Modified: pypy/branch/2.5-merge/pypy/module/operator/interp_operator.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/module/operator/interp_operator.py	(original)
+++ pypy/branch/2.5-merge/pypy/module/operator/interp_operator.py	Wed Oct  8 11:46:35 2008
@@ -228,7 +228,13 @@
 def irepeat(space, w_obj1, w_obj2):
     'irepeat(a, b) -- Same as a *= b, for a and b sequences.'
     if space.findattr(w_obj1, space.wrap('__getitem__')) is None:
-        raise OperationError(space.w_TypeError, space.w_None)
+        # first arg has to be a sequence
+        raise OperationError(space.w_TypeError, "non-sequence object can't be repeated")
+
+    if not (space.is_true(space.isinstance(w_obj2, space.w_int)) or \
+            space.is_true(space.isinstance(w_obj2, space.w_long))):
+        # second arg has to be int/long
+        raise OperationError(space.w_TypeError, 'an integer is required')
 
     return space.inplace_mul(w_obj1, w_obj2)
 

Modified: pypy/branch/2.5-merge/pypy/module/operator/test/test_operator.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/module/operator/test/test_operator.py	(original)
+++ pypy/branch/2.5-merge/pypy/module/operator/test/test_operator.py	Wed Oct  8 11:46:35 2008
@@ -152,3 +152,22 @@
         assert operator.imul(list, 2) is list
         assert list == [1, 2, 1, 2]
 
+    def test_irepeat(self):
+        import operator
+        import py
+
+        class X(object):
+            def __index__(self):
+                return 5
+
+        a = range(3)
+        raises(TypeError, operator.irepeat)
+        raises(TypeError, operator.irepeat, a, None)
+        raises(TypeError, operator.irepeat, a, [])
+        raises(TypeError, operator.irepeat, a, X())
+        raises(TypeError, operator.irepeat, 6, 7)
+        assert operator.irepeat(a, 2L) is a
+        assert a == [0, 1, 2, 0, 1, 2]
+        assert operator.irepeat(a, 1) is a
+        assert a == [0, 1, 2, 0, 1, 2]
+



More information about the Pypy-commit mailing list