[pypy-commit] pypy py3.5: Add bf_getbuffer slot for bytearray

rlamy pypy.commits at gmail.com
Sun Dec 11 12:44:31 EST 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r89011:3b4ef771a4d4
Date: 2016-12-11 17:43 +0000
http://bitbucket.org/pypy/pypy/changeset/3b4ef771a4d4/

Log:	Add bf_getbuffer slot for bytearray

diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -508,10 +508,25 @@
     return PyBuffer_FillInfo(space, view, w_str, c_buf,
                              space.len_w(w_str), 1, flags)
 
-def setup_bytes_buffer_procs(space, pto):
+ at cpython_api([PyObject, lltype.Ptr(Py_buffer), rffi.INT_real], rffi.INT_real,
+              header=None, error=-1)
+def bf_getbuffer(space, w_obj, view, flags):
+    from pypy.module.cpyext.object import PyBuffer_FillInfo
+    buf = space.buffer_w(w_obj, rffi.cast(lltype.Signed, flags))
+    c_buf = rffi.cast(rffi.VOIDP, buf.get_raw_address())
+    return PyBuffer_FillInfo(space, view, w_obj, c_buf,
+                             space.len_w(w_obj), 0, flags)
+
+def setup_buffer_procs(space, w_type, pto):
+    bufspec = w_type.layout.typedef.buffer
+    if not bufspec:
+        return
     c_buf = lltype.malloc(PyBufferProcs, flavor='raw', zero=True)
     lltype.render_immortal(c_buf)
-    c_buf.c_bf_getbuffer = llslot(space, bytes_getbuffer)
+    if space.is_w(w_type, space.w_bytes):
+        c_buf.c_bf_getbuffer = llslot(space, bytes_getbuffer)
+    else:
+        c_buf.c_bf_getbuffer = llslot(space, bf_getbuffer)
     pto.c_tp_as_buffer = c_buf
     pto.c_tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER
 
@@ -580,8 +595,7 @@
     elif space.is_w(w_type, space.w_tuple):
         pto.c_tp_itemsize = rffi.sizeof(PyObject)
     # buffer protocol
-    if space.is_w(w_type, space.w_str):
-        setup_bytes_buffer_procs(space, pto)
+    setup_buffer_procs(space, w_type, pto)
 
     pto.c_tp_free = llslot(space, PyObject_Free)
     pto.c_tp_alloc = llslot(space, PyType_GenericAlloc)
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
@@ -1052,7 +1052,7 @@
 
 
 W_BytearrayObject.typedef = TypeDef(
-    "bytearray",
+    "bytearray", None, None, "read-write",
     __doc__ = BytearrayDocstrings.__doc__,
     __new__ = interp2app(W_BytearrayObject.descr_new),
     __hash__ = None,
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -793,7 +793,7 @@
 
 
 W_BytesObject.typedef = TypeDef(
-    "bytes",
+    "bytes", None, None, "read",
     __new__ = interp2app(W_BytesObject.descr_new),
     __doc__ = """bytes(iterable_of_ints) -> bytes
     bytes(string, encoding[, errors]) -> bytes


More information about the pypy-commit mailing list