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