[pypy-commit] pypy PyBuffer: Merge space.getarg_w('w*') and space.writebuf_w()

rlamy pypy.commits at gmail.com
Thu Apr 20 12:36:59 EDT 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: PyBuffer
Changeset: r91102:32eda5f71241
Date: 2017-04-20 17:36 +0100
http://bitbucket.org/pypy/pypy/changeset/32eda5f71241/

Log:	Merge space.getarg_w('w*') and space.writebuf_w()

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1483,10 +1483,9 @@
     def writebuf_w(self, w_obj):
         # Old buffer interface, returns a writeable buffer (PyObject_AsWriteBuffer)
         try:
-            return w_obj.buffer_w(self, self.BUF_WRITABLE)
-        except BufferInterfaceNotFound:
-            raise oefmt(self.w_TypeError,
-                        "expected an object with a writable buffer interface")
+            return w_obj.buffer_w(self, self.BUF_WRITABLE).as_binary_rw()
+        except (BufferInterfaceNotFound, OperationError):
+            self._getarg_error("read-write bytes-like object", w_obj)
 
     def charbuf_w(self, w_obj):
         # Old buffer interface, returns a character buffer (PyObject_AsCharBuffer)
@@ -1537,13 +1536,7 @@
             except BufferInterfaceNotFound:
                 self._getarg_error("bytes or read-only buffer", w_obj)
         elif code == 'w*':
-            try:
-                return w_obj.buffer_w(self, self.BUF_WRITABLE).as_binary_rw()
-            except OperationError:
-                pass
-            except BufferInterfaceNotFound:
-                pass
-            self._getarg_error("read-write buffer", w_obj)
+            return self.writebuf_w(w_obj)
         elif code == 'y*':
             try:
                 return w_obj.buffer_w(self, self.BUF_SIMPLE).as_binary()
diff --git a/pypy/module/_cffi_backend/func.py b/pypy/module/_cffi_backend/func.py
--- a/pypy/module/_cffi_backend/func.py
+++ b/pypy/module/_cffi_backend/func.py
@@ -116,13 +116,7 @@
     return buf.as_binary()
 
 def _fetch_as_write_buffer(space, w_x):
-    try:
-        buf = space.writebuf_w(w_x)
-    except OperationError as e:
-        if not e.match(space, space.w_TypeError):
-            raise
-        buf = space.buffer_w(w_x, space.BUF_WRITABLE)
-    return buf.as_binary()
+    return space.writebuf_w(w_x)
 
 @unwrap_spec(w_ctype=ctypeobj.W_CType)
 def from_buffer(space, w_ctype, w_x):
diff --git a/pypy/module/fcntl/interp_fcntl.py b/pypy/module/fcntl/interp_fcntl.py
--- a/pypy/module/fcntl/interp_fcntl.py
+++ b/pypy/module/fcntl/interp_fcntl.py
@@ -209,7 +209,7 @@
     op = rffi.cast(rffi.INT, op)        # C long => C int
 
     try:
-        rwbuffer = space.writebuf_w(w_arg).as_binary()
+        rwbuffer = space.writebuf_w(w_arg)
     except OperationError as e:
         if not (e.match(space, space.w_TypeError) or
                 e.match(space, space.w_BufferError)):
diff --git a/pypy/module/struct/test/test_struct.py b/pypy/module/struct/test/test_struct.py
--- a/pypy/module/struct/test/test_struct.py
+++ b/pypy/module/struct/test/test_struct.py
@@ -365,8 +365,8 @@
         assert self.struct.unpack("ii", b) == (62, 12)
         raises(self.struct.error, self.struct.unpack, "i", b)
 
-    def test_pack_unpack_buffer(self):
-        import array
+    def test_pack_buffer(self):
+        import array, sys
         b = array.array('b', b'\x00' * 19)
         sz = self.struct.calcsize("ii")
         for offset in [2, -17]:
@@ -379,10 +379,16 @@
         assert bytes(b2) == self.struct.pack("ii", 17, 42) + (b'\x00' * 11)
 
         exc = raises(TypeError, self.struct.pack_into, "ii", b'test', 0, 17, 42)
-        assert str(exc.value) == "a read-write buffer is required, not bytes"
+        if '__pypy__' in sys.modules:
+            assert str(exc.value) == "a read-write bytes-like object is required, not bytes"
         exc = raises(self.struct.error, self.struct.pack_into, "ii", b[0:1], 0, 17, 42)
         assert str(exc.value) == "pack_into requires a buffer of at least 8 bytes"
 
+    def test_unpack_buffer(self):
+        import array
+        b = array.array('b', b'\x00' * 19)
+        for offset in [2, -17]:
+            self.struct.pack_into("ii", b, offset, 17, 42)
         assert self.struct.unpack_from("ii", b, 2) == (17, 42)
         assert self.struct.unpack_from("ii", b, -17) == (17, 42)
         assert self.struct.unpack_from("ii", memoryview(b)[2:]) == (17, 42)


More information about the pypy-commit mailing list