[pypy-svn] pypy default: (holger, mfoord) Extended bytearray extend

mfoord commits-noreply at bitbucket.org
Mon Jan 17 17:58:59 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: 
Changeset: r40786:d84c4557ec6e
Date: 2011-01-17 17:57 +0100
http://bitbucket.org/pypy/pypy/changeset/d84c4557ec6e/

Log:	(holger, mfoord) Extended bytearray extend

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -380,8 +380,22 @@
 def list_extend__Bytearray_ANY(space, w_bytearray, w_other):
     if space.isinstance_w(w_other, space.w_unicode):
         raise OperationError(space.w_TypeError, space.wrap(
-            "bytes string of buffer expected"))
-    w_bytearray.data += [c for c in space.bufferstr_w(w_other)]
+            "bytes string or buffer expected"))
+    if not space.isinstance_w(w_other, space.w_list):
+        w_bytearray.data += [c for c in space.bufferstr_w(w_other)]
+    else:
+        l = list()
+        for w_item in space.unpackiterable(w_other):
+            i = space.int_w(w_item)
+            try:
+                res = chr(i)
+            except ValueError:
+                raise OperationError(
+                    space.w_ValueError,
+                    space.wrap("byte must be in range(0, 256)")
+                )
+            l.append(res)
+        w_bytearray.data += l
 
 def inplace_add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
     list_extend__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2)

diff --git a/pypy/objspace/std/test/test_bytes.py b/pypy/objspace/std/test/test_bytes.py
--- a/pypy/objspace/std/test/test_bytes.py
+++ b/pypy/objspace/std/test/test_bytes.py
@@ -193,6 +193,12 @@
         b.extend(buffer('jkl'))
         assert b == 'abcdefghijkl'
 
+        b = bytearray('world')
+        b.extend([ord(c) for c in 'hello'])
+        assert b == bytearray('worldhello')
+
+        raises(ValueError, b.extend, [256])
+        raises(TypeError, b.extend, [object()])
         raises(TypeError, b.extend, u"unicode")
 
     def test_delslice(self):


More information about the Pypy-commit mailing list