Author: Ronan Lamy <[email protected]>
Branch: py3.5
Changeset: r87919:b1c7fa3d9853
Date: 2016-10-24 17:50 +0100
http://bitbucket.org/pypy/pypy/changeset/b1c7fa3d9853/

Log:    array.frombytes() now requires a buffer of bytes

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
@@ -53,7 +53,7 @@
                     raise
                 a.extend(w_initializer, True)
             else:
-                a.descr_frombytes(space, buf)
+                a._frombytes(space, buf)
     return a
 
 
@@ -262,18 +262,24 @@
         s = space.getarg_w('s#', w_s)
         msg = "fromstring() is deprecated. Use frombytes() instead."
         space.warn(space.wrap(msg), self.space.w_DeprecationWarning)
-        self.descr_frombytes(space, s)
+        self._frombytes(space, s)
 
-    @unwrap_spec(s='bufferstr')
-    def descr_frombytes(self, space, s):
+    def descr_frombytes(self, space, w_s):
         """frombytes(bytestring)
 
         Appends items from the string, interpreting it as an array of
         machine values, as if it had been read from a file using the
         fromfile() method).
         """
+        buf = space.getarg_w('y*', w_s)
+        if buf.getitemsize() != 1:
+            raise oefmt(space.w_TypeError, "a bytes-like object is required")
+        s = buf.as_str()
+        self._frombytes(space, s)
+
+    def _frombytes(self, space, s):
         if len(s) % self.itemsize != 0:
-            raise oefmt(self.space.w_ValueError,
+            raise oefmt(space.w_ValueError,
                         "string length not a multiple of item size")
         oldlen = self.len
         new = len(s) / self.itemsize
@@ -303,7 +309,7 @@
             elems = max(0, len(item) - (len(item) % self.itemsize))
             if n != 0:
                 item = item[0:elems]
-            self.descr_frombytes(space, item)
+            self._frombytes(space, item)
             raise oefmt(space.w_EOFError, "not enough items in file")
         self.descr_fromstring(space, w_item)
 
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
@@ -181,7 +181,10 @@
                 raises(ValueError, a.frombytes, b'\x00' * (2 * a.itemsize + 1))
             b = self.array(t, b'\x00' * a.itemsize * 2)
             assert len(b) == 2 and b[0] == 0 and b[1] == 0
-            raises(ValueError, a.frombytes, a)
+            if t in 'bB':
+                raises(BufferError, a.frombytes, a)
+            else:
+                raises(TypeError, a.frombytes, a)
 
     def test_fromfile(self):
         def myfile(c, s):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to