Buffer Protocol

Python objects implemented in C can export a “buffer interface.” These functions can be used by an object to expose its data in a raw, byte-oriented format. Clients of the object can use the buffer interface to access the object data directly, without needing to copy it first.

Examples of objects that support the buffer interface are bytes, bytearray and array.array. The bytes and bytearray objects exposes their bytes contents in the buffer interface’s byte-oriented form. An array.array can also expose its contents, but it should be noted that array elements may 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:

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.

How the buffer interface is exposed by a type object is described in the section Buffer Object Structures, under the description for PyBufferProcs.

The 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.

Py_buffer
void *buf
A pointer to the start of the memory for the object.
Py_ssize_t len
The total length of the memory in bytes.
int readonly
An indicator of whether the buffer is read only.
const char *format
A NULL terminated string in struct module style syntax giving the contents of the elements available through the buffer. If this is NULL, "B" (unsigned bytes) is assumed.
int ndim
The number of dimensions the memory represents as a multi-dimensional array. If it is 0, strides and suboffsets must be NULL.
Py_ssize_t *shape
An array of Py_ssize_ts the length of ndim giving the shape of the memory as a multi-dimensional array. Note that ((*shape)[0] * ... * (*shape)[ndims-1])*itemsize should be equal to len.
Py_ssize_t *strides
An array of Py_ssize_ts the length of ndim giving the number of bytes to skip to get to a new element in each dimension.
Py_ssize_t *suboffsets

An array of Py_ssize_ts the length of ndim. If these suboffset numbers are greater than or equal to 0, then the value stored along the indicated dimension is a pointer and the suboffset value dictates how many bytes to add to the pointer after de-referencing. A suboffset value that it negative indicates that no de-referencing should occur (striding in a contiguous memory block).

Here is a function that returns a pointer to the element in an N-D array pointed to by an N-dimensional index when there are both non-NULL strides and suboffsets:

void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
    Py_ssize_t *suboffsets, Py_ssize_t *indices) {
    char *pointer = (char*)buf;
    int i;
    for (i = 0; i < ndim; i++) {
        pointer += strides[i] * indices[i];
        if (suboffsets[i] >=0 ) {
            pointer = *((char**)pointer) + suboffsets[i];
        }
    }
    return (void*)pointer;
 }
Py_ssize_t itemsize
This is a storage for the itemsize (in bytes) of each element of the shared memory. It is technically un-necessary as it can be obtained using PyBuffer_SizeFromFormat(), however an exporter may know this information without parsing the format string and it is necessary to know the itemsize for proper interpretation of striding. Therefore, storing it is more convenient and faster.
void *internal
This is for use internally by the exporting object. For example, this might be re-cast as an integer by the exporter and used to store flags about whether or not the shape, strides, and suboffsets arrays must be freed when the buffer is released. The consumer should never alter this value.

Table Of Contents

Previous topic

Iterator Protocol

Next topic

Old Buffer Protocol

This Page