blob: d636935754e6e780bff1e60bc1a2f2d34ef112e6 [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
Stefan Krahabd887d2012-03-06 14:55:06 +010084For short instructions how to write an exporting object, see
85:ref:`Buffer Object Structures <buffer-structs>`. For obtaining
86a buffer, see :c:func:`PyObject_GetBuffer`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:type:: Py_buffer
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
Stefan Krah9a2d99e2012-02-25 12:24:21 +010090 .. c:member:: void \*obj
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000091
Stefan Krahabd887d2012-03-06 14:55:06 +010092 A new reference to the exporting object. The reference is owned by
93 the consumer and automatically decremented and set to *NULL* by
94 :c:func:`PyBuffer_Release`. The field is the equivalent of the return
95 value of any standard C-API function.
Stefan Krah9a2d99e2012-02-25 12:24:21 +010096
Stefan Krahabd887d2012-03-06 14:55:06 +010097 As a special case, for *temporary* buffers that are wrapped by
98 :c:func:`PyMemoryView_FromBuffer` or :c:func:`PyBuffer_FillInfo`
99 this field is *NULL*. In general, exporting objects MUST NOT
100 use this scheme.
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100101
102 .. c:member:: void \*buf
103
104 A pointer to the start of the logical structure described by the buffer
105 fields. This can be any location within the underlying physical memory
106 block of the exporter. For example, with negative :c:member:`~Py_buffer.strides`
107 the value may point to the end of the memory block.
108
109 For contiguous arrays, the value points to the beginning of the memory
110 block.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000111
Georg Brandl60203b42010-10-06 10:11:56 +0000112 .. c:member:: Py_ssize_t len
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000113
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100114 ``product(shape) * itemsize``. For contiguous arrays, this is the length
115 of the underlying memory block. For non-contiguous arrays, it is the length
116 that the logical structure would have if it were copied to a contiguous
117 representation.
118
119 Accessing ``((char *)buf)[0] up to ((char *)buf)[len-1]`` is only valid
120 if the buffer has been obtained by a request that guarantees contiguity. In
121 most cases such a request will be :c:macro:`PyBUF_SIMPLE` or :c:macro:`PyBUF_WRITABLE`.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000122
Georg Brandl60203b42010-10-06 10:11:56 +0000123 .. c:member:: int readonly
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000124
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100125 An indicator of whether the buffer is read-only. This field is controlled
126 by the :c:macro:`PyBUF_WRITABLE` flag.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000127
Georg Brandl60203b42010-10-06 10:11:56 +0000128 .. c:member:: Py_ssize_t itemsize
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100130 Item size in bytes of a single element. Same as the value of :func:`struct.calcsize`
131 called on non-NULL :c:member:`~Py_buffer.format` values.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000132
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100133 Important exception: If a consumer requests a buffer without the
134 :c:macro:`PyBUF_FORMAT` flag, :c:member:`~Py_Buffer.format` will
135 be set to *NULL*, but :c:member:`~Py_buffer.itemsize` still has
136 the value for the original format.
137
138 If :c:member:`~Py_Buffer.shape` is present, the equality
139 ``product(shape) * itemsize == len`` still holds and the consumer
140 can use :c:member:`~Py_buffer.itemsize` to navigate the buffer.
141
142 If :c:member:`~Py_Buffer.shape` is *NULL* as a result of a :c:macro:`PyBUF_SIMPLE`
143 or a :c:macro:`PyBUF_WRITABLE` request, the consumer must disregard
144 :c:member:`~Py_buffer.itemsize` and assume ``itemsize == 1``.
145
146 .. c:member:: const char \*format
147
148 A *NUL* terminated string in :mod:`struct` module style syntax describing
149 the contents of a single item. If this is *NULL*, ``"B"`` (unsigned bytes)
150 is assumed.
151
152 This field is controlled by the :c:macro:`PyBUF_FORMAT` flag.
153
154 .. c:member:: int ndim
155
156 The number of dimensions the memory represents as an n-dimensional array.
157 If it is 0, :c:member:`~Py_Buffer.buf` points to a single item representing
158 a scalar. In this case, :c:member:`~Py_buffer.shape`, :c:member:`~Py_buffer.strides`
159 and :c:member:`~Py_buffer.suboffsets` MUST be *NULL*.
160
161 The macro :c:macro:`PyBUF_MAX_NDIM` limits the maximum number of dimensions
162 to 64. Exporters MUST respect this limit, consumers of multi-dimensional
163 buffers SHOULD be able to handle up to :c:macro:`PyBUF_MAX_NDIM` dimensions.
164
165 .. c:member:: Py_ssize_t \*shape
166
167 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
168 indicating the shape of the memory as an n-dimensional array. Note that
169 ``shape[0] * ... * shape[ndim-1] * itemsize`` MUST be equal to
170 :c:member:`~Py_buffer.len`.
171
172 Shape values are restricted to ``shape[n] >= 0``. The case
173 ``shape[n] == 0`` requires special attention. See `complex arrays`_
174 for further information.
175
176 The shape array is read-only for the consumer.
177
178 .. c:member:: Py_ssize_t \*strides
179
180 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
181 giving the number of bytes to skip to get to a new element in each
182 dimension.
183
184 Stride values can be any integer. For regular arrays, strides are
185 usually positive, but a consumer MUST be able to handle the case
186 ``strides[n] <= 0``. See `complex arrays`_ for further information.
187
188 The strides array is read-only for the consumer.
189
190 .. c:member:: Py_ssize_t \*suboffsets
191
192 An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`.
193 If ``suboffsets[n] >= 0``, the values stored along the nth dimension are
194 pointers and the suboffset value dictates how many bytes to add to each
195 pointer after de-referencing. A suboffset value that is negative
196 indicates that no de-referencing should occur (striding in a contiguous
197 memory block).
198
199 This type of array representation is used by the Python Imaging Library
200 (PIL). See `complex arrays`_ for further information how to access elements
201 of such an array.
202
203 The suboffsets array is read-only for the consumer.
204
205 .. c:member:: void \*internal
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000206
207 This is for use internally by the exporting object. For example, this
208 might be re-cast as an integer by the exporter and used to store flags
209 about whether or not the shape, strides, and suboffsets arrays must be
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100210 freed when the buffer is released. The consumer MUST NOT alter this
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000211 value.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000212
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100213.. _buffer-request-types:
214
215Buffer request types
216====================
217
218Buffers are usually obtained by sending a buffer request to an exporting
219object via :c:func:`PyObject_GetBuffer`. Since the complexity of the logical
220structure of the memory can vary drastically, the consumer uses the *flags*
221argument to specify the exact buffer type it can handle.
222
223All :c:data:`Py_buffer` fields are unambiguously defined by the request
224type.
225
226request-independent fields
227~~~~~~~~~~~~~~~~~~~~~~~~~~
228The following fields are not influenced by *flags* and must always be filled in
229with the correct values: :c:member:`~Py_buffer.obj`, :c:member:`~Py_buffer.buf`,
230:c:member:`~Py_buffer.len`, :c:member:`~Py_buffer.itemsize`, :c:member:`~Py_buffer.ndim`.
231
232
233readonly, format
234~~~~~~~~~~~~~~~~
235
236 .. c:macro:: PyBUF_WRITABLE
237
238 Controls the :c:member:`~Py_buffer.readonly` field. If set, the exporter
239 MUST provide a writable buffer or else report failure. Otherwise, the
240 exporter MAY provide either a read-only or writable buffer, but the choice
241 MUST be consistent for all consumers.
242
243 .. c:macro:: PyBUF_FORMAT
244
245 Controls the :c:member:`~Py_buffer.format` field. If set, this field MUST
246 be filled in correctly. Otherwise, this field MUST be *NULL*.
247
248
249:c:macro:`PyBUF_WRITABLE` can be \|'d to any of the flags in the next section.
250Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE`
251can be used as a stand-alone flag to request a simple writable buffer.
252
253:c:macro:`PyBUF_FORMAT` can be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`.
254The latter already implies format ``B`` (unsigned bytes).
255
256
257shape, strides, suboffsets
258~~~~~~~~~~~~~~~~~~~~~~~~~~
259
260The flags that control the logical structure of the memory are listed
261in decreasing order of complexity. Note that each flag contains all bits
262of the flags below it.
263
264
265+-----------------------------+-------+---------+------------+
266| Request | shape | strides | suboffsets |
267+=============================+=======+=========+============+
268| .. c:macro:: PyBUF_INDIRECT | yes | yes | if needed |
269+-----------------------------+-------+---------+------------+
270| .. c:macro:: PyBUF_STRIDES | yes | yes | NULL |
271+-----------------------------+-------+---------+------------+
272| .. c:macro:: PyBUF_ND | yes | NULL | NULL |
273+-----------------------------+-------+---------+------------+
274| .. c:macro:: PyBUF_SIMPLE | NULL | NULL | NULL |
275+-----------------------------+-------+---------+------------+
276
277
278contiguity requests
279~~~~~~~~~~~~~~~~~~~
280
281C or Fortran contiguity can be explicitly requested, with and without stride
282information. Without stride information, the buffer must be C-contiguous.
283
284+-----------------------------------+-------+---------+------------+--------+
285| Request | shape | strides | suboffsets | contig |
286+===================================+=======+=========+============+========+
287| .. c:macro:: PyBUF_C_CONTIGUOUS | yes | yes | NULL | C |
288+-----------------------------------+-------+---------+------------+--------+
289| .. c:macro:: PyBUF_F_CONTIGUOUS | yes | yes | NULL | F |
290+-----------------------------------+-------+---------+------------+--------+
291| .. c:macro:: PyBUF_ANY_CONTIGUOUS | yes | yes | NULL | C or F |
292+-----------------------------------+-------+---------+------------+--------+
293| .. c:macro:: PyBUF_ND | yes | NULL | NULL | C |
294+-----------------------------------+-------+---------+------------+--------+
295
296
297compound requests
298~~~~~~~~~~~~~~~~~
299
300All possible requests are fully defined by some combination of the flags in
301the previous section. For convenience, the buffer protocol provides frequently
302used combinations as single flags.
303
304In the following table *U* stands for undefined contiguity. The consumer would
305have to call :c:func:`PyBuffer_IsContiguous` to determine contiguity.
306
307
308
309+-------------------------------+-------+---------+------------+--------+----------+--------+
310| Request | shape | strides | suboffsets | contig | readonly | format |
311+===============================+=======+=========+============+========+==========+========+
312| .. c:macro:: PyBUF_FULL | yes | yes | if needed | U | 0 | yes |
313+-------------------------------+-------+---------+------------+--------+----------+--------+
314| .. c:macro:: PyBUF_FULL_RO | yes | yes | if needed | U | 1 or 0 | yes |
315+-------------------------------+-------+---------+------------+--------+----------+--------+
316| .. c:macro:: PyBUF_RECORDS | yes | yes | NULL | U | 0 | yes |
317+-------------------------------+-------+---------+------------+--------+----------+--------+
318| .. c:macro:: PyBUF_RECORDS_RO | yes | yes | NULL | U | 1 or 0 | yes |
319+-------------------------------+-------+---------+------------+--------+----------+--------+
320| .. c:macro:: PyBUF_STRIDED | yes | yes | NULL | U | 0 | NULL |
321+-------------------------------+-------+---------+------------+--------+----------+--------+
322| .. c:macro:: PyBUF_STRIDED_RO | yes | yes | NULL | U | 1 or 0 | NULL |
323+-------------------------------+-------+---------+------------+--------+----------+--------+
324| .. c:macro:: PyBUF_CONTIG | yes | NULL | NULL | C | 0 | NULL |
325+-------------------------------+-------+---------+------------+--------+----------+--------+
326| .. c:macro:: PyBUF_CONTIG_RO | yes | NULL | NULL | C | 1 or 0 | NULL |
327+-------------------------------+-------+---------+------------+--------+----------+--------+
328
329
330Complex arrays
331==============
332
333NumPy-style: shape and strides
334~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
335
336The logical structure of NumPy-style arrays is defined by :c:member:`~Py_buffer.itemsize`,
337:c:member:`~Py_buffer.ndim`, :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides`.
338
339If ``ndim == 0``, the memory location pointed to by :c:member:`~Py_buffer.buf` is
340interpreted as a scalar of size :c:member:`~Py_buffer.itemsize`. In that case,
341both :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides` are *NULL*.
342
343If :c:member:`~Py_buffer.strides` is *NULL*, the array is interpreted as
344a standard n-dimensional C-array. Otherwise, the consumer must access an
345n-dimensional array as follows:
346
347 ``ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1]``
348 ``item = *((typeof(item) *)ptr);``
349
350
351As noted above, :c:member:`~Py_buffer.buf` can point to any location within
352the actual memory block. An exporter can check the validity of a buffer with
353this function:
354
355.. code-block:: python
356
357 def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
358 """Verify that the parameters represent a valid array within
359 the bounds of the allocated memory:
360 char *mem: start of the physical memory block
361 memlen: length of the physical memory block
362 offset: (char *)buf - mem
363 """
364 if offset % itemsize:
365 return False
366 if offset < 0 or offset+itemsize > memlen:
367 return False
368 if any(v % itemsize for v in strides):
369 return False
370
371 if ndim <= 0:
372 return ndim == 0 and not shape and not strides
373 if 0 in shape:
374 return True
375
376 imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
377 if strides[j] <= 0)
378 imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
379 if strides[j] > 0)
380
381 return 0 <= offset+imin and offset+imax+itemsize <= memlen
382
383
384PIL-style: shape, strides and suboffsets
385~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
386
387In addition to the regular items, PIL-style arrays can contain pointers
388that must be followed in order to get to the next element in a dimension.
389For example, the regular three-dimensional C-array ``char v[2][2][3]`` can
390also be viewed as an array of 2 pointers to 2 two-dimensional arrays:
391``char (*v[2])[2][3]``. In suboffsets representation, those two pointers
392can be embedded at the start of :c:member:`~Py_buffer.buf`, pointing
393to two ``char x[2][3]`` arrays that can be located anywhere in memory.
394
395
396Here is a function that returns a pointer to the element in an N-D array
397pointed to by an N-dimensional index when there are both non-NULL strides
398and suboffsets::
399
400 void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
401 Py_ssize_t *suboffsets, Py_ssize_t *indices) {
402 char *pointer = (char*)buf;
403 int i;
404 for (i = 0; i < ndim; i++) {
405 pointer += strides[i] * indices[i];
406 if (suboffsets[i] >=0 ) {
407 pointer = *((char**)pointer) + suboffsets[i];
408 }
409 }
410 return (void*)pointer;
411 }
412
Georg Brandl54a3faa2008-01-20 09:30:57 +0000413
Antoine Pitrouc663b582010-09-28 23:59:51 +0000414Buffer-related functions
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000415========================
Georg Brandl54a3faa2008-01-20 09:30:57 +0000416
Georg Brandl60203b42010-10-06 10:11:56 +0000417.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000418
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000419 Return 1 if *obj* supports the buffer interface otherwise 0. When 1 is
Georg Brandl60203b42010-10-06 10:11:56 +0000420 returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
Antoine Pitrou99a00a42010-09-28 23:04:04 +0000421 succeed.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000422
423
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100424.. c:function:: int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000425
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100426 Send a request to *exporter* to fill in *view* as specified by *flags*.
427 If the exporter cannot provide a buffer of the exact type, it MUST raise
428 :c:data:`PyExc_BufferError`, set :c:member:`view->obj` to *NULL* and
429 return -1.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000430
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100431 On success, fill in *view*, set :c:member:`view->obj` to a new reference
Stefan Krahabd887d2012-03-06 14:55:06 +0100432 to *exporter* and return 0. In the case of chained buffer providers
433 that redirect requests to a single object, :c:member:`view->obj` MAY
434 refer to this object instead of *exporter* (See :ref:`Buffer Object Structures <buffer-structs>`).
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000435
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100436 Successful calls to :c:func:`PyObject_GetBuffer` must be paired with calls
437 to :c:func:`PyBuffer_Release`, similar to :c:func:`malloc` and :c:func:`free`.
438 Thus, after the consumer is done with the buffer, :c:func:`PyBuffer_Release`
439 must be called exactly once.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000440
441
Georg Brandl60203b42010-10-06 10:11:56 +0000442.. c:function:: void PyBuffer_Release(Py_buffer *view)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000443
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100444 Release the buffer *view* and decrement the reference count for
445 :c:member:`view->obj`. This function MUST be called when the buffer
446 is no longer being used, otherwise reference leaks may occur.
447
448 It is an error to call this function on a buffer that was not obtained via
449 :c:func:`PyObject_GetBuffer`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000450
451
Georg Brandl60203b42010-10-06 10:11:56 +0000452.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000453
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100454 Return the implied :c:data:`~Py_buffer.itemsize` from :c:data:`~Py_buffer.format`.
455 This function is not yet implemented.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000456
457
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100458.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char order)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000459
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100460 Return 1 if the memory defined by the *view* is C-style (*order* is
461 ``'C'``) or Fortran-style (*order* is ``'F'``) contiguous or either one
462 (*order* is ``'A'``). Return 0 otherwise.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000463
464
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100465.. 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 +0000466
467 Fill the *strides* array with byte-strides of a contiguous (C-style if
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100468 *order* is ``'C'`` or Fortran-style if *order* is ``'F'``) array of the
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000469 given shape with the given number of bytes per element.
470
471
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100472.. 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 +0000473
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100474 Handle buffer requests for an exporter that wants to expose *buf* of size *len*
475 with writability set according to *readonly*. *buf* is interpreted as a sequence
476 of unsigned bytes.
477
478 The *flags* argument indicates the request type. This function always fills in
479 *view* as specified by flags, unless *buf* has been designated as read-only
480 and :c:macro:`PyBUF_WRITABLE` is set in *flags*.
481
482 On success, set :c:member:`view->obj` to a new reference to *exporter* and
483 return 0. Otherwise, raise :c:data:`PyExc_BufferError`, set
484 :c:member:`view->obj` to *NULL* and return -1;
485
486 If this function is used as part of a :ref:`getbufferproc <buffer-structs>`,
487 *exporter* MUST be set to the exporting object. Otherwise, *exporter* MUST
488 be NULL.
489
490
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000491