Buffer Protocol — Python 3.9.25 documentation

docs.python.org

Tin mới

Old Buffer Protocol

Python documentation for the current stable release

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

Python Module Index

3.9.25 Documentation

Python/C API Reference Manual

Abstract Objects Layer

Buffer Object Structures

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;

io.BufferedIOBase.write

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

PyArg_ParseTuple()

call PyArg_ParseTuple() (or one of its siblings) with one of the y*, w* or s* format codes.

PyMemoryView_FromBuffer()

A new reference to the exporting object. The reference is owned by the consumer and automatically decremented and set to NULL by PyBuffer_Release(). The field is the equivalent of the return value of any standard C-API f

struct: Interpret bytes as packed binary data.

A NUL terminated string in struct module style syntax describing the contents of a single item. If this is NULL, "B" (unsigned bytes) is assumed.

contiguity requests¶

C or Fortran contiguity can be explicitly requested, with and without stride information. Without stride information, the buffer must be C-contiguous.