xref: /third_party/python/Doc/c-api/objbuffer.rst (revision 7db96d56)
17db96d56Sopenharmony_ci.. highlight:: c
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ciOld Buffer Protocol
47db96d56Sopenharmony_ci-------------------
57db96d56Sopenharmony_ci
67db96d56Sopenharmony_ci.. deprecated:: 3.0
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ciThese functions were part of the "old buffer protocol" API in Python 2.
97db96d56Sopenharmony_ciIn Python 3, this protocol doesn't exist anymore but the functions are still
107db96d56Sopenharmony_ciexposed to ease porting 2.x code.  They act as a compatibility wrapper
117db96d56Sopenharmony_ciaround the :ref:`new buffer protocol <bufferobjects>`, but they don't give
127db96d56Sopenharmony_ciyou control over the lifetime of the resources acquired when a buffer is
137db96d56Sopenharmony_ciexported.
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ciTherefore, it is recommended that you call :c:func:`PyObject_GetBuffer`
167db96d56Sopenharmony_ci(or the ``y*`` or ``w*`` :ref:`format codes <arg-parsing>` with the
177db96d56Sopenharmony_ci:c:func:`PyArg_ParseTuple` family of functions) to get a buffer view over
187db96d56Sopenharmony_cian object, and :c:func:`PyBuffer_Release` when the buffer view can be released.
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ci.. c:function:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ci   Returns a pointer to a read-only memory location usable as character-based
247db96d56Sopenharmony_ci   input.  The *obj* argument must support the single-segment character buffer
257db96d56Sopenharmony_ci   interface.  On success, returns ``0``, sets *buffer* to the memory location
267db96d56Sopenharmony_ci   and *buffer_len* to the buffer length.  Returns ``-1`` and sets a
277db96d56Sopenharmony_ci   :exc:`TypeError` on error.
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci.. c:function:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ci   Returns a pointer to a read-only memory location containing arbitrary data.
337db96d56Sopenharmony_ci   The *obj* argument must support the single-segment readable buffer
347db96d56Sopenharmony_ci   interface.  On success, returns ``0``, sets *buffer* to the memory location
357db96d56Sopenharmony_ci   and *buffer_len* to the buffer length.  Returns ``-1`` and sets a
367db96d56Sopenharmony_ci   :exc:`TypeError` on error.
377db96d56Sopenharmony_ci
387db96d56Sopenharmony_ci
397db96d56Sopenharmony_ci.. c:function:: int PyObject_CheckReadBuffer(PyObject *o)
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci   Returns ``1`` if *o* supports the single-segment readable buffer interface.
427db96d56Sopenharmony_ci   Otherwise returns ``0``.  This function always succeeds.
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   Note that this function tries to get and release a buffer, and exceptions
457db96d56Sopenharmony_ci   which occur while calling corresponding functions will get suppressed.
467db96d56Sopenharmony_ci   To get error reporting use :c:func:`PyObject_GetBuffer()` instead.
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ci
497db96d56Sopenharmony_ci.. c:function:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ci   Returns a pointer to a writable memory location.  The *obj* argument must
527db96d56Sopenharmony_ci   support the single-segment, character buffer interface.  On success,
537db96d56Sopenharmony_ci   returns ``0``, sets *buffer* to the memory location and *buffer_len* to the
547db96d56Sopenharmony_ci   buffer length.  Returns ``-1`` and sets a :exc:`TypeError` on error.
557db96d56Sopenharmony_ci
56