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

jlg at codespeak.net jlg at codespeak.net
Tue Oct 7 17:13:26 CEST 2008


Author: jlg
Date: Tue Oct  7 17:13:24 2008
New Revision: 58768

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:
concat supports only sequence types as arguments; contat test added

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	Tue Oct  7 17:13:24 2008
@@ -1,3 +1,5 @@
+from pypy.interpreter.error import OperationError
+
 def index(space, w_a):
     return space.index(w_a)
 
@@ -15,8 +17,11 @@
 
 def concat(space, w_obj1, w_obj2):
     'concat(a, b) -- Same as a a + b, for a and b sequences.'
-    return space.add(w_obj1, w_obj2) # XXX cPython only works on types with sequence api
-                                     # we support any with __add__
+    if space.findattr(w_obj1, space.wrap('__getitem__')) is None or \
+            space.findattr(w_obj2, space.wrap('__getitem__')) is None:
+        raise OperationError(space.w_TypeError, space.w_None)
+
+    return space.add(w_obj1, w_obj2)
 
 def contains(space, w_obj1, w_obj2):
     'contains(a, b) -- Same as b in a (note reversed operands).'

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:13:24 2008
@@ -35,3 +35,42 @@
         assert operator.itemgetter(2,10,5)(data) == ('2', '10', '5')
         raises(TypeError, operator.itemgetter(2, 'x', 5), data)
 
+    def test_concat(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
+
+        import operator
+
+        raises(TypeError, operator.concat)
+        raises(TypeError, operator.concat, None, None)
+        assert operator.concat('py', 'thon') == 'python'
+        assert operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4]
+        assert operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7]
+        assert operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7]
+        raises(TypeError, operator.concat, 13, 29)
+



More information about the Pypy-commit mailing list