blob: 64e836047824a54292ef80583667bf9aeee6dc85 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _bufferobjects:
4
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +00005Buffer Protocol
6---------------
Georg Brandl54a3faa2008-01-20 09:30:57 +00007
8.. sectionauthor:: Greg Stein <gstein@lyra.org>
Benjamin Peterson9d0ced32008-09-16 02:24:31 +00009.. sectionauthor:: Benjamin Peterson
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
11
12.. index::
Georg Brandl54a3faa2008-01-20 09:30:57 +000013 single: buffer interface
14
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000015Python objects implemented in C can export a "buffer interface." These
16functions can be used by an object to expose its data in a raw, byte-oriented
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000017format. Clients of the object can use the buffer interface to access the
18object data directly, without needing to copy it first.
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
Antoine Pitrou99a00a42010-09-28 23:04:04 +000020Examples of objects that support the buffer interface are :class:`bytes`,
21:class:`bytearray` and :class:`array.array`. The bytes and bytearray objects
22exposes their bytes contents in the buffer interface's byte-oriented form.
23An :class:`array.array` can also expose its contents, but it should be noted
24that array elements may be multi-byte values.
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
Antoine Pitrou99a00a42010-09-28 23:04:04 +000026An example consumer of the buffer interface is the :meth:`~io.BufferedIOBase.write`
27method of file objects: any object that can export a series of bytes through
28the buffer interface can be written to a file. While :meth:`write` only
29needs read-only access to the internal contents of the object passed to it,
30other methods such as :meth:`~io.BufferedIOBase.readinto` need write access
31to the contents of their argument. The buffer interface allows objects to
32selectively allow or reject exporting of read-write and read-only buffers.
33
34There are two ways for a consumer of the buffer interface to acquire a buffer
35over a target object:
36
Georg Brandl60203b42010-10-06 10:11:56 +000037* call :c:func:`PyObject_GetBuffer` with the right parameters;
Antoine Pitrou99a00a42010-09-28 23:04:04 +000038
Georg Brandl60203b42010-10-06 10:11:56 +000039* call :c:func:`PyArg_ParseTuple` (or one of its siblings) with one of the
Antoine Pitrou99a00a42010-09-28 23:04:04 +000040 ``y*``, ``w*`` or ``s*`` :ref:`format codes <arg-parsing>`.
41
Georg Brandl60203b42010-10-06 10:11:56 +000042In both cases, :c:func:`PyBuffer_Release` must be called when the buffer
Antoine Pitrou99a00a42010-09-28 23:04:04 +000043isn't needed anymore. Failure to do so could lead to various issues such as
44resource leaks.
45
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
47.. index:: single: PyBufferProcs
48
Antoine Pitrou99a00a42010-09-28 23:04:04 +000049How the buffer interface is exposed by a type object is described in the
Georg Brandl60203b42010-10-06 10:11:56 +000050section :ref:`buffer-structs`, under the description for :c:type:`PyBufferProcs`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000051
Antoine Pitrou99a00a42010-09-28 23:04:04 +000052
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000053The buffer structure
54====================
Antoine Pitrou99a00a42010-09-28 23:04:04 +000055
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000056Buffer structures (or simply "buffers") are useful as a way to expose the
57binary data from another object to the Python programmer. They can also be
58used as a zero-copy slicing mechanism. Using their ability to reference a
59block of memory, it is possible to expose any data to the Python programmer
60quite easily. The memory could be a large, constant array in a C extension,
61it could be a raw block of memory for manipulation before passing to an
62operating system library, or it could be used to pass around structured data
63in its native, in-memory format.
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000065Contrary to most data types exposed by the Python interpreter, buffers
Georg Brandl60203b42010-10-06 10:11:56 +000066are not :c:type:`PyObject` pointers but rather simple C structures. This
Antoine Pitrou99a00a42010-09-28 23:04:04 +000067allows them to be created and copied very simply. When a generic wrapper
Antoine Pitrouc663b582010-09-28 23:59:51 +000068around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object
Antoine Pitrou99a00a42010-09-28 23:04:04 +000069can be created.
70
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
Georg Brandl60203b42010-10-06 10:11:56 +000072.. c:type:: Py_buffer
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Georg Brandl60203b42010-10-06 10:11:56 +000074 .. c:member:: void *buf
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000075
76 A pointer to the start of the memory for the object.
77
Georg Brandl60203b42010-10-06 10:11:56 +000078 .. c:member:: Py_ssize_t len
Benjamin Petersonf2fa87b2008-09-17 22:59:21 +000079 :noindex:
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000080
81 The total length of the memory in bytes.
82
Georg Brandl60203b42010-10-06 10:11:56 +000083 .. c:member:: int readonly
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000084
85 An indicator of whether the buffer is read only.
86
Georg Brandl60203b42010-10-06 10:11:56 +000087 .. c:member:: const char *format
Benjamin Petersonf2fa87b2008-09-17 22:59:21 +000088 :noindex:
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000089
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000090 A *NULL* terminated string in :mod:`struct` module style syntax giving
91 the contents of the elements available through the buffer. If this is
92 *NULL*, ``"B"`` (unsigned bytes) is assumed.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000093
Georg Brandl60203b42010-10-06 10:11:56 +000094 .. c:member:: int ndim
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000095
96 The number of dimensions the memory represents as a multi-dimensional
Georg Brandl60203b42010-10-06 10:11:56 +000097 array. If it is 0, :c:data:`strides` and :c:data:`suboffsets` must be
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000098 *NULL*.
99
Georg Brandl60203b42010-10-06 10:11:56 +0000100 .. c:member:: Py_ssize_t *shape
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000101
Georg Brandl60203b42010-10-06 10:11:56 +0000102 An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000103 shape of the memory as a multi-dimensional array. Note that
104 ``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to
Georg Brandl60203b42010-10-06 10:11:56 +0000105 :c:data:`len`.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000106
Georg Brandl60203b42010-10-06 10:11:56 +0000107 .. c:member:: Py_ssize_t *strides
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000108
Georg Brandl60203b42010-10-06 10:11:56 +0000109 An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000110 number of bytes to skip to get to a new element in each dimension.
111
Georg Brandl60203b42010-10-06 10:11:56 +0000112 .. c:member:: Py_ssize_t *suboffsets
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000113
Georg Brandl60203b42010-10-06 10:11:56 +0000114 An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim`. If these
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000115 suboffset numbers are greater than or equal to 0, then the value stored
116 along the indicated dimension is a pointer and the suboffset value
117 dictates how many bytes to add to the pointer after de-referencing. A
118 suboffset value that it negative indicates that no de-referencing should
119 occur (striding in a contiguous memory block).
120
121 Here is a function that returns a pointer to the element in an N-D array
Georg Brandlae2dbe22009-03-13 19:04:40 +0000122 pointed to by an N-dimensional index when there are both non-NULL strides
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000123 and suboffsets::
124
125 void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
126 Py_ssize_t *suboffsets, Py_ssize_t *indices) {
127 char *pointer = (char*)buf;
128 int i;
129 for (i = 0; i < ndim; i++) {
130 pointer += strides[i] * indices[i];
131 if (suboffsets[i] >=0 ) {
132 pointer = *((char**)pointer) + suboffsets[i];
Georg Brandl48310cd2009-01-03 21:18:54 +0000133 }
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000134 }
135 return (void*)pointer;
136 }
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137
138
Georg Brandl60203b42010-10-06 10:11:56 +0000139 .. c:member:: Py_ssize_t itemsize
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000141 This is a storage for the itemsize (in bytes) of each element of the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000142 shared memory. It is technically un-necessary as it can be obtained
Georg Brandl60203b42010-10-06 10:11:56 +0000143 using :c:func:`PyBuffer_SizeFromFormat`, however an exporter may know
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000144 this information without parsing the format string and it is necessary
145 to know the itemsize for proper interpretation of striding. Therefore,
146 storing it is more convenient and faster.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000147
Georg Brandl60203b42010-10-06 10:11:56 +0000148 .. c:member:: void *internal
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000149
150 This is for use internally by the exporting object. For example, this
151 might be re-cast as an integer by the exporter and used to store flags
152 about whether or not the shape, strides, and suboffsets arrays must be
153 freed when the buffer is released. The consumer should never alter this
154 value.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000155
156
Antoine Pitrouc663b582010-09-28 23:59:51 +0000157Buffer-related functions
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000158========================
Georg Brandl54a3faa2008-01-20 09:30:57 +0000159
160
Georg Brandl60203b42010-10-06 10:11:56 +0000161.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000162
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000163 Return 1 if *obj* supports the buffer interface otherwise 0. When 1 is
Georg Brandl60203b42010-10-06 10:11:56 +0000164 returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000165 succeed.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000166
167
Georg Brandl60203b42010-10-06 10:11:56 +0000168.. c:function:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000169
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000170 Export a view over some internal data from the target object *obj*.
171 *obj* must not be NULL, and *view* must point to an existing
Georg Brandl60203b42010-10-06 10:11:56 +0000172 :c:type:`Py_buffer` structure allocated by the caller (most uses of
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000173 this function will simply declare a local variable of type
Georg Brandl60203b42010-10-06 10:11:56 +0000174 :c:type:`Py_buffer`). The *flags* argument is a bit field indicating
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000175 what kind of buffer is requested. The buffer interface allows
176 for complicated memory layout possibilities; however, some callers
177 won't want to handle all the complexity and instead request a simple
Georg Brandl60203b42010-10-06 10:11:56 +0000178 view of the target object (using :c:macro:`PyBUF_SIMPLE` for a read-only
179 view and :c:macro:`PyBUF_WRITABLE` for a read-write view).
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000180
181 Some exporters may not be able to share memory in every possible way and
182 may need to raise errors to signal to some consumers that something is
183 just not possible. These errors should be a :exc:`BufferError` unless
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000184 there is another error that is actually causing the problem. The
185 exporter can use flags information to simplify how much of the
Georg Brandl60203b42010-10-06 10:11:56 +0000186 :c:data:`Py_buffer` structure is filled in with non-default values and/or
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000187 raise an error if the object can't support a simpler view of its memory.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000188
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000189 On success, 0 is returned and the *view* structure is filled with useful
190 values. On error, -1 is returned and an exception is raised; the *view*
191 is left in an undefined state.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000192
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000193 The following are the possible values to the *flags* arguments.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000194
Georg Brandl60203b42010-10-06 10:11:56 +0000195 .. c:macro:: PyBUF_SIMPLE
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000196
197 This is the default flag. The returned buffer exposes a read-only
198 memory area. The format of data is assumed to be raw unsigned bytes,
199 without any particular structure. This is a "stand-alone" flag
200 constant. It never needs to be '|'d to the others. The exporter will
201 raise an error if it cannot provide such a contiguous buffer of bytes.
202
Georg Brandl60203b42010-10-06 10:11:56 +0000203 .. c:macro:: PyBUF_WRITABLE
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000204
Georg Brandl60203b42010-10-06 10:11:56 +0000205 Like :c:macro:`PyBUF_SIMPLE`, but the returned buffer is writable. If
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000206 the exporter doesn't support writable buffers, an error is raised.
207
Georg Brandl60203b42010-10-06 10:11:56 +0000208 .. c:macro:: PyBUF_STRIDES
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000209
Georg Brandl60203b42010-10-06 10:11:56 +0000210 This implies :c:macro:`PyBUF_ND`. The returned buffer must provide
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000211 strides information (i.e. the strides cannot be NULL). This would be
212 used when the consumer can handle strided, discontiguous arrays.
213 Handling strides automatically assumes you can handle shape. The
214 exporter can raise an error if a strided representation of the data is
215 not possible (i.e. without the suboffsets).
216
Georg Brandl60203b42010-10-06 10:11:56 +0000217 .. c:macro:: PyBUF_ND
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000218
219 The returned buffer must provide shape information. The memory will be
220 assumed C-style contiguous (last dimension varies the fastest). The
221 exporter may raise an error if it cannot provide this kind of
222 contiguous buffer. If this is not given then shape will be *NULL*.
223
Georg Brandl60203b42010-10-06 10:11:56 +0000224 .. c:macro:: PyBUF_C_CONTIGUOUS
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000225 PyBUF_F_CONTIGUOUS
226 PyBUF_ANY_CONTIGUOUS
227
228 These flags indicate that the contiguity returned buffer must be
229 respectively, C-contiguous (last dimension varies the fastest), Fortran
230 contiguous (first dimension varies the fastest) or either one. All of
Georg Brandl60203b42010-10-06 10:11:56 +0000231 these flags imply :c:macro:`PyBUF_STRIDES` and guarantee that the
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000232 strides buffer info structure will be filled in correctly.
233
Georg Brandl60203b42010-10-06 10:11:56 +0000234 .. c:macro:: PyBUF_INDIRECT
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000235
236 This flag indicates the returned buffer must have suboffsets
237 information (which can be NULL if no suboffsets are needed). This can
238 be used when the consumer can handle indirect array referencing implied
Georg Brandl60203b42010-10-06 10:11:56 +0000239 by these suboffsets. This implies :c:macro:`PyBUF_STRIDES`.
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000240
Georg Brandl60203b42010-10-06 10:11:56 +0000241 .. c:macro:: PyBUF_FORMAT
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000242
243 The returned buffer must have true format information if this flag is
244 provided. This would be used when the consumer is going to be checking
245 for what 'kind' of data is actually stored. An exporter should always
246 be able to provide this information if requested. If format is not
247 explicitly requested then the format must be returned as *NULL* (which
248 means ``'B'``, or unsigned bytes).
249
Georg Brandl60203b42010-10-06 10:11:56 +0000250 .. c:macro:: PyBUF_STRIDED
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000251
252 This is equivalent to ``(PyBUF_STRIDES | PyBUF_WRITABLE)``.
253
Georg Brandl60203b42010-10-06 10:11:56 +0000254 .. c:macro:: PyBUF_STRIDED_RO
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000255
256 This is equivalent to ``(PyBUF_STRIDES)``.
257
Georg Brandl60203b42010-10-06 10:11:56 +0000258 .. c:macro:: PyBUF_RECORDS
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000259
260 This is equivalent to ``(PyBUF_STRIDES | PyBUF_FORMAT |
261 PyBUF_WRITABLE)``.
262
Georg Brandl60203b42010-10-06 10:11:56 +0000263 .. c:macro:: PyBUF_RECORDS_RO
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000264
265 This is equivalent to ``(PyBUF_STRIDES | PyBUF_FORMAT)``.
266
Georg Brandl60203b42010-10-06 10:11:56 +0000267 .. c:macro:: PyBUF_FULL
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000268
269 This is equivalent to ``(PyBUF_INDIRECT | PyBUF_FORMAT |
270 PyBUF_WRITABLE)``.
271
Georg Brandl60203b42010-10-06 10:11:56 +0000272 .. c:macro:: PyBUF_FULL_RO
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000273
274 This is equivalent to ``(PyBUF_INDIRECT | PyBUF_FORMAT)``.
275
Georg Brandl60203b42010-10-06 10:11:56 +0000276 .. c:macro:: PyBUF_CONTIG
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000277
278 This is equivalent to ``(PyBUF_ND | PyBUF_WRITABLE)``.
279
Georg Brandl60203b42010-10-06 10:11:56 +0000280 .. c:macro:: PyBUF_CONTIG_RO
Georg Brandld0ffa4c2010-10-01 05:38:10 +0000281
282 This is equivalent to ``(PyBUF_ND)``.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000283
284
Georg Brandl60203b42010-10-06 10:11:56 +0000285.. c:function:: void PyBuffer_Release(Py_buffer *view)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000286
Brian Curtin1fbd36b2010-06-08 22:27:07 +0000287 Release the buffer *view*. This should be called when the buffer is no
288 longer being used as it may free memory from it.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000289
290
Georg Brandl60203b42010-10-06 10:11:56 +0000291.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000292
Georg Brandl60203b42010-10-06 10:11:56 +0000293 Return the implied :c:data:`~Py_buffer.itemsize` from the struct-stype
294 :c:data:`~Py_buffer.format`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000295
296
Georg Brandl60203b42010-10-06 10:11:56 +0000297.. c:function:: int PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fortran)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000298
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000299 Copy *len* bytes of data pointed to by the contiguous chunk of memory
300 pointed to by *buf* into the buffer exported by obj. The buffer must of
301 course be writable. Return 0 on success and return -1 and raise an error
302 on failure. If the object does not have a writable buffer, then an error
303 is raised. If *fortran* is ``'F'``, then if the object is
304 multi-dimensional, then the data will be copied into the array in
305 Fortran-style (first dimension varies the fastest). If *fortran* is
306 ``'C'``, then the data will be copied into the array in C-style (last
307 dimension varies the fastest). If *fortran* is ``'A'``, then it does not
308 matter and the copy will be made in whatever way is more efficient.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000309
310
Georg Brandl60203b42010-10-06 10:11:56 +0000311.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000312
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000313 Return 1 if the memory defined by the *view* is C-style (*fortran* is
314 ``'C'``) or Fortran-style (*fortran* is ``'F'``) contiguous or either one
315 (*fortran* is ``'A'``). Return 0 otherwise.
316
317
Georg Brandl60203b42010-10-06 10:11:56 +0000318.. c:function:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran)
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000319
320 Fill the *strides* array with byte-strides of a contiguous (C-style if
321 *fortran* is ``'C'`` or Fortran-style if *fortran* is ``'F'`` array of the
322 given shape with the given number of bytes per element.
323
324
Georg Brandl60203b42010-10-06 10:11:56 +0000325.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000326
327 Fill in a buffer-info structure, *view*, correctly for an exporter that can
328 only share a contiguous chunk of memory of "unsigned bytes" of the given
329 length. Return 0 on success and -1 (with raising an error) on error.
330