Python objects implemented in C can export a group of functions called the “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.
Two examples of objects that support the buffer interface are strings and arrays. The string object exposes the character contents in the buffer interface’s byte-oriented form. An array can also expose its contents, but it should be noted that array elements may be multi-byte values.
An example user of the buffer interface is the file object’s write() method. Any object that can export a series of bytes through the buffer interface can be written to a file. There are a number of format codes to PyArg_ParseTuple() that operate against an object’s buffer interface, returning data from the target object.
Starting from version 1.6, Python has been providing Python-level buffer objects and a C-level buffer API so that any built-in or used-defined type can expose its characteristics. Both, however, have been deprecated because of various shortcomings, and have been officially removed in Python 3.0 in favour of a new C-level buffer API and a new Python-level object named memoryview.
The new buffer API has been backported to Python 2.6, and the memoryview object has been backported to Python 2.7. It is strongly advised to use them rather than the old APIs, unless you are blocked from doing so for compatibility reasons.
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-dimesional 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;
}
More information on the buffer interface is provided in the section Buffer Object Structures, under the description for PyBufferProcs.
A “buffer object” is defined in the bufferobject.h header (included by Python.h). These objects look very similar to string objects at the Python programming level: they support slicing, indexing, concatenation, and some other standard string operations. However, their data can come from one of two sources: from a block of memory, or from another object which exports the buffer interface.
Buffer objects are useful as a way to expose the data from another object’s buffer interface 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.
The instance of PyTypeObject which represents the Python buffer type; it is the same object as buffer and types.BufferType in the Python layer. .
Return a new read-only buffer object. This raises TypeError if base doesn’t support the read-only buffer protocol or doesn’t provide exactly one buffer segment, or it raises ValueError if offset is less than zero. The buffer will hold a reference to the base object, and the buffer’s contents will refer to the base object’s buffer interface, starting as position offset and extending for size bytes. If size is Py_END_OF_BUFFER, then the new buffer’s contents extend to the length of the base object’s exported buffer data.
Changed in version 2.5: This function used an int type for offset and size. This might require changes in your code for properly supporting 64-bit systems.
Return a new writable buffer object. Parameters and exceptions are similar to those for PyBuffer_FromObject(). If the base object does not export the writeable buffer protocol, then TypeError is raised.
Changed in version 2.5: This function used an int type for offset and size. This might require changes in your code for properly supporting 64-bit systems.
Return a new read-only buffer object that reads from a specified location in memory, with a specified size. The caller is responsible for ensuring that the memory buffer, passed in as ptr, is not deallocated while the returned buffer object exists. Raises ValueError if size is less than zero. Note that Py_END_OF_BUFFER may not be passed for the size parameter; ValueError will be raised in that case.
Changed in version 2.5: This function used an int type for size. This might require changes in your code for properly supporting 64-bit systems.
Similar to PyBuffer_FromMemory(), but the returned buffer is writable.
Changed in version 2.5: This function used an int type for size. This might require changes in your code for properly supporting 64-bit systems.
Return a new writable buffer object that maintains its own memory buffer of size bytes. ValueError is returned if size is not zero or positive. Note that the memory buffer (as returned by PyObject_AsWriteBuffer()) is not specifically aligned.
Changed in version 2.5: This function used an int type for size. This might require changes in your code for properly supporting 64-bit systems.