Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _bufferobjects: |
| 4 | |
| 5 | Buffer Objects |
| 6 | -------------- |
| 7 | |
| 8 | .. sectionauthor:: Greg Stein <gstein@lyra.org> |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 9 | .. sectionauthor:: Benjamin Peterson |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | .. index:: |
| 13 | object: buffer |
| 14 | single: buffer interface |
| 15 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 16 | Python objects implemented in C can export a "buffer interface." These |
| 17 | functions can be used by an object to expose its data in a raw, byte-oriented |
| 18 | format. Clients of the object can use the buffer interface to access the object |
| 19 | data directly, without needing to copy it first. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 20 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 21 | Two examples of objects that support the buffer interface are bytes and |
| 22 | arrays. The bytes object exposes the character contents in the buffer |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 23 | interface's byte-oriented form. An array can also expose its contents, but it |
| 24 | should be noted that array elements may be multi-byte values. |
| 25 | |
| 26 | An example user of the buffer interface is the file object's :meth:`write` |
| 27 | method. Any object that can export a series of bytes through the buffer |
| 28 | interface can be written to a file. There are a number of format codes to |
| 29 | :cfunc:`PyArg_ParseTuple` that operate against an object's buffer interface, |
| 30 | returning data from the target object. |
| 31 | |
| 32 | .. index:: single: PyBufferProcs |
| 33 | |
| 34 | More information on the buffer interface is provided in the section |
| 35 | :ref:`buffer-structs`, under the description for :ctype:`PyBufferProcs`. |
| 36 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 37 | Buffer objects are useful as a way to expose the data from another object's |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 38 | buffer interface to the Python programmer. They can also be used as a zero-copy |
| 39 | slicing mechanism. Using their ability to reference a block of memory, it is |
| 40 | possible to expose any data to the Python programmer quite easily. The memory |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 41 | could be a large, constant array in a C extension, it could be a raw block of |
| 42 | memory for manipulation before passing to an operating system library, or it |
| 43 | could be used to pass around structured data in its native, in-memory format. |
| 44 | |
| 45 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 46 | .. ctype:: Py_buffer |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 47 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 48 | .. cmember:: void *buf |
| 49 | |
| 50 | A pointer to the start of the memory for the object. |
| 51 | |
| 52 | .. cmember:: Py_ssize_t len |
| 53 | |
| 54 | The total length of the memory in bytes. |
| 55 | |
| 56 | .. cmember:: int readonly |
| 57 | |
| 58 | An indicator of whether the buffer is read only. |
| 59 | |
| 60 | .. cmember:: const char *format |
| 61 | |
| 62 | A *NULL* terminated string in :mod:`struct` module style syntax giving the |
| 63 | contents of the elements available through the buffer. If this is *NULL*, |
| 64 | ``"B"`` (unsigned bytes) is assumed. |
| 65 | |
| 66 | .. cmember:: int ndim |
| 67 | |
| 68 | The number of dimensions the memory represents as a multi-dimensional |
| 69 | array. If it is 0, :cdata:`strides` and :cdata:`suboffsets` must be |
| 70 | *NULL*. |
| 71 | |
| 72 | .. cmember:: Py_ssize_t *shape |
| 73 | |
| 74 | An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim` giving the |
| 75 | shape of the memory as a multi-dimensional array. Note that |
| 76 | ``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to |
| 77 | :cdata:`len`. |
| 78 | |
| 79 | .. cmember:: Py_ssize_t *strides |
| 80 | |
| 81 | An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim` giving the |
| 82 | number of bytes to skip to get to a new element in each dimension. |
| 83 | |
| 84 | .. cmember:: Py_ssize_t *suboffsets |
| 85 | |
| 86 | An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim`. If these |
| 87 | suboffset numbers are greater than or equal to 0, then the value stored |
| 88 | along the indicated dimension is a pointer and the suboffset value |
| 89 | dictates how many bytes to add to the pointer after de-referencing. A |
| 90 | suboffset value that it negative indicates that no de-referencing should |
| 91 | occur (striding in a contiguous memory block). |
| 92 | |
| 93 | Here is a function that returns a pointer to the element in an N-D array |
| 94 | pointed to by an N-dimesional index when there are both non-NULL strides |
| 95 | and suboffsets:: |
| 96 | |
| 97 | void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides, |
| 98 | Py_ssize_t *suboffsets, Py_ssize_t *indices) { |
| 99 | char *pointer = (char*)buf; |
| 100 | int i; |
| 101 | for (i = 0; i < ndim; i++) { |
| 102 | pointer += strides[i] * indices[i]; |
| 103 | if (suboffsets[i] >=0 ) { |
| 104 | pointer = *((char**)pointer) + suboffsets[i]; |
| 105 | } |
| 106 | } |
| 107 | return (void*)pointer; |
| 108 | } |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 109 | |
| 110 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 111 | .. cmember:: Py_ssize_t itemsize |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 112 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 113 | This is a storage for the itemsize (in bytes) of each element of the |
| 114 | shared memory. It is technically un-necessary as it can be obtained using |
| 115 | :cfunc:`PyBuffer_SizeFromFormat`, however an exporter may know this |
| 116 | information without parsing the format string and it is necessary to know |
| 117 | the itemsize for proper interpretation of striding. Therefore, storing it |
| 118 | is more convenient and faster. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 119 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 120 | .. cmember:: void *internal |
| 121 | |
| 122 | This is for use internally by the exporting object. For example, this |
| 123 | might be re-cast as an integer by the exporter and used to store flags |
| 124 | about whether or not the shape, strides, and suboffsets arrays must be |
| 125 | freed when the buffer is released. The consumer should never alter this |
| 126 | value. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 127 | |
| 128 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 129 | Buffer related functions |
| 130 | ======================== |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 131 | |
| 132 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 133 | .. cfunction:: int PyObject_CheckBuffer(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 134 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 135 | Return 1 if *obj* supports the buffer interface otherwise 0. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 136 | |
| 137 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 138 | .. cfunction:: int PyObject_GetBuffer(PyObject *obj, PyObject *view, int flags) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 139 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 140 | Export *obj* into a :ctype:`Py_buffer`, *view*. These arguments must |
| 141 | never be *NULL*. The *flags* argument is a bit field indicating what kind |
| 142 | of buffer the caller is prepared to deal with and therefore what kind of |
| 143 | buffer the exporter is allowed to return. The buffer interface allows for |
| 144 | complicated memory sharing possibilities, but some caller may not be able |
| 145 | to handle all the complexibity but may want to see if the exporter will |
| 146 | let them take a simpler view to its memory. |
| 147 | |
| 148 | Some exporters may not be able to share memory in every possible way and |
| 149 | may need to raise errors to signal to some consumers that something is |
| 150 | just not possible. These errors should be a :exc:`BufferError` unless |
| 151 | there is another error that is actually causing the problem. The exporter |
| 152 | can use flags information to simplify how much of the :cdata:`Py_buffer` |
| 153 | structure is filled in with non-default values and/or raise an error if |
| 154 | the object can't support a simpler view of its memory. |
| 155 | |
| 156 | 0 is returned on success and -1 on error. |
| 157 | |
| 158 | The following table gives possible values to the *flags* arguments. |
| 159 | |
| 160 | +------------------------------+-----------------------------------------------+ |
| 161 | | Flag | Description | |
| 162 | +==============================+===============================================+ |
| 163 | | :cmacro:`PyBUF_SIMPLE` |This is the default flag state. The returned | |
| 164 | | |buffer may or may not have writable memory. | |
| 165 | | |The format will be assumed to be unsigned bytes| |
| 166 | | |. This is a "stand-alone" flag constant. It | |
| 167 | | |never needs to be |'d to the others. The | |
| 168 | | |exporter will raise an error if it cannot | |
| 169 | | |provide such a contiguous buffer of bytes. | |
| 170 | | | | |
| 171 | +------------------------------+-----------------------------------------------+ |
| 172 | | :cmacro:`PyBUF_WRITABLE` |The returned buffer must be writable. If it is | |
| 173 | | |not writable, then raise an error. | |
| 174 | +------------------------------+-----------------------------------------------+ |
| 175 | | :cmacro:`PyBUF_STRIDES` |This implies :cmacro:`PyBUF_ND`. The returned | |
| 176 | | |buffer must provide strides information | |
| 177 | | |(i.e. the strides cannot be NULL). This would | |
| 178 | | |be used when the consumer can handle strided, | |
| 179 | | |discontiguous arrays. Handling strides | |
| 180 | | |automatically assumes you can handle shape. The| |
| 181 | | |exporter may raise an error if cannot provide a| |
| 182 | | |strided-only representation of the data | |
| 183 | | |(i.e. without the suboffsets). | |
| 184 | +------------------------------+-----------------------------------------------+ |
| 185 | | :cmacro:`PyBUF_ND` |The returned buffer must provide shape | |
| 186 | | |information. The memory will be assumed C-style| |
| 187 | | |contiguous (last dimension varies the | |
| 188 | | |fastest). The exporter may raise an error if it| |
| 189 | | |cannot provide this kind of contiguous | |
| 190 | | |buffer. If this is not given then shape will be| |
| 191 | | |*NULL*. | |
| 192 | | | | |
| 193 | | | | |
| 194 | +------------------------------+-----------------------------------------------+ |
| 195 | |:cmacro:`PyBUF_C_CONTIGUOUS` |These flags indicate that the contiguoity | |
| 196 | |:cmacro:`PyBUF_F_CONTIGUOUS` |returned buffer must be respectively, | |
| 197 | |:cmacro:`PyBUF_ANY_CONTIGUOUS`|C-contiguous (last dimension varies the | |
| 198 | | |fastest), Fortran contiguous (first dimension | |
| 199 | | |varies the fastest) or either one. All of | |
| 200 | | |these flags imply :cmacro:`PyBUF_STRIDES` and | |
| 201 | | |guarantee that the strides buffer info | |
| 202 | | |structure will be filled in correctly. | |
| 203 | | | | |
| 204 | +------------------------------+-----------------------------------------------+ |
| 205 | | :cmacro:`PyBUF_INDIRECT` |This implies :cmacro:`PyBUF_STRIDES`. The | |
| 206 | | |returned buffer must have suboffsets | |
| 207 | | |information (which can be NULL if no suboffsets| |
| 208 | | |are needed). This would be used when the | |
| 209 | | |consumer can handle indirect array referencing | |
| 210 | | |implied by these suboffsets. | |
| 211 | | | | |
| 212 | | | | |
| 213 | | | | |
| 214 | +------------------------------+-----------------------------------------------+ |
| 215 | | :cmacro:`PyBUF_FORMAT` |The returned buffer must have true format | |
| 216 | | |information if this flag is provided. This | |
| 217 | | |would be used when the consumer is going to be | |
| 218 | | |checking for what 'kind' of data is actually | |
| 219 | | |stored. An exporter should always be able to | |
| 220 | | |provide this information if requested. If | |
| 221 | | |format is not explicitly requested then the | |
| 222 | | |format must be returned as *NULL* (which means | |
| 223 | | |``'B'``, or unsigned bytes) | |
| 224 | +------------------------------+-----------------------------------------------+ |
| 225 | | :cmacro:`PyBUF_STRIDED` |This is equivalent to ``(PyBUF_STRIDES | | |
| 226 | | |PyBUF_WRITABLE)``. | |
| 227 | +------------------------------+-----------------------------------------------+ |
| 228 | | :cmacro:`PyBUF_STRIDED_RO` |This is equivalent to ``(PyBUF_STRIDES)``. | |
| 229 | | | | |
| 230 | +------------------------------+-----------------------------------------------+ |
| 231 | | :cmacro:`PyBUF_RECORDS` |This is equivalent to ``(PyBUF_STRIDES | | |
| 232 | | |PyBUF_FORMAT | PyBUF_WRITABLE)``. | |
| 233 | +------------------------------+-----------------------------------------------+ |
| 234 | | :cmacro:`PyBUF_RECORDS_RO` |This is equivalent to ``(PyBUF_STRIDES | | |
| 235 | | |PyBUF_FORMAT)``. | |
| 236 | +------------------------------+-----------------------------------------------+ |
| 237 | | :cmacro:`PyBUF_FULL` |This is equivalent to ``(PyBUF_INDIRECT | | |
| 238 | | |PyBUF_FORMAT | PyBUF_WRITABLE)``. | |
| 239 | +------------------------------+-----------------------------------------------+ |
| 240 | | :cmacro:`PyBUF_FULL_RO`` |This is equivalent to ``(PyBUF_INDIRECT | | |
| 241 | | |PyBUF_FORMAT)``. | |
| 242 | +------------------------------+-----------------------------------------------+ |
| 243 | | :cmacro:`PyBUF_CONTIG` |This is equivalent to ``(PyBUF_ND | | |
| 244 | | |PyBUF_WRITABLE)``. | |
| 245 | +------------------------------+-----------------------------------------------+ |
| 246 | | :cmacro:`PyBUF_CONTIG_RO` |This is equivalent to ``(PyBUF_ND)``. | |
| 247 | | | | |
| 248 | +------------------------------+-----------------------------------------------+ |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 249 | |
| 250 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 251 | .. cfunction:: void PyBuffer_Release(PyObject *obj, Py_buffer *view) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 252 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 253 | Release the buffer *view* over *obj*. This shouldd be called when the buffer |
| 254 | is no longer being used as it may free memory from it. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 255 | |
| 256 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 257 | .. cfunction:: Py_ssize_t PyBuffer_SizeFromFormat(const char *) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 258 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 259 | Return the implied :cdata:`~Py_buffer.itemsize` from the struct-stype |
| 260 | :cdata:`~Py_buffer.format`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 261 | |
| 262 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 263 | .. cfunction:: int PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fortran) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 264 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 265 | Copy *len* bytes of data pointed to by the contiguous chunk of memory pointed |
| 266 | to by *buf* into the buffer exported by obj. The buffer must of course be |
| 267 | writable. Return 0 on success and return -1 and raise an error on failure. |
| 268 | If the object does not have a writable buffer, then an error is raised. If |
| 269 | *fortran* is ``'F'``, then if the object is multi-dimensional, then the data |
| 270 | will be copied into the array in Fortran-style (first dimension varies the |
| 271 | fastest). If *fortran* is ``'C'``, then the data will be copied into the |
| 272 | array in C-style (last dimension varies the fastest). If *fortran* is |
| 273 | ``'A'``, then it does not matter and the copy will be made in whatever way is |
| 274 | more efficient. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 275 | |
| 276 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 277 | .. cfunction:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 278 | |
Benjamin Peterson | 9d0ced3 | 2008-09-16 02:24:31 +0000 | [diff] [blame^] | 279 | Return 1 if the memory defined by the *view* is C-style (*fortran* is |
| 280 | ``'C'``) or Fortran-style (*fortran* is ``'F'``) contiguous or either one |
| 281 | (*fortran* is ``'A'``). Return 0 otherwise. |
| 282 | |
| 283 | |
| 284 | .. cfunction:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran) |
| 285 | |
| 286 | Fill the *strides* array with byte-strides of a contiguous (C-style if |
| 287 | *fortran* is ``'C'`` or Fortran-style if *fortran* is ``'F'`` array of the |
| 288 | given shape with the given number of bytes per element. |
| 289 | |
| 290 | |
| 291 | .. cfunction:: int PyBuffer_FillInfo(Py_buffer *view, void *buf, Py_ssize_t len, int readonly, int infoflags) |
| 292 | |
| 293 | Fill in a buffer-info structure, *view*, correctly for an exporter that can |
| 294 | only share a contiguous chunk of memory of "unsigned bytes" of the given |
| 295 | length. Return 0 on success and -1 (with raising an error) on error. |
| 296 | |
| 297 | |
| 298 | MemoryView objects |
| 299 | ================== |
| 300 | |
| 301 | A memoryview object is an extended buffer object that could replace the buffer |
| 302 | object (but doesn't have to as that could be kept as a simple 1-d memoryview |
| 303 | object). It, unlike :ctype:`Py_buffer`, is a Python object (exposed as |
| 304 | :class:`memoryview` in :mod:`builtins`), so it can be used with Python code. |
| 305 | |
| 306 | .. cfunction:: PyObject* PyMemoryView_FromObject(PyObject *obj) |
| 307 | |
| 308 | Return a memoryview object from an object that defines the buffer interface. |