Buffer Protocol¶
Certain objects available in Python wrap access to an underlying memory
array or buffer. Such objects include the built-in bytes
and
bytearray
, and some extension types like array.array
.
Third-party libraries may define their own types for special purposes, such
as image processing or numeric analysis.
While each of these types have their own semantics, they share the common characteristic of being backed by a possibly large memory buffer. It is then desirable, in some situations, to access that buffer directly and without intermediate copying.
Python provides such a facility at the C and Python level in the form of the buffer protocol. This protocol has two sides:
on the producer side, a type can export a “buffer interface” which allows objects of that type to expose information about their underlying buffer. This interface is described in the section Buffer Object Structures; for Python see Emulating buffer types.
on the consumer side, several means are available to obtain a pointer to the raw underlying data of an object (for example a method parameter). For Python see
memoryview
.
Simple objects such as bytes
and bytearray
expose their
underlying buffer in byte-oriented form. Other forms are possible; for example,
the elements exposed by an array.array
can be multi-byte values.
An example consumer of the buffer interface is the write()
method of file objects: any object that can export a series of bytes through
the buffer interface can be written to a file. While write()
only
needs read-only access to the internal contents of the object passed to it,
other methods such as readinto()
need write access
to the contents of their argument. The buffer interface allows objects to
selectively allow or reject exporting of read-write and read-only buffers.
There are two ways for a consumer of the buffer interface to acquire a buffer over a target object:
call
PyObject_GetBuffer()
with the right parameters;call
PyArg_ParseTuple()
(or one of its siblings) with one of they*
,w*
ors*
format codes.
In both cases, PyBuffer_Release()
must be called when the buffer
isn’t needed anymore. Failure to do so could lead to various issues such as
resource leaks.
Added in version 3.12: The buffer protocol is now accessible in Python, see
Emulating buffer types and memoryview
.
Buffer structure¶
Buffer structures (or simply “buffers”) are useful as a way to expose the binary data from another object to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily. The memory could be a large, constant array in a C extension, it could be a raw block of memory for manipulation before passing to an operating system library, or it could be used to pass around structured data in its native, in-memory format.
Contrary to most data types exposed by the Python interpreter, buffers
are not PyObject
pointers but rather simple C structures. This
allows them to be created and copied very simply. When a generic wrapper
around a buffer is needed, a memoryview object
can be created.
For short instructions how to write an exporting object, see
Buffer Object Structures. For obtaining
a buffer, see PyObject_GetBuffer()
.
-
type Py_buffer¶
- Part of the