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