• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
3.. index::
4   single: buffer protocol
5   single: buffer interface; (see buffer protocol)
6   single: buffer object; (see buffer protocol)
7
8.. _bufferobjects:
9
10Buffer Protocol
11---------------
12
13.. sectionauthor:: Greg Stein <gstein@lyra.org>
14.. sectionauthor:: Benjamin Peterson
15.. sectionauthor:: Stefan Krah
16
17
18Certain 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.
23
24While each of these types have their own semantics, they share the common
25characteristic of being backed by a possibly large memory buffer.  It is
26then desirable, in some situations, to access that buffer directly and
27without intermediate copying.
28
29Python provides such a facility at the C level in the form of the :ref:`buffer
30protocol <bufferobjects>`.  This protocol has two sides:
31
32.. index:: single: PyBufferProcs (C type)
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,
43the elements exposed by an :class:`array.array` can be multi-byte values.
44
45An 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
56* call :c:func:`PyObject_GetBuffer` with the right parameters;
57
58* call :c:func:`PyArg_ParseTuple` (or one of its siblings) with one of the
59  ``y*``, ``w*`` or ``s*`` :ref:`format codes <arg-parsing>`.
60
61In both cases, :c:func:`PyBuffer_Release` must be called when the buffer
62isn't needed anymore.  Failure to do so could lead to various issues such as
63resource leaks.
64
65
66.. _buffer-structure:
67
68Buffer structure
69================
70
71Buffer 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.
79
80Contrary to most data types exposed by the Python interpreter, buffers
81are not :c:type:`PyObject` pointers but rather simple C structures.  This
82allows them to be created and copied very simply.  When a generic wrapper
83around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object
84can be created.
85
86For 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`.
89
90.. c:type:: Py_buffer
91
92   .. 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
99      For :term:`contiguous` arrays, the value points to the beginning of
100      the memory block.
101
102   .. c:member:: PyObject *obj
103
104      A new reference to the exporting object. The reference is owned by
105      the consumer and automatically released
106      (i.e. reference count decremented)
107      and set to ``NULL`` by
108      :c:func:`PyBuffer_Release`. The field is the equivalent of the return
109      value of any standard C-API function.
110
111      As a special case, for *temporary* buffers that are wrapped by
112      :c:func:`PyMemoryView_FromBuffer` or :c:func:`PyBuffer_FillInfo`
113      this field is ``NULL``. In general, exporting objects MUST NOT
114      use this scheme.
115
116   .. c:member:: Py_ssize_t len
117
118      ``product(shape) * itemsize``. For contiguous arrays, this is the length
119      of the underlying memory block. For non-contiguous arrays, it is the length
120      that the logical structure would have if it were copied to a contiguous
121      representation.
122
123      Accessing ``((char *)buf)[0] up to ((char *)buf)[len-1]`` is only valid
124      if the buffer has been obtained by a request that guarantees contiguity. In
125      most cases such a request will be :c:macro:`PyBUF_SIMPLE` or :c:macro:`PyBUF_WRITABLE`.
126
127   .. c:member:: int readonly
128
129      An indicator of whether the buffer is read-only. This field is controlled
130      by the :c:macro:`PyBUF_WRITABLE` flag.
131
132   .. c:member:: Py_ssize_t itemsize
133
134      Item size in bytes of a single element. Same as the value of :func:`struct.calcsize`
135      called on non-``NULL`` :c:member:`~Py_buffer.format` values.
136
137      Important exception: If a consumer requests a buffer without the
138      :c:macro:`PyBUF_FORMAT` flag, :c:member:`~Py_buffer.format` will
139      be set to  ``NULL``,  but :c:member:`~Py_buffer.itemsize` still has
140      the value for the original format.
141
142      If :c:member:`~Py_buffer.shape` is present, the equality
143      ``product(shape) * itemsize == len`` still holds and the consumer
144      can use :c:member:`~Py_buffer.itemsize` to navigate the buffer.
145
146      If :c:member:`~Py_buffer.shape` is ``NULL`` as a result of a :c:macro:`PyBUF_SIMPLE`
147      or a :c:macro:`PyBUF_WRITABLE` request, the consumer must disregard
148      :c:member:`~Py_buffer.itemsize` and assume ``itemsize == 1``.
149
150   .. c:member:: char *format
151
152      A *NULL* terminated string in :mod:`struct` module style syntax describing
153      the contents of a single item. If this is ``NULL``, ``"B"`` (unsigned bytes)
154      is assumed.
155
156      This field is controlled by the :c:macro:`PyBUF_FORMAT` flag.
157
158   .. c:member:: int ndim
159
160      The number of dimensions the memory represents as an n-dimensional array.
161      If it is ``0``, :c:member:`~Py_buffer.buf` points to a single item representing
162      a scalar. In this case, :c:member:`~Py_buffer.shape`, :c:member:`~Py_buffer.strides`
163      and :c:member:`~Py_buffer.suboffsets` MUST be ``NULL``.
164      The maximum number of dimensions is given by :c:macro:`PyBUF_MAX_NDIM`.
165
166   .. c:member:: Py_ssize_t *shape
167
168      An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
169      indicating the shape of the memory as an n-dimensional array. Note that
170      ``shape[0] * ... * shape[ndim-1] * itemsize`` MUST be equal to
171      :c:member:`~Py_buffer.len`.
172
173      Shape values are restricted to ``shape[n] >= 0``. The case
174      ``shape[n] == 0`` requires special attention. See `complex arrays`_
175      for further information.
176
177      The shape array is read-only for the consumer.
178
179   .. c:member:: Py_ssize_t *strides
180
181      An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
182      giving the number of bytes to skip to get to a new element in each
183      dimension.
184
185      Stride values can be any integer. For regular arrays, strides are
186      usually positive, but a consumer MUST be able to handle the case
187      ``strides[n] <= 0``. See `complex arrays`_ for further information.
188
189      The strides array is read-only for the consumer.
190
191   .. c:member:: Py_ssize_t *suboffsets
192
193      An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`.
194      If ``suboffsets[n] >= 0``, the values stored along the nth dimension are
195      pointers and the suboffset value dictates how many bytes to add to each
196      pointer after de-referencing. A suboffset value that is negative
197      indicates that no de-referencing should occur (striding in a contiguous
198      memory block).
199
200      If all suboffsets are negative (i.e. no de-referencing is needed), then
201      this field must be ``NULL`` (the default value).
202
203      This type of array representation is used by the Python Imaging Library
204      (PIL). See `complex arrays`_ for further information how to access elements
205      of such an array.
206
207      The suboffsets array is read-only for the consumer.
208
209   .. c:member:: void *internal
210
211      This is for use internally by the exporting object. For example, this
212      might be re-cast as an integer by the exporter and used to store flags
213      about whether or not the shape, strides, and suboffsets arrays must be
214      freed when the buffer is released. The consumer MUST NOT alter this
215      value.
216
217
218Constants:
219
220.. c:macro:: PyBUF_MAX_NDIM
221
222   The maximum number of dimensions the memory represents.
223   Exporters MUST respect this limit, consumers of multi-dimensional
224   buffers SHOULD be able to handle up to :c:macro:`!PyBUF_MAX_NDIM` dimensions.
225   Currently set to 64.
226
227
228.. _buffer-request-types:
229
230Buffer request types
231====================
232
233Buffers are usually obtained by sending a buffer request to an exporting
234object via :c:func:`PyObject_GetBuffer`. Since the complexity of the logical
235structure of the memory can vary drastically, the consumer uses the *flags*
236argument to specify the exact buffer type it can handle.
237
238All :c:type:`Py_buffer` fields are unambiguously defined by the request
239type.
240
241request-independent fields
242~~~~~~~~~~~~~~~~~~~~~~~~~~
243The following fields are not influenced by *flags* and must always be filled in
244with the correct values: :c:member:`~Py_buffer.obj`, :c:member:`~Py_buffer.buf`,
245:c:member:`~Py_buffer.len`, :c:member:`~Py_buffer.itemsize`, :c:member:`~Py_buffer.ndim`.
246
247readonly, format
248~~~~~~~~~~~~~~~~
249
250   .. c:macro:: PyBUF_WRITABLE
251
252      Controls the :c:member:`~Py_buffer.readonly` field. If set, the exporter
253      MUST provide a writable buffer or else report failure. Otherwise, the
254      exporter MAY provide either a read-only or writable buffer, but the choice
255      MUST be consistent for all consumers. For example, :c:expr:`PyBUF_SIMPLE | PyBUF_WRITABLE`
256      can be used to request a simple writable buffer.
257
258   .. c:macro:: PyBUF_FORMAT
259
260      Controls the :c:member:`~Py_buffer.format` field. If set, this field MUST
261      be filled in correctly. Otherwise, this field MUST be ``NULL``.
262
263
264:c:macro:`PyBUF_WRITABLE` can be \|'d to any of the flags in the next section.
265Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE`
266can be used as a stand-alone flag to request a simple writable buffer.
267
268:c:macro:`PyBUF_FORMAT` must be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`, because
269the latter already implies format ``B`` (unsigned bytes). :c:macro:`!PyBUF_FORMAT` cannot be
270used on its own.
271
272
273shape, strides, suboffsets
274~~~~~~~~~~~~~~~~~~~~~~~~~~
275
276The flags that control the logical structure of the memory are listed
277in decreasing order of complexity. Note that each flag contains all bits
278of the flags below it.
279
280.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|
281
282+-----------------------------+-------+---------+------------+
283|  Request                    | shape | strides | suboffsets |
284+=============================+=======+=========+============+
285| .. c:macro:: PyBUF_INDIRECT |  yes  |   yes   | if needed  |
286+-----------------------------+-------+---------+------------+
287| .. c:macro:: PyBUF_STRIDES  |  yes  |   yes   |    NULL    |
288+-----------------------------+-------+---------+------------+
289| .. c:macro:: PyBUF_ND       |  yes  |   NULL  |    NULL    |
290+-----------------------------+-------+---------+------------+
291| .. c:macro:: PyBUF_SIMPLE   |  NULL |   NULL  |    NULL    |
292+-----------------------------+-------+---------+------------+
293
294
295.. index:: contiguous, C-contiguous, Fortran contiguous
296
297contiguity requests
298~~~~~~~~~~~~~~~~~~~
299
300C or Fortran :term:`contiguity <contiguous>` can be explicitly requested,
301with and without stride information. Without stride information, the buffer
302must be C-contiguous.
303
304.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|
305
306+-----------------------------------+-------+---------+------------+--------+
307|  Request                          | shape | strides | suboffsets | contig |
308+===================================+=======+=========+============+========+
309| .. c:macro:: PyBUF_C_CONTIGUOUS   |  yes  |   yes   |    NULL    |   C    |
310+-----------------------------------+-------+---------+------------+--------+
311| .. c:macro:: PyBUF_F_CONTIGUOUS   |  yes  |   yes   |    NULL    |   F    |
312+-----------------------------------+-------+---------+------------+--------+
313| .. c:macro:: PyBUF_ANY_CONTIGUOUS |  yes  |   yes   |    NULL    | C or F |
314+-----------------------------------+-------+---------+------------+--------+
315| :c:macro:`PyBUF_ND`               |  yes  |   NULL  |    NULL    |   C    |
316+-----------------------------------+-------+---------+------------+--------+
317
318
319compound requests
320~~~~~~~~~~~~~~~~~
321
322All possible requests are fully defined by some combination of the flags in
323the previous section. For convenience, the buffer protocol provides frequently
324used combinations as single flags.
325
326In the following table *U* stands for undefined contiguity. The consumer would
327have to call :c:func:`PyBuffer_IsContiguous` to determine contiguity.
328
329.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|l|l|
330
331+-------------------------------+-------+---------+------------+--------+----------+--------+
332|  Request                      | shape | strides | suboffsets | contig | readonly | format |
333+===============================+=======+=========+============+========+==========+========+
334| .. c:macro:: PyBUF_FULL       |  yes  |   yes   | if needed  |   U    |     0    |  yes   |
335+-------------------------------+-------+---------+------------+--------+----------+--------+
336| .. c:macro:: PyBUF_FULL_RO    |  yes  |   yes   | if needed  |   U    |  1 or 0  |  yes   |
337+-------------------------------+-------+---------+------------+--------+----------+--------+
338| .. c:macro:: PyBUF_RECORDS    |  yes  |   yes   |    NULL    |   U    |     0    |  yes   |
339+-------------------------------+-------+---------+------------+--------+----------+--------+
340| .. c:macro:: PyBUF_RECORDS_RO |  yes  |   yes   |    NULL    |   U    |  1 or 0  |  yes   |
341+-------------------------------+-------+---------+------------+--------+----------+--------+
342| .. c:macro:: PyBUF_STRIDED    |  yes  |   yes   |    NULL    |   U    |     0    |  NULL  |
343+-------------------------------+-------+---------+------------+--------+----------+--------+
344| .. c:macro:: PyBUF_STRIDED_RO |  yes  |   yes   |    NULL    |   U    |  1 or 0  |  NULL  |
345+-------------------------------+-------+---------+------------+--------+----------+--------+
346| .. c:macro:: PyBUF_CONTIG     |  yes  |   NULL  |    NULL    |   C    |     0    |  NULL  |
347+-------------------------------+-------+---------+------------+--------+----------+--------+
348| .. c:macro:: PyBUF_CONTIG_RO  |  yes  |   NULL  |    NULL    |   C    |  1 or 0  |  NULL  |
349+-------------------------------+-------+---------+------------+--------+----------+--------+
350
351
352Complex arrays
353==============
354
355NumPy-style: shape and strides
356~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
357
358The logical structure of NumPy-style arrays is defined by :c:member:`~Py_buffer.itemsize`,
359:c:member:`~Py_buffer.ndim`, :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides`.
360
361If ``ndim == 0``, the memory location pointed to by :c:member:`~Py_buffer.buf` is
362interpreted as a scalar of size :c:member:`~Py_buffer.itemsize`. In that case,
363both :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides` are ``NULL``.
364
365If :c:member:`~Py_buffer.strides` is ``NULL``, the array is interpreted as
366a standard n-dimensional C-array. Otherwise, the consumer must access an
367n-dimensional array as follows:
368
369.. code-block:: c
370
371   ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1];
372   item = *((typeof(item) *)ptr);
373
374
375As noted above, :c:member:`~Py_buffer.buf` can point to any location within
376the actual memory block. An exporter can check the validity of a buffer with
377this function:
378
379.. code-block:: python
380
381   def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
382       """Verify that the parameters represent a valid array within
383          the bounds of the allocated memory:
384              char *mem: start of the physical memory block
385              memlen: length of the physical memory block
386              offset: (char *)buf - mem
387       """
388       if offset % itemsize:
389           return False
390       if offset < 0 or offset+itemsize > memlen:
391           return False
392       if any(v % itemsize for v in strides):
393           return False
394
395       if ndim <= 0:
396           return ndim == 0 and not shape and not strides
397       if 0 in shape:
398           return True
399
400       imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
401                  if strides[j] <= 0)
402       imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
403                  if strides[j] > 0)
404
405       return 0 <= offset+imin and offset+imax+itemsize <= memlen
406
407
408PIL-style: shape, strides and suboffsets
409~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
410
411In addition to the regular items, PIL-style arrays can contain pointers
412that must be followed in order to get to the next element in a dimension.
413For example, the regular three-dimensional C-array ``char v[2][2][3]`` can
414also be viewed as an array of 2 pointers to 2 two-dimensional arrays:
415``char (*v[2])[2][3]``. In suboffsets representation, those two pointers
416can be embedded at the start of :c:member:`~Py_buffer.buf`, pointing
417to two ``char x[2][3]`` arrays that can be located anywhere in memory.
418
419
420Here is a function that returns a pointer to the element in an N-D array
421pointed to by an N-dimensional index when there are both non-``NULL`` strides
422and suboffsets::
423
424   void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
425                          Py_ssize_t *suboffsets, Py_ssize_t *indices) {
426       char *pointer = (char*)buf;
427       int i;
428       for (i = 0; i < ndim; i++) {
429           pointer += strides[i] * indices[i];
430           if (suboffsets[i] >=0 ) {
431               pointer = *((char**)pointer) + suboffsets[i];
432           }
433       }
434       return (void*)pointer;
435   }
436
437
438Buffer-related functions
439========================
440
441.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
442
443   Return ``1`` if *obj* supports the buffer interface otherwise ``0``.  When ``1`` is
444   returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
445   succeed.  This function always succeeds.
446
447
448.. c:function:: int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)
449
450   Send a request to *exporter* to fill in *view* as specified by  *flags*.
451   If the exporter cannot provide a buffer of the exact type, it MUST raise
452   :exc:`BufferError`, set ``view->obj`` to ``NULL`` and
453   return ``-1``.
454
455   On success, fill in *view*, set ``view->obj`` to a new reference
456   to *exporter* and return 0. In the case of chained buffer providers
457   that redirect requests to a single object, ``view->obj`` MAY
458   refer to this object instead of *exporter* (See :ref:`Buffer Object Structures <buffer-structs>`).
459
460   Successful calls to :c:func:`PyObject_GetBuffer` must be paired with calls
461   to :c:func:`PyBuffer_Release`, similar to :c:func:`malloc` and :c:func:`free`.
462   Thus, after the consumer is done with the buffer, :c:func:`PyBuffer_Release`
463   must be called exactly once.
464
465
466.. c:function:: void PyBuffer_Release(Py_buffer *view)
467
468   Release the buffer *view* and release the :term:`strong reference`
469   (i.e. decrement the reference count) to the view's supporting object,
470   ``view->obj``. This function MUST be called when the buffer
471   is no longer being used, otherwise reference leaks may occur.
472
473   It is an error to call this function on a buffer that was not obtained via
474   :c:func:`PyObject_GetBuffer`.
475
476
477.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *format)
478
479   Return the implied :c:member:`~Py_buffer.itemsize` from :c:member:`~Py_buffer.format`.
480   On error, raise an exception and return -1.
481
482   .. versionadded:: 3.9
483
484
485.. c:function:: int PyBuffer_IsContiguous(const Py_buffer *view, char order)
486
487   Return ``1`` if the memory defined by the *view* is C-style (*order* is
488   ``'C'``) or Fortran-style (*order* is ``'F'``) :term:`contiguous` or either one
489   (*order* is ``'A'``).  Return ``0`` otherwise.  This function always succeeds.
490
491
492.. c:function:: void* PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices)
493
494   Get the memory area pointed to by the *indices* inside the given *view*.
495   *indices* must point to an array of ``view->ndim`` indices.
496
497
498.. c:function:: int PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort)
499
500   Copy contiguous *len* bytes from *buf* to *view*.
501   *fort* can be ``'C'`` or ``'F'`` (for C-style or Fortran-style ordering).
502   ``0`` is returned on success, ``-1`` on error.
503
504
505.. c:function:: int PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)
506
507   Copy *len* bytes from *src* to its contiguous representation in *buf*.
508   *order* can be ``'C'`` or ``'F'`` or ``'A'`` (for C-style or Fortran-style
509   ordering or either one). ``0`` is returned on success, ``-1`` on error.
510
511   This function fails if *len* != *src->len*.
512
513
514.. c:function:: int PyObject_CopyData(PyObject *dest, PyObject *src)
515
516   Copy data from *src* to *dest* buffer. Can convert between C-style and
517   or Fortran-style buffers.
518
519   ``0`` is returned on success, ``-1`` on error.
520
521.. c:function:: void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)
522
523   Fill the *strides* array with byte-strides of a :term:`contiguous` (C-style if
524   *order* is ``'C'`` or Fortran-style if *order* is ``'F'``) array of the
525   given shape with the given number of bytes per element.
526
527
528.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags)
529
530   Handle buffer requests for an exporter that wants to expose *buf* of size *len*
531   with writability set according to *readonly*. *buf* is interpreted as a sequence
532   of unsigned bytes.
533
534   The *flags* argument indicates the request type. This function always fills in
535   *view* as specified by flags, unless *buf* has been designated as read-only
536   and :c:macro:`PyBUF_WRITABLE` is set in *flags*.
537
538   On success, set ``view->obj`` to a new reference to *exporter* and
539   return 0. Otherwise, raise :exc:`BufferError`, set
540   ``view->obj`` to ``NULL`` and return ``-1``;
541
542   If this function is used as part of a :ref:`getbufferproc <buffer-structs>`,
543   *exporter* MUST be set to the exporting object and *flags* must be passed
544   unmodified. Otherwise, *exporter* MUST be ``NULL``.
545