blob: 2d199922f2e05da24b91f2d122f0cda799950763 [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
Stefan Krah9a2d99e2012-02-25 12:24:21 +010010.. sectionauthor:: Stefan Krah
Georg Brandl54a3faa2008-01-20 09:30:57 +000011
12
13.. index::
Georg Brandl54a3faa2008-01-20 09:30:57 +000014 single: buffer interface
15
Antoine Pitrou8abc9352010-12-12 19:59:47 +000016Certain objects available in Python wrap access to an underlying memory
17array or *buffer*. Such objects include the built-in :class:`bytes` and
18:class:`bytearray`, and some extension types like :class:`array.array`.
19Third-party libraries may define their own types for special purposes, such
20as image processing or numeric analysis.
Georg Brandl54a3faa2008-01-20 09:30:57 +000021
Antoine Pitrou8abc9352010-12-12 19:59:47 +000022While each of these types have their own semantics, they share the common
23characteristic of being backed by a possibly large memory buffer. It is
Stefan Krah9a2d99e2012-02-25 12:24:21 +010024then desirable, in some situations, to access that buffer directly and
Antoine Pitrou8abc9352010-12-12 19:59:47 +000025without intermediate copying.
26
27Python provides such a facility at the C level in the form of the *buffer
28protocol*. This protocol has two sides:
29
30.. index:: single: PyBufferProcs
31
32- on the producer side, a type can export a "buffer interface" which allows
33 objects of that type to expose information about their underlying buffer.
34 This interface is described in the section :ref:`buffer-structs`;
35
36- on the consumer side, several means are available to obtain a pointer to
37 the raw underlying data of an object (for example a method parameter).
38
39Simple objects such as :class:`bytes` and :class:`bytearray` expose their
40underlying buffer in byte-oriented form. Other forms are possible; for example,
41the elements exposed by a :class:`array.array` can be multi-byte values.
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
Antoine Pitrou99a00a42010-09-28 23:04:04 +000043An example consumer of the buffer interface is the :meth:`~io.BufferedIOBase.write`
44method of file objects: any object that can export a series of bytes through
45the buffer interface can be written to a file. While :meth:`write` only
46needs read-only access to the internal contents of the object passed to it,
47other methods such as :meth:`~io.BufferedIOBase.readinto` need write access
48to the contents of their argument. The buffer interface allows objects to
49selectively allow or reject exporting of read-write and read-only buffers.
50
51There are two ways for a consumer of the buffer interface to acquire a buffer
52over a target object:
53
Georg Brandl60203b42010-10-06 10:11:56 +000054* call :c:func:`PyObject_GetBuffer` with the right parameters;
Antoine Pitrou99a00a42010-09-28 23:04:04 +000055
Georg Brandl60203b42010-10-06 10:11:56 +000056* call :c:func:`PyArg_ParseTuple` (or one of its siblings) with one of the
Antoine Pitrou99a00a42010-09-28 23:04:04 +000057 ``y*``, ``w*`` or ``s*`` :ref:`format codes <arg-parsing>`.
58
Georg Brandl60203b42010-10-06 10:11:56 +000059In both cases, :c:func:`PyBuffer_Release` must be called when the buffer
Antoine Pitrou99a00a42010-09-28 23:04:04 +000060isn't needed anymore. Failure to do so could lead to various issues such as
61resource leaks.
62
Georg Brandl54a3faa2008-01-20 09:30:57 +000063
Stefan Krah9a2d99e2012-02-25 12:24:21 +010064.. _buffer-structure:
65
66Buffer structure
67================
Antoine Pitrou99a00a42010-09-28 23:04:04 +000068
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000069Buffer structures (or simply "buffers") are useful as a way to expose the
70binary data from another object to the Python programmer. They can also be
71used as a zero-copy slicing mechanism. Using their ability to reference a
72block of memory, it is possible to expose any data to the Python programmer
73quite easily. The memory could be a large, constant array in a C extension,
74it could be a raw block of memory for manipulation before passing to an
75operating system library, or it could be used to pass around structured data
76in its native, in-memory format.
Georg Brandl54a3faa2008-01-20 09:30:57 +000077
Antoine Pitrouf7ba2fa2010-09-28 23:39:41 +000078Contrary to most data types exposed by the Python interpreter, buffers
Georg Brandl60203b42010-10-06 10:11:56 +000079are not :c:type:`PyObject` pointers but rather simple C structures. This
Antoine Pitrou99a00a42010-09-28 23:04:04 +000080allows them to be created and copied very simply. When a generic wrapper
Antoine Pitrouc663b582010-09-28 23:59:51 +000081around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object
Antoine Pitrou99a00a42010-09-28 23:04:04 +000082can be created.
83
Georg Brandl54a3faa2008-01-20 09:30:57 +000084
Georg Brandl60203b42010-10-06 10:11:56 +000085.. c:type:: Py_buffer
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
Stefan Krah9a2d99e2012-02-25 12:24:21 +010087 .. c:member:: void \*obj
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000088
Stefan Krah9a2d99e2012-02-25 12:24:21 +010089 A new reference to the exporting object or *NULL*. The reference is owned
90 by the consumer and automatically decremented and set to *NULL* by
91 :c:func:`PyBuffer_Release`.
92
93 For temporary buffers that are wrapped by :c:func:`PyMemoryView_FromBuffer`
94 this field must be *NULL*.
95
96 .. c:member:: void \*buf
97
98 A pointer to the start of the logical structure described by the buffer
99 fields. This can be any location within the underlying physical memory
100 block of the exporter. For example, with negative :c:member:`~Py_buffer.strides`
101 the value may point to the end of the memory block.
102
103 For contiguous arrays, the value points to the beginning of the memory
104 block.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000105
Georg Brandl60203b42010-10-06 10:11:56 +0000106 .. c:member:: Py_ssize_t len
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000107
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100108 ``product(shape) * itemsize``. For contiguous arrays, this is the length
109 of the underlying memory block. For non-contiguous arrays, it is the length
110 that the logical structure would have if it were copied to a contiguous
111 representation.
112
113 Accessing ``((char *)buf)[0] up to ((char *)buf)[len-1]`` is only valid
114 if the buffer has been obtained by a request that guarantees contiguity. In
115 most cases such a request will be :c:macro:`PyBUF_SIMPLE` or :c:macro:`PyBUF_WRITABLE`.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000116
Georg Brandl60203b42010-10-06 10:11:56 +0000117 .. c:member:: int readonly
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000118
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100119 An indicator of whether the buffer is read-only. This field is controlled
120 by the :c:macro:`PyBUF_WRITABLE` flag.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000121
Georg Brandl60203b42010-10-06 10:11:56 +0000122 .. c:member:: Py_ssize_t itemsize
Georg Brandl54a3faa2008-01-20 09:30:57 +0000123
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100124 Item size in bytes of a single element. Same as the value of :func:`struct.calcsize`
125 called on non-NULL :c:member:`~Py_buffer.format` values.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000126
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100127 Important exception: If a consumer requests a buffer without the
128 :c:macro:`PyBUF_FORMAT` flag, :c:member:`~Py_Buffer.format` will
129 be set to *NULL*, but :c:member:`~Py_buffer.itemsize` still has
130 the value for the original format.
131
132 If :c:member:`~Py_Buffer.shape` is present, the equality
133 ``product(shape) * itemsize == len`` still holds and the consumer
134 can use :c:member:`~Py_buffer.itemsize` to navigate the buffer.
135
136 If :c:member:`~Py_Buffer.shape` is *NULL* as a result of a :c:macro:`PyBUF_SIMPLE`
137 or a :c:macro:`PyBUF_WRITABLE` request, the consumer must disregard
138 :c:member:`~Py_buffer.itemsize` and assume ``itemsize == 1``.
139
140 .. c:member:: const char \*format
141
142 A *NUL* terminated string in :mod:`struct` module style syntax describing
143 the contents of a single item. If this is *NULL*, ``"B"`` (unsigned bytes)
144 is assumed.
145
146 This field is controlled by the :c:macro:`PyBUF_FORMAT` flag.
147
148 .. c:member:: int ndim
149
150 The number of dimensions the memory represents as an n-dimensional array.
151 If it is 0, :c:member:`~Py_Buffer.buf` points to a single item representing
152 a scalar. In this case, :c:member:`~Py_buffer.shape`, :c:member:`~Py_buffer.strides`
153 and :c:member:`~Py_buffer.suboffsets` MUST be *NULL*.
154
155 The macro :c:macro:`PyBUF_MAX_NDIM` limits the maximum number of dimensions
156 to 64. Exporters MUST respect this limit, consumers of multi-dimensional
157 buffers SHOULD be able to handle up to :c:macro:`PyBUF_MAX_NDIM` dimensions.
158
159 .. c:member:: Py_ssize_t \*shape
160
161 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
162 indicating the shape of the memory as an n-dimensional array. Note that
163 ``shape[0] * ... * shape[ndim-1] * itemsize`` MUST be equal to
164 :c:member:`~Py_buffer.len`.
165
166 Shape values are restricted to ``shape[n] >= 0``. The case
167 ``shape[n] == 0`` requires special attention. See `complex arrays`_
168 for further information.
169
170 The shape array is read-only for the consumer.
171
172 .. c:member:: Py_ssize_t \*strides
173
174 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
175 giving the number of bytes to skip to get to a new element in each
176 dimension.
177
178 Stride values can be any integer. For regular arrays, strides are
179 usually positive, but a consumer MUST be able to handle the case
180 ``strides[n] <= 0``. See `complex arrays`_ for further information.
181
182 The strides array is read-only for the consumer.
183
184 .. c:member:: Py_ssize_t \*suboffsets
185
186 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`.
187 If ``suboffsets[n] >= 0``, the values stored along the nth dimension are
188 pointers and the suboffset value dictates how many bytes to add to each
189 pointer after de-referencing. A suboffset value that is negative
190 indicates that no de-referencing should occur (striding in a contiguous
191 memory block).
192
193 This type of array representation is used by the Python Imaging Library
194 (PIL). See `complex arrays`_ for further information how to access elements
195 of such an array.
196
197 The suboffsets array is read-only for the consumer.
198
199 .. c:member:: void \*internal
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000200
201 This is for use internally by the exporting object. For example, this
202 might be re-cast as an integer by the exporter and used to store flags
203 about whether or not the shape, strides, and suboffsets arrays must be
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100204 freed when the buffer is released. The consumer MUST NOT alter this
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000205 value.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000206
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100207.. _buffer-request-types:
208
209Buffer request types
210====================
211
212Buffers are usually obtained by sending a buffer request to an exporting
213object via :c:func:`PyObject_GetBuffer`. Since the complexity of the logical
214structure of the memory can vary drastically, the consumer uses the *flags*
215argument to specify the exact buffer type it can handle.
216
217All :c:data:`Py_buffer` fields are unambiguously defined by the request
218type.
219
220request-independent fields
221~~~~~~~~~~~~~~~~~~~~~~~~~~
222The following fields are not influenced by *flags* and must always be filled in
223with the correct values: :c:member:`~Py_buffer.obj`, :c:member:`~Py_buffer.buf`,
224:c:member:`~Py_buffer.len`, :c:member:`~Py_buffer.itemsize`, :c:member:`~Py_buffer.ndim`.
225
226
227readonly, format
228~~~~~~~~~~~~~~~~
229
230 .. c:macro:: PyBUF_WRITABLE
231
232 Controls the :c:member:`~Py_buffer.readonly` field. If set, the exporter
233 MUST provide a writable buffer or else report failure. Otherwise, the
234 exporter MAY provide either a read-only or writable buffer, but the choice
235 MUST be consistent for all consumers.
236
237 .. c:macro:: PyBUF_FORMAT
238
239 Controls the :c:member:`~Py_buffer.format` field. If set, this field MUST
240 be filled in correctly. Otherwise, this field MUST be *NULL*.
241
242
243:c:macro:`PyBUF_WRITABLE` can be \|'d to any of the flags in the next section.
244Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE`
245can be used as a stand-alone flag to request a simple writable buffer.
246
247:c:macro:`PyBUF_FORMAT` can be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`.
248The latter already implies format ``B`` (unsigned bytes).
249
250
251shape, strides, suboffsets
252~~~~~~~~~~~~~~~~~~~~~~~~~~
253
254The flags that control the logical structure of the memory are listed
255in decreasing order of complexity. Note that each flag contains all bits
256of the flags below it.
257
258
259+-----------------------------+-------+---------+------------+
260| Request | shape | strides | suboffsets |
261+=============================+=======+=========+============+
262| .. c:macro:: PyBUF_INDIRECT | yes | yes | if needed |
263+-----------------------------+-------+---------+------------+
264| .. c:macro:: PyBUF_STRIDES | yes | yes | NULL |
265+-----------------------------+-------+---------+------------+
266| .. c:macro:: PyBUF_ND | yes | NULL | NULL |
267+-----------------------------+-------+---------+------------+
268| .. c:macro:: PyBUF_SIMPLE | NULL | NULL | NULL |
269+-----------------------------+-------+---------+------------+
270
271
272contiguity requests
273~~~~~~~~~~~~~~~~~~~
274
275C or Fortran contiguity can be explicitly requested, with and without stride
276information. Without stride information, the buffer must be C-contiguous.
277
278+-----------------------------------+-------+---------+------------+--------+
279| Request | shape | strides | suboffsets | contig |
280+===================================+=======+=========+============+========+
281| .. c:macro:: PyBUF_C_CONTIGUOUS | yes | yes | NULL | C |
282+-----------------------------------+-------+---------+------------+--------+
283| .. c:macro:: PyBUF_F_CONTIGUOUS | yes | yes | NULL | F |
284+-----------------------------------+-------+---------+------------+--------+
285| .. c:macro:: PyBUF_ANY_CONTIGUOUS | yes | yes | NULL | C or F |
286+-----------------------------------+-------+---------+------------+--------+
287| .. c:macro:: PyBUF_ND | yes | NULL | NULL | C |
288+-----------------------------------+-------+---------+------------+--------+
289
290
291compound requests
292~~~~~~~~~~~~~~~~~
293
294All possible requests are fully defined by some combination of the flags in
295the previous section. For convenience, the buffer protocol provides frequently
296used combinations as single flags.
297
298In the following table *U* stands for undefined contiguity. The consumer would
299have to call :c:func:`PyBuffer_IsContiguous` to determine contiguity.
300
301
302
303+-------------------------------+-------+---------+------------+--------+----------+--------+
304| Request | shape | strides | suboffsets | contig | readonly | format |
305+===============================+=======+=========+============+========+==========+========+
306| .. c:macro:: PyBUF_FULL | yes | yes | if needed | U | 0 | yes |
307+-------------------------------+-------+---------+------------+--------+----------+--------+
308| .. c:macro:: PyBUF_FULL_RO | yes | yes | if needed | U | 1 or 0 | yes |
309+-------------------------------+-------+---------+------------+--------+----------+--------+
310| .. c:macro:: PyBUF_RECORDS | yes | yes | NULL | U | 0 | yes |
311+-------------------------------+-------+---------+------------+--------+----------+--------+
312| .. c:macro:: PyBUF_RECORDS_RO | yes | yes | NULL | U | 1 or 0 | yes |
313+-------------------------------+-------+---------+------------+--------+----------+--------+
314| .. c:macro:: PyBUF_STRIDED | yes | yes | NULL | U | 0 | NULL |
315+-------------------------------+-------+---------+------------+--------+----------+--------+
316| .. c:macro:: PyBUF_STRIDED_RO | yes | yes | NULL | U | 1 or 0 | NULL |
317+-------------------------------+-------+---------+------------+--------+----------+--------+
318| .. c:macro:: PyBUF_CONTIG | yes | NULL | NULL | C | 0 | NULL |
319+-------------------------------+-------+---------+------------+--------+----------+--------+
320| .. c:macro:: PyBUF_CONTIG_RO | yes | NULL | NULL | C | 1 or 0 | NULL |
321+-------------------------------+-------+---------+------------+--------+----------+--------+
322
323
324Complex arrays
325==============
326
327NumPy-style: shape and strides
328~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
329
330The logical structure of NumPy-style arrays is defined by :c:member:`~Py_buffer.itemsize`,
331:c:member:`~Py_buffer.ndim`, :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides`.
332
333If ``ndim == 0``, the memory location pointed to by :c:member:`~Py_buffer.buf` is
334interpreted as a scalar of size :c:member:`~Py_buffer.itemsize`. In that case,
335both :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides` are *NULL*.
336
337If :c:member:`~Py_buffer.strides` is *NULL*, the array is interpreted as
338a standard n-dimensional C-array. Otherwise, the consumer must access an
339n-dimensional array as follows:
340
341 ``ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1]``
342 ``item = *((typeof(item) *)ptr);``
343
344
345As noted above, :c:member:`~Py_buffer.buf` can point to any location within
346the actual memory block. An exporter can check the validity of a buffer with
347this function:
348
349.. code-block:: python
350
351 def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
352 """Verify that the parameters represent a valid array within
353 the bounds of the allocated memory:
354 char *mem: start of the physical memory block
355 memlen: length of the physical memory block
356 offset: (char *)buf - mem
357 """
358 if offset % itemsize:
359 return False
360 if offset < 0 or offset+itemsize > memlen:
361 return False
362 if any(v % itemsize for v in strides):
363 return False
364
365 if ndim <= 0:
366 return ndim == 0 and not shape and not strides
367 if 0 in shape:
368 return True
369
370 imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
371 if strides[j] <= 0)
372 imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
373 if strides[j] > 0)
374
375 return 0 <= offset+imin and offset+imax+itemsize <= memlen
376
377
378PIL-style: shape, strides and suboffsets
379~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
380
381In addition to the regular items, PIL-style arrays can contain pointers
382that must be followed in order to get to the next element in a dimension.
383For example, the regular three-dimensional C-array ``char v[2][2][3]`` can
384also be viewed as an array of 2 pointers to 2 two-dimensional arrays:
385``char (*v[2])[2][3]``. In suboffsets representation, those two pointers
386can be embedded at the start of :c:member:`~Py_buffer.buf`, pointing
387to two ``char x[2][3]`` arrays that can be located anywhere in memory.
388
389
390Here is a function that returns a pointer to the element in an N-D array
391pointed to by an N-dimensional index when there are both non-NULL strides
392and suboffsets::
393
394 void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
395 Py_ssize_t *suboffsets, Py_ssize_t *indices) {
396 char *pointer = (char*)buf;
397 int i;
398 for (i = 0; i < ndim; i++) {
399 pointer += strides[i] * indices[i];
400 if (suboffsets[i] >=0 ) {
401 pointer = *((char**)pointer) + suboffsets[i];
402 }
403 }
404 return (void*)pointer;
405 }
406
Georg Brandl54a3faa2008-01-20 09:30:57 +0000407
Antoine Pitrouc663b582010-09-28 23:59:51 +0000408Buffer-related functions
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000409========================
Georg Brandl54a3faa2008-01-20 09:30:57 +0000410
Georg Brandl60203b42010-10-06 10:11:56 +0000411.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000412
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000413 Return 1 if *obj* supports the buffer interface otherwise 0. When 1 is
Georg Brandl60203b42010-10-06 10:11:56 +0000414 returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000415 succeed.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000416
417
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100418.. c:function:: int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000419
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100420 Send a request to *exporter* to fill in *view* as specified by *flags*.
421 If the exporter cannot provide a buffer of the exact type, it MUST raise
422 :c:data:`PyExc_BufferError`, set :c:member:`view->obj` to *NULL* and
423 return -1.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000424
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100425 On success, fill in *view*, set :c:member:`view->obj` to a new reference
426 to *exporter* and return 0.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000427
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100428 Successful calls to :c:func:`PyObject_GetBuffer` must be paired with calls
429 to :c:func:`PyBuffer_Release`, similar to :c:func:`malloc` and :c:func:`free`.
430 Thus, after the consumer is done with the buffer, :c:func:`PyBuffer_Release`
431 must be called exactly once.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000432
433
Georg Brandl60203b42010-10-06 10:11:56 +0000434.. c:function:: void PyBuffer_Release(Py_buffer *view)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000435
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100436 Release the buffer *view* and decrement the reference count for
437 :c:member:`view->obj`. This function MUST be called when the buffer
438 is no longer being used, otherwise reference leaks may occur.
439
440 It is an error to call this function on a buffer that was not obtained via
441 :c:func:`PyObject_GetBuffer`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000442
443
Georg Brandl60203b42010-10-06 10:11:56 +0000444.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000445
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100446 Return the implied :c:data:`~Py_buffer.itemsize` from :c:data:`~Py_buffer.format`.
447 This function is not yet implemented.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000448
449
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100450.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char order)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000451
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100452 Return 1 if the memory defined by the *view* is C-style (*order* is
453 ``'C'``) or Fortran-style (*order* is ``'F'``) contiguous or either one
454 (*order* is ``'A'``). Return 0 otherwise.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000455
456
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100457.. c:function:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char order)
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000458
459 Fill the *strides* array with byte-strides of a contiguous (C-style if
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100460 *order* is ``'C'`` or Fortran-style if *order* is ``'F'``) array of the
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000461 given shape with the given number of bytes per element.
462
463
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100464.. 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 +0000465
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100466 Handle buffer requests for an exporter that wants to expose *buf* of size *len*
467 with writability set according to *readonly*. *buf* is interpreted as a sequence
468 of unsigned bytes.
469
470 The *flags* argument indicates the request type. This function always fills in
471 *view* as specified by flags, unless *buf* has been designated as read-only
472 and :c:macro:`PyBUF_WRITABLE` is set in *flags*.
473
474 On success, set :c:member:`view->obj` to a new reference to *exporter* and
475 return 0. Otherwise, raise :c:data:`PyExc_BufferError`, set
476 :c:member:`view->obj` to *NULL* and return -1;
477
478 If this function is used as part of a :ref:`getbufferproc <buffer-structs>`,
479 *exporter* MUST be set to the exporting object. Otherwise, *exporter* MUST
480 be NULL.
481
482
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000483