Buffer Protocol

Certain objects available in Python wrap access to an underlying memoryarray orbuffer. Such objects include the built-inbytes andbytearray, and some extension types likearray.array.Third-party libraries may define their own types for special purposes, suchas image processing or numeric analysis.

While each of these types have their own semantics, they share the commoncharacteristic of being backed by a possibly large memory buffer. It isthen desirable, in some situations, to access that buffer directly andwithout intermediate copying.

Python provides such a facility at the C and Python level in the form of thebuffer protocol. This protocol has two sides:

  • on the producer side, a type can export a “buffer interface” which allowsobjects of that type to expose information about their underlying buffer.This interface is described in the sectionBuffer Object Structures; forPython seeEmulating buffer types.

  • on the consumer side, several means are available to obtain a pointer tothe raw underlying data of an object (for example a method parameter). ForPython seememoryview.

Simple objects such asbytes andbytearray expose theirunderlying buffer in byte-oriented form. Other forms are possible; for example,the elements exposed by anarray.array can be multi-byte values.

An example consumer of the buffer interface is thewrite()method of file objects: any object that can export a series of bytes throughthe buffer interface can be written to a file. Whilewrite() onlyneeds read-only access to the internal contents of the object passed to it,other methods such asreadinto() need write accessto the contents of their argument. The buffer interface allows objects toselectively 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 bufferover a target object:

In both cases,PyBuffer_Release() must be called when the bufferisn’t needed anymore. Failure to do so could lead to various issues such asresource leaks.

Added in version 3.12:The buffer protocol is now accessible in Python, seeEmulating buffer types andmemoryview.

Buffer structure

Buffer structures (or simply “buffers”) are useful as a way to expose thebinary data from another object to the Python programmer. They can also beused as a zero-copy slicing mechanism. Using their ability to reference ablock of memory, it is possible to expose any data to the Python programmerquite 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 anoperating system library, or it could be used to pass around structured datain its native, in-memory format.

Contrary to most data types exposed by the Python interpreter, buffersare notPyObject pointers but rather simple C structures. Thisallows them to be created and copied very simply. When a generic wrapperaround a buffer is needed, amemoryview objectcan be created.

For short instructions how to write an exporting object, seeBuffer Object Structures. For obtaininga buffer, seePyObject_GetBuffer().

typePy_buffer
Part of theStable ABI (including all members) since version 3.11.
void*buf

A pointer to the start of the logical structure described by the bufferfields. This can be any location within the underlying physical memoryblock of the exporter. For example, with negativestridesthe value may point to the end of the memory block.

Forcontiguous arrays, the value points to the beginning ofthe memory block.

PyObject*obj

A new reference to the exporting object. The reference is owned bythe consumer and automatically released(i.e. reference count decremented)and set toNULL byPyBuffer_Release(). The field is the equivalent of the returnvalue of any standard C-API function.

As a special case, fortemporary buffers that are wrapped byPyMemoryView_FromBuffer() orPyBuffer_FillInfo()this field isNULL. In general, exporting objects MUST NOTuse this scheme.

Py_ssize_tlen

product(shape)*itemsize. For contiguous arrays, this is the lengthof the underlying memory block. For non-contiguous arrays, it is the lengththat the logical structure would have if it were copied to a contiguousrepresentation.

Accessing((char*)buf)[0]upto((char*)buf)[len-1] is only validif the buffer has been obtained by a request that guarantees contiguity. Inmost cases such a request will bePyBUF_SIMPLE orPyBUF_WRITABLE.

intreadonly

An indicator of whether the buffer is read-only. This field is controlledby thePyBUF_WRITABLE flag.

Py_ssize_titemsize

Item size in bytes of a single element. Same as the value ofstruct.calcsize()called on non-NULLformat values.

Important exception: If a consumer requests a buffer without thePyBUF_FORMAT flag,format willbe set toNULL, butitemsize still hasthe value for the original format.

Ifshape is present, the equalityproduct(shape)*itemsize==len still holds and the consumercan useitemsize to navigate the buffer.

Ifshape isNULL as a result of aPyBUF_SIMPLEor aPyBUF_WRITABLE request, the consumer must disregarditemsize and assumeitemsize==1.

char*format

ANULL terminated string instruct module style syntax describingthe contents of a single item. If this isNULL,"B" (unsigned bytes)is assumed.

This field is controlled by thePyBUF_FORMAT flag.

intndim

The number of dimensions the memory represents as an n-dimensional array.If it is0,buf points to a single item representinga scalar. In this case,shape,stridesandsuboffsets MUST beNULL.The maximum number of dimensions is given byPyBUF_MAX_NDIM.

Py_ssize_t*shape

An array ofPy_ssize_t of lengthndimindicating the shape of the memory as an n-dimensional array. Note thatshape[0]*...*shape[ndim-1]*itemsize MUST be equal tolen.

Shape values are restricted toshape[n]>=0. The caseshape[n]==0 requires special attention. Seecomplex arraysfor further information.

The shape array is read-only for the consumer.

Py_ssize_t*strides

An array ofPy_ssize_t of lengthndimgiving the number of bytes to skip to get to a new element in eachdimension.

Stride values can be any integer. For regular arrays, strides areusually positive, but a consumer MUST be able to handle the casestrides[n]<=0. Seecomplex arrays for further information.

The strides array is read-only for the consumer.

Py_ssize_t*suboffsets

An array ofPy_ssize_t of lengthndim.Ifsuboffsets[n]>=0, the values stored along the nth dimension arepointers and the suboffset value dictates how many bytes to add to eachpointer after de-referencing. A suboffset value that is negativeindicates that no de-referencing should occur (striding in a contiguousmemory block).

If all suboffsets are negative (i.e. no de-referencing is needed), thenthis field must beNULL (the default value).

This type of array representation is used by the Python Imaging Library(PIL). Seecomplex arrays for further information how to access elementsof such an array.

The suboffsets array is read-only for the consumer.

void*internal

This is for use internally by the exporting object. For example, thismight be re-cast as an integer by the exporter and used to store flagsabout whether or not the shape, strides, and suboffsets arrays must befreed when the buffer is released. The consumer MUST NOT alter thisvalue.

Constants:

PyBUF_MAX_NDIM

The maximum number of dimensions the memory represents.Exporters MUST respect this limit, consumers of multi-dimensionalbuffers SHOULD be able to handle up toPyBUF_MAX_NDIM dimensions.Currently set to 64.

Buffer request types

Buffers are usually obtained by sending a buffer request to an exportingobject viaPyObject_GetBuffer(). Since the complexity of the logicalstructure of the memory can vary drastically, the consumer uses theflagsargument to specify the exact buffer type it can handle.

AllPy_buffer fields are unambiguously defined by the requesttype.

request-independent fields

The following fields are not influenced byflags and must always be filled inwith the correct values:obj,buf,len,itemsize,ndim.

readonly, format

PyBUF_WRITABLE

Controls thereadonly field. If set, the exporterMUST provide a writable buffer or else report failure. Otherwise, theexporter MAY provide either a read-only or writable buffer, but the choiceMUST be consistent for all consumers. For example,PyBUF_SIMPLE|PyBUF_WRITABLEcan be used to request a simple writable buffer.

PyBUF_FORMAT

Controls theformat field. If set, this field MUSTbe filled in correctly. Otherwise, this field MUST beNULL.

PyBUF_WRITABLE can be |’d to any of the flags in the next section.SincePyBUF_SIMPLE is defined as 0,PyBUF_WRITABLEcan be used as a stand-alone flag to request a simple writable buffer.

PyBUF_FORMAT must be |’d to any of the flags exceptPyBUF_SIMPLE, becausethe latter already implies formatB (unsigned bytes).PyBUF_FORMAT cannot beused on its own.

shape, strides, suboffsets

The flags that control the logical structure of the memory are listedin decreasing order of complexity. Note that each flag contains all bitsof the flags below it.

Request

shape

strides

suboffsets

PyBUF_INDIRECT

yes

yes

if needed

PyBUF_STRIDES

yes

yes

NULL

PyBUF_ND

yes

NULL

NULL

PyBUF_SIMPLE

NULL

NULL

NULL

contiguity requests

C or Fortrancontiguity can be explicitly requested,with and without stride information. Without stride information, the buffermust be C-contiguous.

Request

shape

strides

suboffsets

contig

PyBUF_C_CONTIGUOUS

yes

yes

NULL

C

PyBUF_F_CONTIGUOUS

yes

yes

NULL

F

PyBUF_ANY_CONTIGUOUS

yes

yes

NULL

C or F

PyBUF_ND

yes

NULL

NULL

C

compound requests

All possible requests are fully defined by some combination of the flags inthe previous section. For convenience, the buffer protocol provides frequentlyused combinations as single flags.

In the following tableU stands for undefined contiguity. The consumer wouldhave to callPyBuffer_IsContiguous() to determine contiguity.

Request

shape

strides

suboffsets

contig

readonly

format

PyBUF_FULL

yes

yes

if needed

U

0

yes

PyBUF_FULL_RO

yes

yes

if needed

U

1 or 0

yes

PyBUF_RECORDS

yes

yes

NULL

U

0

yes

PyBUF_RECORDS_RO

yes

yes

NULL

U

1 or 0

yes

PyBUF_STRIDED

yes

yes

NULL

U

0

NULL

PyBUF_STRIDED_RO

yes

yes

NULL

U

1 or 0

NULL

PyBUF_CONTIG

yes

NULL

NULL

C

0

NULL

PyBUF_CONTIG_RO

yes

NULL

NULL

C

1 or 0

NULL

Complex arrays

NumPy-style: shape and strides

The logical structure of NumPy-style arrays is defined byitemsize,ndim,shape andstrides.

Ifndim==0, the memory location pointed to bybuf isinterpreted as a scalar of sizeitemsize. In that case,bothshape andstrides areNULL.

Ifstrides isNULL, the array is interpreted asa standard n-dimensional C-array. Otherwise, the consumer must access ann-dimensional array as follows:

ptr=(char*)buf+indices[0]*strides[0]+...+indices[n-1]*strides[n-1];item=*((typeof(item)*)ptr);

As noted above,buf can point to any location withinthe actual memory block. An exporter can check the validity of a buffer withthis function:

defverify_structure(memlen,itemsize,ndim,shape,strides,offset):"""Verify that the parameters represent a valid array within       the bounds of the allocated memory:           char *mem: start of the physical memory block           memlen: length of the physical memory block           offset: (char *)buf - mem    """ifoffset%itemsize:returnFalseifoffset<0oroffset+itemsize>memlen:returnFalseifany(v%itemsizeforvinstrides):returnFalseifndim<=0:returnndim==0andnotshapeandnotstridesif0inshape:returnTrueimin=sum(strides[j]*(shape[j]-1)forjinrange(ndim)ifstrides[j]<=0)imax=sum(strides[j]*(shape[j]-1)forjinrange(ndim)ifstrides[j]>0)return0<=offset+iminandoffset+imax+itemsize<=memlen

PIL-style: shape, strides and suboffsets

In addition to the regular items, PIL-style arrays can contain pointersthat must be followed in order to get to the next element in a dimension.For example, the regular three-dimensional C-arraycharv[2][2][3] canalso be viewed as an array of 2 pointers to 2 two-dimensional arrays:char(*v[2])[2][3]. In suboffsets representation, those two pointerscan be embedded at the start ofbuf, pointingto twocharx[2][3] arrays that can be located anywhere in memory.

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

void*get_item_pointer(intndim,void*buf,Py_ssize_t*strides,Py_ssize_t*suboffsets,Py_ssize_t*indices){char*pointer=(char*)buf;inti;for(i=0;i<ndim;i++){pointer+=strides[i]*indices[i];if(suboffsets[i]>=0){pointer=*((char**)pointer)+suboffsets[i];}}return(void*)pointer;}

Buffer-related functions

intPyObject_CheckBuffer(PyObject*obj)
Part of theStable ABI since version 3.11.

Return1 ifobj supports the buffer interface otherwise0. When1 isreturned, it doesn’t guarantee thatPyObject_GetBuffer() willsucceed. This function always succeeds.

intPyObject_GetBuffer(PyObject*exporter,Py_buffer*view,intflags)
Part of theStable ABI since version 3.11.

Send a request toexporter to fill inview as specified byflags.If the exporter cannot provide a buffer of the exact type, it MUST raiseBufferError, setview->obj toNULL andreturn-1.

On success, fill inview, setview->obj to a new referencetoexporter and return 0. In the case of chained buffer providersthat redirect requests to a single object,view->obj MAYrefer to this object instead ofexporter (SeeBuffer Object Structures).

Successful calls toPyObject_GetBuffer() must be paired with callstoPyBuffer_Release(), similar tomalloc() andfree().Thus, after the consumer is done with the buffer,PyBuffer_Release()must be called exactly once.

voidPyBuffer_Release(Py_buffer*view)
Part of theStable ABI since version 3.11.

Release the bufferview and release thestrong reference(i.e. decrement the reference count) to the view’s supporting object,view->obj. This function MUST be called when the bufferis no longer being used, otherwise reference leaks may occur.

It is an error to call this function on a buffer that was not obtained viaPyObject_GetBuffer().

Py_ssize_tPyBuffer_SizeFromFormat(constchar*format)
Part of theStable ABI since version 3.11.

Return the implieditemsize fromformat.On error, raise an exception and return -1.

Added in version 3.9.

intPyBuffer_IsContiguous(constPy_buffer*view,charorder)
Part of theStable ABI since version 3.11.

Return1 if the memory defined by theview is C-style (order is'C') or Fortran-style (order is'F')contiguous or either one(order is'A'). Return0 otherwise. This function always succeeds.

void*PyBuffer_GetPointer(constPy_buffer*view,constPy_ssize_t*indices)
Part of theStable ABI since version 3.11.

Get the memory area pointed to by theindices inside the givenview.indices must point to an array ofview->ndim indices.

intPyBuffer_FromContiguous(constPy_buffer*view,constvoid*buf,Py_ssize_tlen,charfort)
Part of theStable ABI since version 3.11.

Copy contiguouslen bytes frombuf toview.fort can be'C' or'F' (for C-style or Fortran-style ordering).0 is returned on success,-1 on error.

intPyBuffer_ToContiguous(void*buf,constPy_buffer*src,Py_ssize_tlen,charorder)
Part of theStable ABI since version 3.11.

Copylen bytes fromsrc to its contiguous representation inbuf.order can be'C' or'F' or'A' (for C-style or Fortran-styleordering or either one).0 is returned on success,-1 on error.

This function fails iflen !=src->len.

intPyObject_CopyData(PyObject*dest,PyObject*src)
Part of theStable ABI since version 3.11.

Copy data fromsrc todest buffer. Can convert between C-style andor Fortran-style buffers.

0 is returned on success,-1 on error.

voidPyBuffer_FillContiguousStrides(intndims,Py_ssize_t*shape,Py_ssize_t*strides,intitemsize,charorder)
Part of theStable ABI since version 3.11.

Fill thestrides array with byte-strides of acontiguous (C-style iforder is'C' or Fortran-style iforder is'F') array of thegiven shape with the given number of bytes per element.

intPyBuffer_FillInfo(Py_buffer*view,PyObject*exporter,void*buf,Py_ssize_tlen,intreadonly,intflags)
Part of theStable ABI since version 3.11.

Handle buffer requests for an exporter that wants to exposebuf of sizelenwith writability set according toreadonly.buf is interpreted as a sequenceof unsigned bytes.

Theflags argument indicates the request type. This function always fills inview as specified by flags, unlessbuf has been designated as read-onlyandPyBUF_WRITABLE is set inflags.

On success, setview->obj to a new reference toexporter andreturn 0. Otherwise, raiseBufferError, setview->obj toNULL and return-1;

If this function is used as part of agetbufferproc,exporter MUST be set to the exporting object andflags must be passedunmodified. Otherwise,exporter MUST beNULL.