python buffer

Solutions on MaxInterview for python buffer by the best coders in the world

showing results for - "python buffer"
Scottie
19 Jul 2016
1Python provides such a facility at the C level in the form of the buffer protocol. This protocol has two sides:
2
3    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;
4
5    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).
6
7Simple 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.
8
9An 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.
10
11There are two ways for a consumer of the buffer interface to acquire a buffer over a target object:
12
13    call PyObject_GetBuffer() with the right parameters;
14
15    call PyArg_ParseTuple() (or one of its siblings) with one of the y*, w* or s* format codes.
16
17In 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.