Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r89960:357c149fd26e Date: 2017-02-05 21:49 +0100 http://bitbucket.org/pypy/pypy/changeset/357c149fd26e/
Log: merge heads 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 @@ METH_NOARGS METH_VARARGS METH_KEYWORDS METH_O Py_TPFLAGS_HAVE_INPLACEOPS Py_TPFLAGS_HEAPTYPE Py_TPFLAGS_HAVE_CLASS Py_TPFLAGS_HAVE_NEWBUFFER Py_LT Py_LE Py_EQ Py_NE Py_GT Py_GE Py_TPFLAGS_CHECKTYPES Py_MAX_NDIMS -PyBUF_FORMAT PyBUF_ND PyBUF_STRIDES +PyBUF_FORMAT PyBUF_ND PyBUF_STRIDES PyBUF_WRITABLE """.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 --- a/pypy/module/cpyext/buffer.py +++ b/pypy/module/cpyext/buffer.py @@ -1,7 +1,10 @@ -from rpython.rtyper.lltypesystem import rffi +from rpython.rtyper.lltypesystem import rffi, lltype +from pypy.interpreter.error import oefmt from pypy.module.cpyext.api import ( - cpython_api, CANNOT_FAIL, Py_TPFLAGS_HAVE_NEWBUFFER, cts) -from pypy.module.cpyext.pyobject import PyObject + cpython_api, CANNOT_FAIL, Py_TPFLAGS_HAVE_NEWBUFFER, cts, Py_buffer, + Py_ssize_t, Py_ssize_tP, + PyBUF_WRITABLE, PyBUF_FORMAT, PyBUF_ND, PyBUF_STRIDES) +from pypy.module.cpyext.pyobject import PyObject, Py_IncRef, Py_DecRef @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) def PyObject_CheckBuffer(space, pyobj): @@ -11,7 +14,52 @@ if (flags & Py_TPFLAGS_HAVE_NEWBUFFER and as_buffer.c_bf_getbuffer): return 1 name = rffi.charp2str(cts.cast('char*', pyobj.c_ob_type.c_tp_name)) - if name in ('str', 'bytes'): + if name in ('str', 'bytes'): # XXX remove once wrapper of __buffer__ -> bf_getbuffer works return 1 return 0 + +@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. + """ + 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 + + +@cpython_api([lltype.Ptr(Py_buffer)], lltype.Void, error=CANNOT_FAIL) +def PyBuffer_Release(space, view): + """ + Release the buffer view. This should be called when the buffer is + no longer being used as it may free memory from it + """ + Py_DecRef(space, view.c_obj) + view.c_obj = lltype.nullptr(PyObject.TO) + # XXX do other fields leak memory? 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,12 @@ 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, - PyBUF_FORMAT, PyBUF_ND, PyBUF_STRIDES, + PyVarObject, size_t, slot_function, Py_TPFLAGS_HEAPTYPE, Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE, CONST_STRING, CONST_STRINGP, 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 @@ -476,51 +475,3 @@ with rffi.scoped_nonmovingbuffer(data) as buf: fwrite(buf, 1, count, fp) return 0 - - -PyBUF_WRITABLE = 0x0001 # Copied from object.h - -@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. - """ - 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 - - -@cpython_api([lltype.Ptr(Py_buffer)], lltype.Void, error=CANNOT_FAIL) -def PyBuffer_Release(space, view): - """ - Release the buffer view. This should be called when the buffer is - no longer being used as it may free memory from it - """ - Py_DecRef(space, view.c_obj) - view.c_obj = lltype.nullptr(PyObject.TO) - # XXX do other fields leak memory? _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit