objbuffer.rst 2.15 KB
Newer Older
1 2
.. highlightlang:: c

3 4
Old Buffer Protocol
-------------------
5 6 7 8

.. deprecated:: 3.0

These functions were part of the "old buffer protocol" API in Python 2.
9 10 11 12 13
In Python 3, this protocol doesn't exist anymore but the functions are still
exposed to ease porting 2.x code.  They act as a compatibility wrapper
around the :ref:`new buffer protocol <bufferobjects>`, but they don't give
you control over the lifetime of the resources acquired when a buffer is
exported.
14

15
Therefore, it is recommended that you call :c:func:`PyObject_GetBuffer`
16
(or the ``y*`` or ``w*`` :ref:`format codes <arg-parsing>` with the
17 18
:c:func:`PyArg_ParseTuple` family of functions) to get a buffer view over
an object, and :c:func:`PyBuffer_Release` when the buffer view can be released.
19

20

21
.. c:function:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
22

Christian Heimes's avatar
Christian Heimes committed
23
   Returns a pointer to a read-only memory location usable as character-based
24
   input.  The *obj* argument must support the single-segment character buffer
25 26 27 28
   interface.  On success, returns ``0``, sets *buffer* to the memory location
   and *buffer_len* to the buffer length.  Returns ``-1`` and sets a
   :exc:`TypeError` on error.

29

30
.. c:function:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
31

32 33 34 35 36 37
   Returns a pointer to a read-only memory location containing arbitrary data.
   The *obj* argument must support the single-segment readable buffer
   interface.  On success, returns ``0``, sets *buffer* to the memory location
   and *buffer_len* to the buffer length.  Returns ``-1`` and sets a
   :exc:`TypeError` on error.

38

39
.. c:function:: int PyObject_CheckReadBuffer(PyObject *o)
40 41 42 43 44

   Returns ``1`` if *o* supports the single-segment readable buffer interface.
   Otherwise returns ``0``.


45
.. c:function:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
46 47

   Returns a pointer to a writable memory location.  The *obj* argument must
48 49 50 51
   support the single-segment, character buffer interface.  On success,
   returns ``0``, sets *buffer* to the memory location and *buffer_len* to the
   buffer length.  Returns ``-1`` and sets a :exc:`TypeError` on error.