Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _bufferobjects: |
| 4 | |
Antoine Pitrou | f7ba2fa | 2010-09-28 23:39:41 +0000 | [diff] [blame] | 5 | Buffer Protocol |
| 6 | --------------- |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 7 | |
| 8 | .. sectionauthor:: Greg Stein <gstein@lyra.org> |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 9 | .. sectionauthor:: Benjamin Peterson |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 10 | .. sectionauthor:: Stefan Krah |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | .. index:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 14 | single: buffer interface |
| 15 | |
Antoine Pitrou | 8abc935 | 2010-12-12 19:59:47 +0000 | [diff] [blame] | 16 | Certain objects available in Python wrap access to an underlying memory |
| 17 | array or *buffer*. Such objects include the built-in :class:`bytes` and |
| 18 | :class:`bytearray`, and some extension types like :class:`array.array`. |
| 19 | Third-party libraries may define their own types for special purposes, such |
| 20 | as image processing or numeric analysis. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 21 | |
Antoine Pitrou | 8abc935 | 2010-12-12 19:59:47 +0000 | [diff] [blame] | 22 | While each of these types have their own semantics, they share the common |
| 23 | characteristic of being backed by a possibly large memory buffer. It is |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 24 | then desirable, in some situations, to access that buffer directly and |
Antoine Pitrou | 8abc935 | 2010-12-12 19:59:47 +0000 | [diff] [blame] | 25 | without intermediate copying. |
| 26 | |
| 27 | Python provides such a facility at the C level in the form of the *buffer |
| 28 | protocol*. 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 | |
| 39 | Simple objects such as :class:`bytes` and :class:`bytearray` expose their |
| 40 | underlying buffer in byte-oriented form. Other forms are possible; for example, |
| 41 | the elements exposed by a :class:`array.array` can be multi-byte values. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 43 | An example consumer of the buffer interface is the :meth:`~io.BufferedIOBase.write` |
| 44 | method of file objects: any object that can export a series of bytes through |
| 45 | the buffer interface can be written to a file. While :meth:`write` only |
| 46 | needs read-only access to the internal contents of the object passed to it, |
| 47 | other methods such as :meth:`~io.BufferedIOBase.readinto` need write access |
| 48 | to the contents of their argument. The buffer interface allows objects to |
| 49 | selectively allow or reject exporting of read-write and read-only buffers. |
| 50 | |
| 51 | There are two ways for a consumer of the buffer interface to acquire a buffer |
| 52 | over a target object: |
| 53 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 54 | * call :c:func:`PyObject_GetBuffer` with the right parameters; |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 55 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 56 | * call :c:func:`PyArg_ParseTuple` (or one of its siblings) with one of the |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 57 | ``y*``, ``w*`` or ``s*`` :ref:`format codes <arg-parsing>`. |
| 58 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 59 | In both cases, :c:func:`PyBuffer_Release` must be called when the buffer |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 60 | isn't needed anymore. Failure to do so could lead to various issues such as |
| 61 | resource leaks. |
| 62 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 64 | .. _buffer-structure: |
| 65 | |
| 66 | Buffer structure |
| 67 | ================ |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 68 | |
Antoine Pitrou | f7ba2fa | 2010-09-28 23:39:41 +0000 | [diff] [blame] | 69 | Buffer structures (or simply "buffers") are useful as a way to expose the |
| 70 | binary data from another object to the Python programmer. They can also be |
| 71 | used as a zero-copy slicing mechanism. Using their ability to reference a |
| 72 | block of memory, it is possible to expose any data to the Python programmer |
| 73 | quite easily. The memory could be a large, constant array in a C extension, |
| 74 | it could be a raw block of memory for manipulation before passing to an |
| 75 | operating system library, or it could be used to pass around structured data |
| 76 | in its native, in-memory format. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | f7ba2fa | 2010-09-28 23:39:41 +0000 | [diff] [blame] | 78 | Contrary to most data types exposed by the Python interpreter, buffers |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 79 | are not :c:type:`PyObject` pointers but rather simple C structures. This |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 80 | allows them to be created and copied very simply. When a generic wrapper |
Antoine Pitrou | c663b58 | 2010-09-28 23:59:51 +0000 | [diff] [blame] | 81 | around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 82 | can be created. |
| 83 | |
Stefan Krah | abd887d | 2012-03-06 14:55:06 +0100 | [diff] [blame] | 84 | For short instructions how to write an exporting object, see |
| 85 | :ref:`Buffer Object Structures <buffer-structs>`. For obtaining |
| 86 | a buffer, see :c:func:`PyObject_GetBuffer`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 87 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. c:type:: Py_buffer |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 89 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 90 | .. c:member:: void \*obj |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 91 | |
Stefan Krah | abd887d | 2012-03-06 14:55:06 +0100 | [diff] [blame] | 92 | 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 Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 96 | |
Stefan Krah | abd887d | 2012-03-06 14:55:06 +0100 | [diff] [blame] | 97 | 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 Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 101 | |
| 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 Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 111 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 112 | .. c:member:: Py_ssize_t len |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 113 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 114 | ``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 Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 122 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 123 | .. c:member:: int readonly |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 124 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 125 | An indicator of whether the buffer is read-only. This field is controlled |
| 126 | by the :c:macro:`PyBUF_WRITABLE` flag. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 127 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 128 | .. c:member:: Py_ssize_t itemsize |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 129 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 130 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 132 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 133 | 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 Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 206 | |
| 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 Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 210 | freed when the buffer is released. The consumer MUST NOT alter this |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 211 | value. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 212 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 213 | .. _buffer-request-types: |
| 214 | |
| 215 | Buffer request types |
| 216 | ==================== |
| 217 | |
| 218 | Buffers are usually obtained by sending a buffer request to an exporting |
| 219 | object via :c:func:`PyObject_GetBuffer`. Since the complexity of the logical |
| 220 | structure of the memory can vary drastically, the consumer uses the *flags* |
| 221 | argument to specify the exact buffer type it can handle. |
| 222 | |
| 223 | All :c:data:`Py_buffer` fields are unambiguously defined by the request |
| 224 | type. |
| 225 | |
| 226 | request-independent fields |
| 227 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 228 | The following fields are not influenced by *flags* and must always be filled in |
| 229 | with 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 | |
| 233 | readonly, 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. |
| 250 | Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE` |
| 251 | can 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`. |
| 254 | The latter already implies format ``B`` (unsigned bytes). |
| 255 | |
| 256 | |
| 257 | shape, strides, suboffsets |
| 258 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 259 | |
| 260 | The flags that control the logical structure of the memory are listed |
| 261 | in decreasing order of complexity. Note that each flag contains all bits |
| 262 | of 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 | |
| 278 | contiguity requests |
| 279 | ~~~~~~~~~~~~~~~~~~~ |
| 280 | |
| 281 | C or Fortran contiguity can be explicitly requested, with and without stride |
| 282 | information. 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 | |
| 297 | compound requests |
| 298 | ~~~~~~~~~~~~~~~~~ |
| 299 | |
| 300 | All possible requests are fully defined by some combination of the flags in |
| 301 | the previous section. For convenience, the buffer protocol provides frequently |
| 302 | used combinations as single flags. |
| 303 | |
| 304 | In the following table *U* stands for undefined contiguity. The consumer would |
| 305 | have 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 | |
| 330 | Complex arrays |
| 331 | ============== |
| 332 | |
| 333 | NumPy-style: shape and strides |
| 334 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 335 | |
| 336 | The 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 | |
| 339 | If ``ndim == 0``, the memory location pointed to by :c:member:`~Py_buffer.buf` is |
| 340 | interpreted as a scalar of size :c:member:`~Py_buffer.itemsize`. In that case, |
| 341 | both :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides` are *NULL*. |
| 342 | |
| 343 | If :c:member:`~Py_buffer.strides` is *NULL*, the array is interpreted as |
| 344 | a standard n-dimensional C-array. Otherwise, the consumer must access an |
| 345 | n-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 | |
| 351 | As noted above, :c:member:`~Py_buffer.buf` can point to any location within |
| 352 | the actual memory block. An exporter can check the validity of a buffer with |
| 353 | this 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 | |
| 384 | PIL-style: shape, strides and suboffsets |
| 385 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 386 | |
| 387 | In addition to the regular items, PIL-style arrays can contain pointers |
| 388 | that must be followed in order to get to the next element in a dimension. |
| 389 | For example, the regular three-dimensional C-array ``char v[2][2][3]`` can |
| 390 | also 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 |
| 392 | can be embedded at the start of :c:member:`~Py_buffer.buf`, pointing |
| 393 | to two ``char x[2][3]`` arrays that can be located anywhere in memory. |
| 394 | |
| 395 | |
| 396 | Here is a function that returns a pointer to the element in an N-D array |
| 397 | pointed to by an N-dimensional index when there are both non-NULL strides |
| 398 | and 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | c663b58 | 2010-09-28 23:59:51 +0000 | [diff] [blame] | 414 | Buffer-related functions |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 415 | ======================== |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 416 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 417 | .. c:function:: int PyObject_CheckBuffer(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 419 | Return 1 if *obj* supports the buffer interface otherwise 0. When 1 is |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 420 | returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will |
Antoine Pitrou | 99a00a4 | 2010-09-28 23:04:04 +0000 | [diff] [blame] | 421 | succeed. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 422 | |
| 423 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 424 | .. c:function:: int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 425 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 426 | 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 Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 430 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 431 | On success, fill in *view*, set :c:member:`view->obj` to a new reference |
Stefan Krah | abd887d | 2012-03-06 14:55:06 +0100 | [diff] [blame] | 432 | 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 Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 435 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 436 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 440 | |
| 441 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 442 | .. c:function:: void PyBuffer_Release(Py_buffer *view) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 443 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 444 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 450 | |
| 451 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 452 | .. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 453 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 454 | Return the implied :c:data:`~Py_buffer.itemsize` from :c:data:`~Py_buffer.format`. |
| 455 | This function is not yet implemented. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 456 | |
| 457 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 458 | .. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char order) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 459 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 460 | 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 Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 463 | |
| 464 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 465 | .. c:function:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char order) |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 466 | |
| 467 | Fill the *strides* array with byte-strides of a contiguous (C-style if |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 468 | *order* is ``'C'`` or Fortran-style if *order* is ``'F'``) array of the |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 469 | given shape with the given number of bytes per element. |
| 470 | |
| 471 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 472 | .. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags) |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 473 | |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 474 | 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 Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame] | 491 | |