[pypy-commit] pypy py3k: Fix and test for the array module: accept all objects implementing the buffer interface where previously only bytes were accepted.

Manuel Jacob noreply at buildbot.pypy.org
Wed Feb 13 22:24:54 CET 2013


Author: Manuel Jacob
Branch: py3k
Changeset: r61195:4eaf46aa0ce4
Date: 2013-02-12 02:23 +0100
http://bitbucket.org/pypy/pypy/changeset/4eaf46aa0ce4/

Log:	Fix and test for the array module: accept all objects implementing
	the buffer interface where previously only bytes were accepted.

diff --git a/pypy/module/array/interp_array.py b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -38,8 +38,11 @@
 
             if len(__args__.arguments_w) > 0:
                 w_initializer = __args__.arguments_w[0]
-                if space.type(w_initializer) is space.w_bytes:
-                    a.fromstring(space.bytes_w(w_initializer))
+                if space.lookup(w_initializer, '__buffer__') is not None:
+                    if isinstance(w_initializer, W_ArrayBase):
+                        a.extend(w_initializer, True)
+                    else:
+                        a.fromstring(space.bufferstr_w(w_initializer))
                 elif space.type(w_initializer) is space.w_list:
                     a.fromlist(w_initializer)
                 else:
@@ -569,7 +572,7 @@
         self.fromlist(w_lst)
 
     def array_frombytes__Array_ANY(space, self, w_s):
-        self.fromstring(space.bytes_w(w_s))
+        self.fromstring(space.bufferstr_w(w_s))
 
     def array_fromstring__Array_ANY(space, self, w_s):
         space.warn("fromstring() is deprecated. Use frombytes() instead.",
diff --git a/pypy/module/array/test/test_array.py b/pypy/module/array/test/test_array.py
--- a/pypy/module/array/test/test_array.py
+++ b/pypy/module/array/test/test_array.py
@@ -848,6 +848,13 @@
         assert l
         assert l[0] is None or len(l[0]) == 0
 
+    def test_bytearray(self):
+        a = self.array('u', 'hi')
+        b = self.array('u')
+        b.frombytes(bytearray(a.tobytes()))
+        assert a == b
+        assert self.array('u', bytearray(a.tobytes())) == a
+
 
 class DontTestCPythonsOwnArray(BaseArrayTests):
 


More information about the pypy-commit mailing list