[pypy-svn] pypy default: (holger, mfoord) bytearray.extend takes iterables of strings
mfoord
commits-noreply at bitbucket.org
Mon Jan 17 18:48:07 CET 2011
Author: Michael Foord <michael at voidspace.org.uk>
Branch:
Changeset: r40806:514e29c0a7cf
Date: 2011-01-17 18:11 +0100
http://bitbucket.org/pypy/pypy/changeset/514e29c0a7cf/
Log: (holger, mfoord) bytearray.extend takes iterables of strings
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
@@ -386,14 +386,19 @@
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)")
- )
+ if space.isinstance_w(w_item, space.w_str):
+ res = space.str_w(w_item)
+ else:
+ 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
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
@@ -197,6 +197,10 @@
b.extend([ord(c) for c in 'hello'])
assert b == bytearray('worldhello')
+ b = bytearray('world')
+ b.extend(list('hello'))
+ assert b == bytearray('worldhello')
+
raises(ValueError, b.extend, [256])
raises(TypeError, b.extend, [object()])
raises(TypeError, b.extend, u"unicode")
More information about the Pypy-commit
mailing list