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

jlg at codespeak.net jlg at codespeak.net
Tue Oct 7 17:27:20 CEST 2008


Author: jlg
Date: Tue Oct  7 17:27:20 2008
New Revision: 58772

Modified:
   pypy/branch/2.5-merge/pypy/module/operator/app_operator.py
   pypy/branch/2.5-merge/pypy/module/operator/test/test_operator.py
Log:
concat supports only sequence types as arguments; contat test added

Modified: pypy/branch/2.5-merge/pypy/module/operator/app_operator.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/module/operator/app_operator.py	(original)
+++ pypy/branch/2.5-merge/pypy/module/operator/app_operator.py	Tue Oct  7 17:27:20 2008
@@ -54,8 +54,11 @@
     'repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.'
     if not isinstance(num, (int, long)):
         raise TypeError, 'an integer is required'
-    return obj * num   # XXX cPython only supports objects with the sequence
-                       # protocol. We support any with a __mul__
+    if not isSequenceType(obj):
+        raise TypeError, "non-sequence object can't be repeated"
+
+    return obj * num
+
 __repeat__ = repeat
 
 def setslice(a, b, c, d):

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	Tue Oct  7 17:27:20 2008
@@ -74,3 +74,56 @@
         assert operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7]
         raises(TypeError, operator.concat, 13, 29)
 
+    def test_repeat(self):
+        class Seq1:
+            def __init__(self, lst):
+                self.lst = lst
+            def __len__(self):
+                return len(self.lst)
+            def __getitem__(self, i):
+                return self.lst[i]
+            def __add__(self, other):
+                return self.lst + other.lst
+            def __mul__(self, other):
+                return self.lst * other
+            def __rmul__(self, other):
+                return other * self.lst
+
+        class Seq2(object):
+            def __init__(self, lst):
+                self.lst = lst
+            def __len__(self):
+                return len(self.lst)
+            def __getitem__(self, i):
+                return self.lst[i]
+            def __add__(self, other):
+                return self.lst + other.lst
+            def __mul__(self, other):
+                return self.lst * other
+            def __rmul__(self, other):
+                return other * self.lst
+
+        a = range(3)
+        raises(TypeError, operator.repeat)
+        raises(TypeError, operator.repeat, a, None)
+        assert operator.repeat(a, 2) == a+a
+        assert operator.repeat(a, 1) == a
+        assert operator.repeat(a, 0) == []
+        a = (1, 2, 3)
+        assert operator.repeat(a, 2) == a+a
+        assert operator.repeat(a, 1) == a
+        assert operator.repeat(a, 0) == ()
+        a = '123'
+        assert operator.repeat(a, 2) == a+a
+        assert operator.repeat(a, 1) == a
+        assert operator.repeat(a, 0) == ''
+        a = Seq1([4, 5, 6])
+        assert operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]
+        assert operator.repeat(a, 1) == [4, 5, 6]
+        assert operator.repeat(a, 0) == []
+        a = Seq2([4, 5, 6])
+        assert operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]
+        assert operator.repeat(a, 1) == [4, 5, 6]
+        assert operator.repeat(a, 0) == []
+        raises(TypeError, operator.repeat, 6, 7
+



More information about the Pypy-commit mailing list