blob: fc1430efa8a4bb76df0afff8732512e81869ec43 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08003.. index::
4 single: buffer protocol
5 single: buffer interface; (see buffer protocol)
6 single: buffer object; (see buffer protocol)
7
Georg Brandl54a3faa2008-01-20 09:30:57 +00008.. _bufferobjects:
9
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000010Buffer Protocol
11---------------
Georg Brandl54a3faa2008-01-20 09:30:57 +000012
13.. sectionauthor:: Greg Stein <gstein@lyra.org>
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000014.. sectionauthor:: Benjamin Peterson
Stefan Krah9a2d99e2012-02-25 12:24:21 +010015.. sectionauthor:: Stefan Krah
Georg Brandl54a3faa2008-01-20 09:30:57 +000016
17
Antoine Pitrou8abc9352010-12-12 19:59:47 +000018Certain objects available in Python wrap access to an underlying memory
19array or *buffer*. Such objects include the built-in :class:`bytes` and
20:class:`bytearray`, and some extension types like :class:`array.array`.
21Third-party libraries may define their own types for special purposes, such
22as image processing or numeric analysis.
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
Antoine Pitrou8abc9352010-12-12 19:59:47 +000024While each of these types have their own semantics, they share the common
25characteristic of being backed by a possibly large memory buffer. It is
Stefan Krah9a2d99e2012-02-25 12:24:21 +010026then desirable, in some situations, to access that buffer directly and
Antoine Pitrou8abc9352010-12-12 19:59:47 +000027without intermediate copying.
28
Chris Jerdonek5fae0e52012-11-20 17:45:51 -080029Python provides such a facility at the C level in the form of the :ref:`buffer
30protocol <bufferobjects>`. This protocol has two sides:
Antoine Pitrou8abc9352010-12-12 19:59:47 +000031
32.. index:: single: PyBufferProcs
33
34- on the producer side, a type can export a "buffer interface" which allows
35 objects of that type to expose information about their underlying buffer.
36 This interface is described in the section :ref:`buffer-structs`;
37
38- on the consumer side, several means are available to obtain a pointer to
39 the raw underlying data of an object (for example a method parameter).
40
41Simple objects such as :class:`bytes` and :class:`bytearray` expose their
42underlying buffer in byte-oriented form. Other forms are possible; for example,
Martin Panter7462b6492015-11-02 03:37:02 +000043the elements exposed by an :class:`array.array` can be multi-byte values.
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
Antoine Pitrou99a00a42010-09-28 23:04:04 +000045An example consumer of the buffer interface is the :meth:`~io.BufferedIOBase.write`
46method of file objects: any object that can export a series of bytes through
47the buffer interface can be written to a file. While :meth:`write` only
48needs read-only access to the internal contents of the object passed to it,
49other methods such as :meth:`~io.BufferedIOBase.readinto` need write access
50to the contents of their argument. The buffer interface allows objects to
51selectively allow or reject exporting of read-write and read-only buffers.
52
53There are two ways for a consumer of the buffer interface to acquire a buffer
54over a target object:
55
Georg Brandl60203b42010-10-06 10:11:56 +000056* call :c:func:`PyObject_GetBuffer` with the right parameters;
Antoine Pitrou99a00a42010-09-28 23:04:04 +000057
Georg Brandl60203b42010-10-06 10:11:56 +000058* call :c:func:`PyArg_ParseTuple` (or one of its siblings) with one of the
Antoine Pitrou99a00a42010-09-28 23:04:04 +000059 ``y*``, ``w*`` or ``s*`` :ref:`format codes <arg-parsing>`.
60
Georg Brandl60203b42010-10-06 10:11:56 +000061In both cases, :c:func:`PyBuffer_Release` must be called when the buffer
Antoine Pitrou99a00a42010-09-28 23:04:04 +000062isn't needed anymore. Failure to do so could lead to various issues such as
63resource leaks.
64
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
Stefan Krah9a2d99e2012-02-25 12:24:21 +010066.. _buffer-structure:
67
68Buffer structure
69================
Antoine Pitrou99a00a42010-09-28 23:04:04 +000070
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000071Buffer structures (or simply "buffers") are useful as a way to expose the
72binary data from another object to the Python programmer. They can also be
73used as a zero-copy slicing mechanism. Using their ability to reference a
74block of memory, it is possible to expose any data to the Python programmer
75quite easily. The memory could be a large, constant array in a C extension,
76it could be a raw block of memory for manipulation before passing to an
77operating system library, or it could be used to pass around structured data
78in its native, in-memory format.
Georg Brandl54a3faa2008-01-20 09:30:57 +000079
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000080Contrary to most data types exposed by the Python interpreter, buffers
Georg Brandl60203b42010-10-06 10:11:56 +000081are not :c:type:`PyObject` pointers but rather simple C structures. This
Antoine Pitrou99a00a42010-09-28 23:04:04 +000082allows them to be created and copied very simply. When a generic wrapper
Antoine Pitrouc663b582010-09-28 23:59:51 +000083around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object
Antoine Pitrou99a00a42010-09-28 23:04:04 +000084can be created.
85
Stefan Krahabd887d2012-03-06 14:55:06 +010086For short instructions how to write an exporting object, see
87:ref:`Buffer Object Structures <buffer-structs>`. For obtaining
88a buffer, see :c:func:`PyObject_GetBuffer`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
Georg Brandl60203b42010-10-06 10:11:56 +000090.. c:type:: Py_buffer
Georg Brandl54a3faa2008-01-20 09:30:57 +000091
Jesus Ceae8ef8b72014-06-25 05:37:17 +020092 .. c:member:: void \*buf
93
94 A pointer to the start of the logical structure described by the buffer
95 fields. This can be any location within the underlying physical memory
96 block of the exporter. For example, with negative :c:member:`~Py_buffer.strides`
97 the value may point to the end of the memory block.
98
Stefan Krah70e543b2015-08-08 14:33:28 +020099 For :term:`contiguous` arrays, the value points to the beginning of
100 the memory block.
Jesus Ceae8ef8b72014-06-25 05:37:17 +0200101
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100102 .. c:member:: void \*obj
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000103
Stefan Krahabd887d2012-03-06 14:55:06 +0100104 A new reference to the exporting object. The reference is owned by
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200105 the consumer and automatically decremented and set to ``NULL`` by
Stefan Krahabd887d2012-03-06 14:55:06 +0100106 :c:func:`PyBuffer_Release`. The field is the equivalent of the return
107 value of any standard C-API function.
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100108
Stefan Krahabd887d2012-03-06 14:55:06 +0100109 As a special case, for *temporary* buffers that are wrapped by
110 :c:func:`PyMemoryView_FromBuffer` or :c:func:`PyBuffer_FillInfo`
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200111 this field is ``NULL``. In general, exporting objects MUST NOT
Stefan Krahabd887d2012-03-06 14:55:06 +0100112 use this scheme.
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100113
Georg Brandl60203b42010-10-06 10:11:56 +0000114 .. c:member:: Py_ssize_t len
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000115
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100116 ``product(shape) * itemsize``. For contiguous arrays, this is the length
117 of the underlying memory block. For non-contiguous arrays, it is the length
118 that the logical structure would have if it were copied to a contiguous
119 representation.
120
121 Accessing ``((char *)buf)[0] up to ((char *)buf)[len-1]`` is only valid
122 if the buffer has been obtained by a request that guarantees contiguity. In
123 most cases such a request will be :c:macro:`PyBUF_SIMPLE` or :c:macro:`PyBUF_WRITABLE`.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000124
Georg Brandl60203b42010-10-06 10:11:56 +0000125 .. c:member:: int readonly
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000126
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100127 An indicator of whether the buffer is read-only. This field is controlled
128 by the :c:macro:`PyBUF_WRITABLE` flag.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
Georg Brandl60203b42010-10-06 10:11:56 +0000130 .. c:member:: Py_ssize_t itemsize
Georg Brandl54a3faa2008-01-20 09:30:57 +0000131
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100132 Item size in bytes of a single element. Same as the value of :func:`struct.calcsize`
Serhiy Storchakae835b312019-10-30 21:37:16 +0200133 called on non-``NULL`` :c:member:`~Py_buffer.format` values.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000134
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100135 Important exception: If a consumer requests a buffer without the
Benjamin Peterson2053aa12015-08-17 23:38:34 -0700136 :c:macro:`PyBUF_FORMAT` flag, :c:member:`~Py_buffer.format` will
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200137 be set to ``NULL``, but :c:member:`~Py_buffer.itemsize` still has
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100138 the value for the original format.
139
Benjamin Peterson2053aa12015-08-17 23:38:34 -0700140 If :c:member:`~Py_buffer.shape` is present, the equality
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100141 ``product(shape) * itemsize == len`` still holds and the consumer
142 can use :c:member:`~Py_buffer.itemsize` to navigate the buffer.
143
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200144 If :c:member:`~Py_buffer.shape` is ``NULL`` as a result of a :c:macro:`PyBUF_SIMPLE`
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100145 or a :c:macro:`PyBUF_WRITABLE` request, the consumer must disregard
146 :c:member:`~Py_buffer.itemsize` and assume ``itemsize == 1``.
147
148 .. c:member:: const char \*format
149
150 A *NUL* terminated string in :mod:`struct` module style syntax describing
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200151 the contents of a single item. If this is ``NULL``, ``"B"`` (unsigned bytes)
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100152 is assumed.
153
154 This field is controlled by the :c:macro:`PyBUF_FORMAT` flag.
155
156 .. c:member:: int ndim
157
158 The number of dimensions the memory represents as an n-dimensional array.
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300159 If it is ``0``, :c:member:`~Py_buffer.buf` points to a single item representing
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100160 a scalar. In this case, :c:member:`~Py_buffer.shape`, :c:member:`~Py_buffer.strides`
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200161 and :c:member:`~Py_buffer.suboffsets` MUST be ``NULL``.
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100162
163 The macro :c:macro:`PyBUF_MAX_NDIM` limits the maximum number of dimensions
164 to 64. Exporters MUST respect this limit, consumers of multi-dimensional
165 buffers SHOULD be able to handle up to :c:macro:`PyBUF_MAX_NDIM` dimensions.
166
167 .. c:member:: Py_ssize_t \*shape
168
169 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
170 indicating the shape of the memory as an n-dimensional array. Note that
171 ``shape[0] * ... * shape[ndim-1] * itemsize`` MUST be equal to
172 :c:member:`~Py_buffer.len`.
173
174 Shape values are restricted to ``shape[n] >= 0``. The case
175 ``shape[n] == 0`` requires special attention. See `complex arrays`_
176 for further information.
177
178 The shape array is read-only for the consumer.
179
180 .. c:member:: Py_ssize_t \*strides
181
182 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
183 giving the number of bytes to skip to get to a new element in each
184 dimension.
185
186 Stride values can be any integer. For regular arrays, strides are
187 usually positive, but a consumer MUST be able to handle the case
188 ``strides[n] <= 0``. See `complex arrays`_ for further information.
189
190 The strides array is read-only for the consumer.
191
192 .. c:member:: Py_ssize_t \*suboffsets
193
194 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`.
195 If ``suboffsets[n] >= 0``, the values stored along the nth dimension are
196 pointers and the suboffset value dictates how many bytes to add to each
197 pointer after de-referencing. A suboffset value that is negative
198 indicates that no de-referencing should occur (striding in a contiguous
199 memory block).
200
Andre Delfino55f41e42018-12-05 16:45:30 -0300201 If all suboffsets are negative (i.e. no de-referencing is needed), then
Serhiy Storchakae835b312019-10-30 21:37:16 +0200202 this field must be ``NULL`` (the default value).
Stefan Krah0dc4e152015-02-01 19:42:12 +0100203
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100204 This type of array representation is used by the Python Imaging Library
205 (PIL). See `complex arrays`_ for further information how to access elements
206 of such an array.
207
208 The suboffsets array is read-only for the consumer.
209
210 .. c:member:: void \*internal
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000211
212 This is for use internally by the exporting object. For example, this
213 might be re-cast as an integer by the exporter and used to store flags
214 about whether or not the shape, strides, and suboffsets arrays must be
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100215 freed when the buffer is released. The consumer MUST NOT alter this
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000216 value.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000217
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100218.. _buffer-request-types:
219
220Buffer request types
221====================
222
223Buffers are usually obtained by sending a buffer request to an exporting
224object via :c:func:`PyObject_GetBuffer`. Since the complexity of the logical
225structure of the memory can vary drastically, the consumer uses the *flags*
226argument to specify the exact buffer type it can handle.
227
228All :c:data:`Py_buffer` fields are unambiguously defined by the request
229type.
230
231request-independent fields
232~~~~~~~~~~~~~~~~~~~~~~~~~~
233The following fields are not influenced by *flags* and must always be filled in
234with the correct values: :c:member:`~Py_buffer.obj`, :c:member:`~Py_buffer.buf`,
235:c:member:`~Py_buffer.len`, :c:member:`~Py_buffer.itemsize`, :c:member:`~Py_buffer.ndim`.
236
237
238readonly, format
239~~~~~~~~~~~~~~~~
240
241 .. c:macro:: PyBUF_WRITABLE
242
243 Controls the :c:member:`~Py_buffer.readonly` field. If set, the exporter
244 MUST provide a writable buffer or else report failure. Otherwise, the
245 exporter MAY provide either a read-only or writable buffer, but the choice
246 MUST be consistent for all consumers.
247
248 .. c:macro:: PyBUF_FORMAT
249
250 Controls the :c:member:`~Py_buffer.format` field. If set, this field MUST
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200251 be filled in correctly. Otherwise, this field MUST be ``NULL``.
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100252
253
254:c:macro:`PyBUF_WRITABLE` can be \|'d to any of the flags in the next section.
255Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE`
256can be used as a stand-alone flag to request a simple writable buffer.
257
258:c:macro:`PyBUF_FORMAT` can be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`.
259The latter already implies format ``B`` (unsigned bytes).
260
261
262shape, strides, suboffsets
263~~~~~~~~~~~~~~~~~~~~~~~~~~
264
265The flags that control the logical structure of the memory are listed
266in decreasing order of complexity. Note that each flag contains all bits
267of the flags below it.
268
Georg Brandl44ea77b2013-03-28 13:28:44 +0100269.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100270
271+-----------------------------+-------+---------+------------+
272| Request | shape | strides | suboffsets |
273+=============================+=======+=========+============+
274| .. c:macro:: PyBUF_INDIRECT | yes | yes | if needed |
275+-----------------------------+-------+---------+------------+
276| .. c:macro:: PyBUF_STRIDES | yes | yes | NULL |
277+-----------------------------+-------+---------+------------+
278| .. c:macro:: PyBUF_ND | yes | NULL | NULL |
279+-----------------------------+-------+---------+------------+
280| .. c:macro:: PyBUF_SIMPLE | NULL | NULL | NULL |
281+-----------------------------+-------+---------+------------+
282
283
Stefan Krah70e543b2015-08-08 14:33:28 +0200284.. index:: contiguous, C-contiguous, Fortran contiguous
285
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100286contiguity requests
287~~~~~~~~~~~~~~~~~~~
288
Stefan Krah70e543b2015-08-08 14:33:28 +0200289C or Fortran :term:`contiguity <contiguous>` can be explicitly requested,
290with and without stride information. Without stride information, the buffer
291must be C-contiguous.
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100292
Georg Brandl44ea77b2013-03-28 13:28:44 +0100293.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|
294
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100295+-----------------------------------+-------+---------+------------+--------+
296| Request | shape | strides | suboffsets | contig |
297+===================================+=======+=========+============+========+
298| .. c:macro:: PyBUF_C_CONTIGUOUS | yes | yes | NULL | C |
299+-----------------------------------+-------+---------+------------+--------+
300| .. c:macro:: PyBUF_F_CONTIGUOUS | yes | yes | NULL | F |
301+-----------------------------------+-------+---------+------------+--------+
302| .. c:macro:: PyBUF_ANY_CONTIGUOUS | yes | yes | NULL | C or F |
303+-----------------------------------+-------+---------+------------+--------+
304| .. c:macro:: PyBUF_ND | yes | NULL | NULL | C |
305+-----------------------------------+-------+---------+------------+--------+
306
307
308compound requests
309~~~~~~~~~~~~~~~~~
310
311All possible requests are fully defined by some combination of the flags in
312the previous section. For convenience, the buffer protocol provides frequently
313used combinations as single flags.
314
315In the following table *U* stands for undefined contiguity. The consumer would
316have to call :c:func:`PyBuffer_IsContiguous` to determine contiguity.
317
Georg Brandl44ea77b2013-03-28 13:28:44 +0100318.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|l|l|
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100319
320+-------------------------------+-------+---------+------------+--------+----------+--------+
321| Request | shape | strides | suboffsets | contig | readonly | format |
322+===============================+=======+=========+============+========+==========+========+
323| .. c:macro:: PyBUF_FULL | yes | yes | if needed | U | 0 | yes |
324+-------------------------------+-------+---------+------------+--------+----------+--------+
325| .. c:macro:: PyBUF_FULL_RO | yes | yes | if needed | U | 1 or 0 | yes |
326+-------------------------------+-------+---------+------------+--------+----------+--------+
327| .. c:macro:: PyBUF_RECORDS | yes | yes | NULL | U | 0 | yes |
328+-------------------------------+-------+---------+------------+--------+----------+--------+
329| .. c:macro:: PyBUF_RECORDS_RO | yes | yes | NULL | U | 1 or 0 | yes |
330+-------------------------------+-------+---------+------------+--------+----------+--------+
331| .. c:macro:: PyBUF_STRIDED | yes | yes | NULL | U | 0 | NULL |
332+-------------------------------+-------+---------+------------+--------+----------+--------+
333| .. c:macro:: PyBUF_STRIDED_RO | yes | yes | NULL | U | 1 or 0 | NULL |
334+-------------------------------+-------+---------+------------+--------+----------+--------+
335| .. c:macro:: PyBUF_CONTIG | yes | NULL | NULL | C | 0 | NULL |
336+-------------------------------+-------+---------+------------+--------+----------+--------+
337| .. c:macro:: PyBUF_CONTIG_RO | yes | NULL | NULL | C | 1 or 0 | NULL |
338+-------------------------------+-------+---------+------------+--------+----------+--------+
339
340
341Complex arrays
342==============
343
344NumPy-style: shape and strides
345~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
346
347The logical structure of NumPy-style arrays is defined by :c:member:`~Py_buffer.itemsize`,
348:c:member:`~Py_buffer.ndim`, :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides`.
349
350If ``ndim == 0``, the memory location pointed to by :c:member:`~Py_buffer.buf` is
351interpreted as a scalar of size :c:member:`~Py_buffer.itemsize`. In that case,
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200352both :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides` are ``NULL``.
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100353
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200354If :c:member:`~Py_buffer.strides` is ``NULL``, the array is interpreted as
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100355a standard n-dimensional C-array. Otherwise, the consumer must access an
356n-dimensional array as follows:
357
Joseph Fox-Rabinovitzbd26a442019-07-17 03:13:01 -0500358.. code-block:: c
359
360 ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1];
361 item = *((typeof(item) *)ptr);
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100362
363
364As noted above, :c:member:`~Py_buffer.buf` can point to any location within
365the actual memory block. An exporter can check the validity of a buffer with
366this function:
367
368.. code-block:: python
369
370 def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
371 """Verify that the parameters represent a valid array within
372 the bounds of the allocated memory:
373 char *mem: start of the physical memory block
374 memlen: length of the physical memory block
375 offset: (char *)buf - mem
376 """
377 if offset % itemsize:
378 return False
379 if offset < 0 or offset+itemsize > memlen:
380 return False
381 if any(v % itemsize for v in strides):
382 return False
383
384 if ndim <= 0:
385 return ndim == 0 and not shape and not strides
386 if 0 in shape:
387 return True
388
389 imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
390 if strides[j] <= 0)
391 imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
392 if strides[j] > 0)
393
394 return 0 <= offset+imin and offset+imax+itemsize <= memlen
395
396
397PIL-style: shape, strides and suboffsets
398~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
399
400In addition to the regular items, PIL-style arrays can contain pointers
401that must be followed in order to get to the next element in a dimension.
402For example, the regular three-dimensional C-array ``char v[2][2][3]`` can
403also be viewed as an array of 2 pointers to 2 two-dimensional arrays:
404``char (*v[2])[2][3]``. In suboffsets representation, those two pointers
405can be embedded at the start of :c:member:`~Py_buffer.buf`, pointing
406to two ``char x[2][3]`` arrays that can be located anywhere in memory.
407
408
409Here is a function that returns a pointer to the element in an N-D array
Serhiy Storchakae835b312019-10-30 21:37:16 +0200410pointed to by an N-dimensional index when there are both non-``NULL`` strides
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100411and suboffsets::
412
413 void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
414 Py_ssize_t *suboffsets, Py_ssize_t *indices) {
415 char *pointer = (char*)buf;
416 int i;
417 for (i = 0; i < ndim; i++) {
418 pointer += strides[i] * indices[i];
419 if (suboffsets[i] >=0 ) {
420 pointer = *((char**)pointer) + suboffsets[i];
421 }
422 }
423 return (void*)pointer;
424 }
425
Georg Brandl54a3faa2008-01-20 09:30:57 +0000426
Antoine Pitrouc663b582010-09-28 23:59:51 +0000427Buffer-related functions
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000428========================
Georg Brandl54a3faa2008-01-20 09:30:57 +0000429
Georg Brandl60203b42010-10-06 10:11:56 +0000430.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000431
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300432 Return ``1`` if *obj* supports the buffer interface otherwise ``0``. When ``1`` is
Georg Brandl60203b42010-10-06 10:11:56 +0000433 returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
Serhiy Storchaka3fcc1e02018-12-18 13:57:17 +0200434 succeed. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000435
436
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100437.. c:function:: int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000438
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100439 Send a request to *exporter* to fill in *view* as specified by *flags*.
440 If the exporter cannot provide a buffer of the exact type, it MUST raise
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200441 :c:data:`PyExc_BufferError`, set :c:member:`view->obj` to ``NULL`` and
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300442 return ``-1``.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000443
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100444 On success, fill in *view*, set :c:member:`view->obj` to a new reference
Stefan Krahabd887d2012-03-06 14:55:06 +0100445 to *exporter* and return 0. In the case of chained buffer providers
446 that redirect requests to a single object, :c:member:`view->obj` MAY
447 refer to this object instead of *exporter* (See :ref:`Buffer Object Structures <buffer-structs>`).
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000448
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100449 Successful calls to :c:func:`PyObject_GetBuffer` must be paired with calls
450 to :c:func:`PyBuffer_Release`, similar to :c:func:`malloc` and :c:func:`free`.
451 Thus, after the consumer is done with the buffer, :c:func:`PyBuffer_Release`
452 must be called exactly once.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000453
454
Georg Brandl60203b42010-10-06 10:11:56 +0000455.. c:function:: void PyBuffer_Release(Py_buffer *view)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000456
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100457 Release the buffer *view* and decrement the reference count for
458 :c:member:`view->obj`. This function MUST be called when the buffer
459 is no longer being used, otherwise reference leaks may occur.
460
461 It is an error to call this function on a buffer that was not obtained via
462 :c:func:`PyObject_GetBuffer`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000463
464
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300465.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *format)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000466
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100467 Return the implied :c:data:`~Py_buffer.itemsize` from :c:data:`~Py_buffer.format`.
Joannah Nanjekye9e66aba2019-08-20 11:46:36 -0300468 On error, raise an exception and return -1.
469
470 .. versionadded:: 3.9
Georg Brandl54a3faa2008-01-20 09:30:57 +0000471
472
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100473.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char order)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000474
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300475 Return ``1`` if the memory defined by the *view* is C-style (*order* is
Stefan Krah70e543b2015-08-08 14:33:28 +0200476 ``'C'``) or Fortran-style (*order* is ``'F'``) :term:`contiguous` or either one
Serhiy Storchaka3fcc1e02018-12-18 13:57:17 +0200477 (*order* is ``'A'``). Return ``0`` otherwise. This function always succeeds.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000478
479
Hai Shi1b29af82019-07-31 09:48:15 -0500480.. c:function:: void* PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
481
482 Get the memory area pointed to by the *indices* inside the given *view*.
483 *indices* must point to an array of ``view->ndim`` indices.
484
485
Hai Shi5a56ce42019-09-11 11:38:47 -0500486.. c:function:: int PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
487
488 Copy contiguous *len* bytes from *buf* to *view*.
489 *fort* can be ``'C'`` or ``'F'`` (for C-style or Fortran-style ordering).
490 ``0`` is returned on success, ``-1`` on error.
491
492
Antoine Pitrouaa50bf02018-03-28 17:26:32 +0200493.. c:function:: int PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)
494
495 Copy *len* bytes from *src* to its contiguous representation in *buf*.
Hai Shi15f5a752019-09-11 12:25:55 -0500496 *order* can be ``'C'`` or ``'F'`` or ``'A'`` (for C-style or Fortran-style
497 ordering or either one). ``0`` is returned on success, ``-1`` on error.
Antoine Pitrouaa50bf02018-03-28 17:26:32 +0200498
499 This function fails if *len* != *src->len*.
500
501
vyas451b9e76e2017-10-15 00:31:36 -0700502.. c:function:: void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000503
Stefan Krah70e543b2015-08-08 14:33:28 +0200504 Fill the *strides* array with byte-strides of a :term:`contiguous` (C-style if
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100505 *order* is ``'C'`` or Fortran-style if *order* is ``'F'``) array of the
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000506 given shape with the given number of bytes per element.
507
508
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100509.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags)
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000510
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100511 Handle buffer requests for an exporter that wants to expose *buf* of size *len*
512 with writability set according to *readonly*. *buf* is interpreted as a sequence
513 of unsigned bytes.
514
515 The *flags* argument indicates the request type. This function always fills in
516 *view* as specified by flags, unless *buf* has been designated as read-only
517 and :c:macro:`PyBUF_WRITABLE` is set in *flags*.
518
519 On success, set :c:member:`view->obj` to a new reference to *exporter* and
520 return 0. Otherwise, raise :c:data:`PyExc_BufferError`, set
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200521 :c:member:`view->obj` to ``NULL`` and return ``-1``;
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100522
523 If this function is used as part of a :ref:`getbufferproc <buffer-structs>`,
Stefan Krahbb458db2014-06-30 00:15:45 +0200524 *exporter* MUST be set to the exporting object and *flags* must be passed
Serhiy Storchakae835b312019-10-30 21:37:16 +0200525 unmodified. Otherwise, *exporter* MUST be ``NULL``.