[pypy-commit] pypy py3.5: Move buffer functions to pypy.module.cpyext.buffer
rlamy
pypy.commits at gmail.com
Sun Feb 5 15:15:41 EST 2017
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r89954:1a3f307b706d
Date: 2017-02-05 20:15 +0000
http://bitbucket.org/pypy/pypy/changeset/1a3f307b706d/
Log: Move buffer functions to pypy.module.cpyext.buffer
diff --git a/pypy/module/cpyext/__init__.py b/pypy/module/cpyext/__init__.py
--- a/pypy/module/cpyext/__init__.py
+++ b/pypy/module/cpyext/__init__.py
@@ -40,6 +40,7 @@
import pypy.module.cpyext.pyerrors
import pypy.module.cpyext.typeobject
import pypy.module.cpyext.object
+import pypy.module.cpyext.buffer
import pypy.module.cpyext.bytesobject
import pypy.module.cpyext.bytearrayobject
import pypy.module.cpyext.tupleobject
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -126,7 +126,7 @@
Py_TPFLAGS_HEAPTYPE
Py_LT Py_LE Py_EQ Py_NE Py_GT Py_GE Py_MAX_NDIMS
Py_CLEANUP_SUPPORTED
-PyBUF_FORMAT PyBUF_ND PyBUF_STRIDES
+PyBUF_FORMAT PyBUF_ND PyBUF_STRIDES PyBUF_WRITABLE PyBUF_SIMPLE
""".split()
for name in constant_names:
setattr(CConfig_constants, name, rffi_platform.ConstantInteger(name))
diff --git a/pypy/module/cpyext/buffer.py b/pypy/module/cpyext/buffer.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/buffer.py
@@ -0,0 +1,72 @@
+from rpython.rtyper.lltypesystem import rffi, lltype
+from pypy.interpreter.error import oefmt
+from pypy.module.cpyext.api import (
+ cpython_api, Py_buffer, Py_ssize_t, Py_ssize_tP, CONST_STRINGP,
+ generic_cpy_call,
+ PyBUF_WRITABLE, PyBUF_FORMAT, PyBUF_ND, PyBUF_STRIDES, PyBUF_SIMPLE)
+from pypy.module.cpyext.pyobject import PyObject, Py_IncRef, Py_DecRef
+
+ at cpython_api([PyObject, CONST_STRINGP, Py_ssize_tP], rffi.INT_real, error=-1)
+def PyObject_AsCharBuffer(space, obj, bufferp, sizep):
+ """Returns a pointer to a read-only memory location usable as
+ character-based input. The obj argument must support the single-segment
+ character buffer interface. On success, returns 0, sets buffer to the
+ memory location and size to the buffer length. Returns -1 and sets a
+ TypeError on error.
+ """
+ pto = obj.c_ob_type
+ pb = pto.c_tp_as_buffer
+ if not (pb and pb.c_bf_getbuffer):
+ raise oefmt(space.w_TypeError,
+ "expected an object with the buffer interface")
+ with lltype.scoped_alloc(Py_buffer) as view:
+ ret = generic_cpy_call(
+ space, pb.c_bf_getbuffer,
+ obj, view, rffi.cast(rffi.INT_real, PyBUF_SIMPLE))
+ if rffi.cast(lltype.Signed, ret) == -1:
+ return -1
+
+ bufferp[0] = rffi.cast(rffi.CCHARP, view.c_buf)
+ sizep[0] = view.c_len
+
+ if pb.c_bf_releasebuffer:
+ generic_cpy_call(space, pb.c_bf_releasebuffer,
+ obj, view)
+ Py_DecRef(space, view.c_obj)
+ return 0
+
+
+ at cpython_api([lltype.Ptr(Py_buffer), PyObject, rffi.VOIDP, Py_ssize_t,
+ lltype.Signed, lltype.Signed], rffi.INT, error=-1)
+def PyBuffer_FillInfo(space, view, obj, buf, length, readonly, flags):
+ """
+ Fills in a buffer-info structure correctly for an exporter that can only
+ share a contiguous chunk of memory of "unsigned bytes" of the given
+ length. Returns 0 on success and -1 (with raising an error) on error.
+ """
+ flags = rffi.cast(lltype.Signed, flags)
+ if flags & PyBUF_WRITABLE and readonly:
+ raise oefmt(space.w_ValueError, "Object is not writable")
+ view.c_buf = buf
+ view.c_len = length
+ view.c_obj = obj
+ if obj:
+ Py_IncRef(space, obj)
+ view.c_itemsize = 1
+ rffi.setintfield(view, 'c_readonly', readonly)
+ rffi.setintfield(view, 'c_ndim', 1)
+ view.c_format = lltype.nullptr(rffi.CCHARP.TO)
+ if (flags & PyBUF_FORMAT) == PyBUF_FORMAT:
+ view.c_format = rffi.str2charp("B")
+ view.c_shape = lltype.nullptr(Py_ssize_tP.TO)
+ if (flags & PyBUF_ND) == PyBUF_ND:
+ view.c_shape = rffi.cast(Py_ssize_tP, view.c__shape)
+ view.c_shape[0] = view.c_len
+ view.c_strides = lltype.nullptr(Py_ssize_tP.TO)
+ if (flags & PyBUF_STRIDES) == PyBUF_STRIDES:
+ view.c_strides = rffi.cast(Py_ssize_tP, view.c__strides)
+ view.c_strides[0] = view.c_itemsize
+ view.c_suboffsets = lltype.nullptr(Py_ssize_tP.TO)
+ view.c_internal = lltype.nullptr(rffi.VOIDP.TO)
+
+ return 0
diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -1,13 +1,11 @@
from rpython.rtyper.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import (
- cpython_api, generic_cpy_call, CANNOT_FAIL, Py_ssize_t, Py_ssize_tP,
- PyVarObject, Py_buffer, size_t, slot_function, cts,
- PyBUF_FORMAT, PyBUF_ND, PyBUF_STRIDES,
+ cpython_api, generic_cpy_call, CANNOT_FAIL, Py_ssize_t,
+ PyVarObject, size_t, slot_function, cts,
Py_TPFLAGS_HEAPTYPE, Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT,
- Py_GE, CONST_STRING, CONST_STRINGP, FILEP, fwrite)
+ Py_GE, CONST_STRING, FILEP, fwrite)
from pypy.module.cpyext.pyobject import (
- PyObject, PyObjectP, create_ref, from_ref, Py_IncRef, Py_DecRef,
- get_typedescr, _Py_NewReference)
+ PyObject, PyObjectP, from_ref, Py_IncRef, Py_DecRef, get_typedescr)
from pypy.module.cpyext.typeobject import PyTypeObjectPtr
from pypy.module.cpyext.pyerrors import PyErr_NoMemory, PyErr_BadInternalCall
from pypy.objspace.std.typeobject import W_TypeObject
@@ -16,10 +14,6 @@
import pypy.module.__builtin__.operation as operation
-# from include/object.h
-PyBUF_SIMPLE = 0x0000
-PyBUF_WRITABLE = 0x0001
-
@cpython_api([size_t], rffi.VOIDP)
def PyObject_Malloc(space, size):
# returns non-zero-initialized memory, like CPython
@@ -444,36 +438,6 @@
is active then NULL is returned but PyErr_Occurred() will return false."""
return space.call_function(space.builtin.get('dir'), w_o)
- at cpython_api([PyObject, CONST_STRINGP, Py_ssize_tP], rffi.INT_real, error=-1)
-def PyObject_AsCharBuffer(space, obj, bufferp, sizep):
- """Returns a pointer to a read-only memory location usable as
- character-based input. The obj argument must support the single-segment
- character buffer interface. On success, returns 0, sets buffer to the
- memory location and size to the buffer length. Returns -1 and sets a
- TypeError on error.
- """
- pto = obj.c_ob_type
-
- pb = pto.c_tp_as_buffer
- if not (pb and pb.c_bf_getbuffer):
- raise oefmt(space.w_TypeError,
- "expected an object with the buffer interface")
- with lltype.scoped_alloc(Py_buffer) as view:
- ret = generic_cpy_call(
- space, pb.c_bf_getbuffer,
- obj, view, rffi.cast(rffi.INT_real, PyBUF_SIMPLE))
- if rffi.cast(lltype.Signed, ret) == -1:
- return -1
-
- bufferp[0] = rffi.cast(rffi.CCHARP, view.c_buf)
- sizep[0] = view.c_len
-
- if pb.c_bf_releasebuffer:
- generic_cpy_call(space, pb.c_bf_releasebuffer,
- obj, view)
- Py_DecRef(space, view.c_obj)
- return 0
-
# Also in include/object.h
Py_PRINT_RAW = 1 # No string quotes etc.
@@ -493,41 +457,3 @@
with rffi.scoped_nonmovingbuffer(data) as buf:
fwrite(buf, 1, count, fp)
return 0
-
-
-PyBUF_WRITABLE = 0x0001 # Copied from object.h
-
- at cpython_api([lltype.Ptr(Py_buffer), PyObject, rffi.VOIDP, Py_ssize_t,
- lltype.Signed, lltype.Signed], rffi.INT, error=-1)
-def PyBuffer_FillInfo(space, view, obj, buf, length, readonly, flags):
- """
- Fills in a buffer-info structure correctly for an exporter that can only
- share a contiguous chunk of memory of "unsigned bytes" of the given
- length. Returns 0 on success and -1 (with raising an error) on error.
- """
- flags = rffi.cast(lltype.Signed, flags)
- if flags & PyBUF_WRITABLE and readonly:
- raise oefmt(space.w_ValueError, "Object is not writable")
- view.c_buf = buf
- view.c_len = length
- view.c_obj = obj
- if obj:
- Py_IncRef(space, obj)
- view.c_itemsize = 1
- rffi.setintfield(view, 'c_readonly', readonly)
- rffi.setintfield(view, 'c_ndim', 1)
- view.c_format = lltype.nullptr(rffi.CCHARP.TO)
- if (flags & PyBUF_FORMAT) == PyBUF_FORMAT:
- view.c_format = rffi.str2charp("B")
- view.c_shape = lltype.nullptr(Py_ssize_tP.TO)
- if (flags & PyBUF_ND) == PyBUF_ND:
- view.c_shape = rffi.cast(Py_ssize_tP, view.c__shape)
- view.c_shape[0] = view.c_len
- view.c_strides = lltype.nullptr(Py_ssize_tP.TO)
- if (flags & PyBUF_STRIDES) == PyBUF_STRIDES:
- view.c_strides = rffi.cast(Py_ssize_tP, view.c__strides)
- view.c_strides[0] = view.c_itemsize
- view.c_suboffsets = lltype.nullptr(Py_ssize_tP.TO)
- view.c_internal = lltype.nullptr(rffi.VOIDP.TO)
-
- return 0
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
@@ -548,14 +548,14 @@
@slot_function([PyObject, lltype.Ptr(Py_buffer), rffi.INT_real], rffi.INT_real, error=-1)
def bytes_getbuffer(space, w_str, view, flags):
from pypy.module.cpyext.bytesobject import PyBytes_AsString
- from pypy.module.cpyext.object import PyBuffer_FillInfo
+ from pypy.module.cpyext.buffer import PyBuffer_FillInfo
c_buf = rffi.cast(rffi.VOIDP, PyBytes_AsString(space, w_str))
return PyBuffer_FillInfo(space, view, w_str, c_buf,
space.len_w(w_str), 1, flags)
@slot_function([PyObject, lltype.Ptr(Py_buffer), rffi.INT_real], rffi.INT_real, error=-1)
def bf_getbuffer(space, w_obj, view, flags):
- from pypy.module.cpyext.object import PyBuffer_FillInfo
+ from pypy.module.cpyext.buffer 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,
More information about the pypy-commit
mailing list