Author: Ronan Lamy <[email protected]>
Branch:
Changeset: r91675:94366943700b
Date: 2017-07-04 17:54 +0100
http://bitbucket.org/pypy/pypy/changeset/94366943700b/
Log: Move PyObject_AsCharBuffer to buffer.py for consistency with py3.5
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,10 +1,33 @@
from rpython.rtyper.lltypesystem import rffi, lltype
from pypy.interpreter.error import oefmt
from pypy.module.cpyext.api import (
- cpython_api, CANNOT_FAIL, cts, Py_buffer,
- Py_ssize_t, Py_ssize_tP, generic_cpy_call,
+ cpython_api, Py_buffer, Py_ssize_t, Py_ssize_tP, CONST_STRINGP, cts,
+ generic_cpy_call,
PyBUF_WRITABLE, PyBUF_FORMAT, PyBUF_ND, PyBUF_STRIDES)
-from pypy.module.cpyext.pyobject import PyObject, Py_IncRef
+from pypy.module.cpyext.pyobject import PyObject, incref
+
+@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_getreadbuffer and pb.c_bf_getsegcount):
+ raise oefmt(space.w_TypeError, "expected a character buffer object")
+ if generic_cpy_call(space, pb.c_bf_getsegcount,
+ obj, lltype.nullptr(Py_ssize_tP.TO)) != 1:
+ raise oefmt(space.w_TypeError,
+ "expected a single-segment buffer object")
+ size = generic_cpy_call(space, pb.c_bf_getcharbuffer,
+ obj, 0, bufferp)
+ if size < 0:
+ return -1
+ sizep[0] = size
+ return 0
@cpython_api([lltype.Ptr(Py_buffer), PyObject, rffi.VOIDP, Py_ssize_t,
lltype.Signed, lltype.Signed], rffi.INT, error=-1)
@@ -20,7 +43,7 @@
view.c_len = length
view.c_obj = obj
if obj:
- Py_IncRef(space, obj)
+ incref(space, obj)
view.c_itemsize = 1
rffi.setintfield(view, 'c_readonly', readonly)
rffi.setintfield(view, 'c_ndim', 1)
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
@@ -3,7 +3,7 @@
cpython_api, generic_cpy_call, CANNOT_FAIL, Py_ssize_t, Py_ssize_tP,
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)
+ Py_GE, CONST_STRING, FILEP, fwrite)
from pypy.module.cpyext.pyobject import (
PyObject, PyObjectP, from_ref, Py_IncRef, Py_DecRef,
get_typedescr)
@@ -432,30 +432,6 @@
is active then NULL is returned but PyErr_Occurred() will return false."""
return space.call_function(space.builtin.get('dir'), w_o)
-@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_getreadbuffer and pb.c_bf_getsegcount):
- raise oefmt(space.w_TypeError, "expected a character buffer object")
- if generic_cpy_call(space, pb.c_bf_getsegcount,
- obj, lltype.nullptr(Py_ssize_tP.TO)) != 1:
- raise oefmt(space.w_TypeError,
- "expected a single-segment buffer object")
- size = generic_cpy_call(space, pb.c_bf_getcharbuffer,
- obj, 0, bufferp)
- if size < 0:
- return -1
- sizep[0] = size
- return 0
-
# Also in include/object.h
Py_PRINT_RAW = 1 # No string quotes etc.
diff --git a/pypy/module/cpyext/test/test_bytesobject.py
b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -11,7 +11,7 @@
_PyString_Join)
from pypy.module.cpyext.api import PyObjectP, PyObject, Py_ssize_tP,
generic_cpy_call
from pypy.module.cpyext.pyobject import Py_DecRef, from_ref, make_ref
-from pypy.module.cpyext.object import PyObject_AsCharBuffer
+from pypy.module.cpyext.buffer import PyObject_AsCharBuffer
from pypy.module.cpyext.api import PyTypeObjectPtr
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit