Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _bufferobjects: |
| 4 | |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 5 | Buffers and Memoryview Objects |
| 6 | ------------------------------ |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 7 | |
| 8 | .. sectionauthor:: Greg Stein <gstein@lyra.org> |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 9 | .. sectionauthor:: Benjamin Peterson |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | .. index:: |
| 13 | object: buffer |
| 14 | single: buffer interface |
| 15 | |
| 16 | Python objects implemented in C can export a group of functions called the |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 17 | "buffer interface." These functions can be used by an object to expose its |
| 18 | data in a raw, byte-oriented format. Clients of the object can use the buffer |
| 19 | interface to access the object data directly, without needing to copy it |
| 20 | first. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 21 | |
| 22 | Two examples of objects that support the buffer interface are strings and |
| 23 | arrays. The string object exposes the character contents in the buffer |
Stefan Krah | 32ae437 | 2014-04-26 22:50:25 +0200 | [diff] [blame] | 24 | interface's byte-oriented form. An array can only expose its contents via the |
| 25 | old-style buffer interface. This limitation does not apply to Python 3, |
| 26 | where :class:`memoryview` objects can be constructed from arrays, too. |
| 27 | Array elements may be multi-byte values. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 28 | |
| 29 | An example user of the buffer interface is the file object's :meth:`write` |
| 30 | method. Any object that can export a series of bytes through the buffer |
| 31 | interface can be written to a file. There are a number of format codes to |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 32 | :c:func:`PyArg_ParseTuple` that operate against an object's buffer interface, |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 33 | returning data from the target object. |
| 34 | |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 35 | Starting from version 1.6, Python has been providing Python-level buffer |
Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 36 | objects and a C-level buffer API so that any built-in or used-defined type can |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 37 | expose its characteristics. Both, however, have been deprecated because of |
Ezio Melotti | 510ff54 | 2012-05-03 19:21:40 +0300 | [diff] [blame] | 38 | various shortcomings, and have been officially removed in Python 3 in favour |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 39 | of a new C-level buffer API and a new Python-level object named |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 40 | :class:`memoryview`. |
| 41 | |
| 42 | The new buffer API has been backported to Python 2.6, and the |
| 43 | :class:`memoryview` object has been backported to Python 2.7. It is strongly |
| 44 | advised to use them rather than the old APIs, unless you are blocked from |
| 45 | doing so for compatibility reasons. |
| 46 | |
| 47 | |
| 48 | The new-style Py_buffer struct |
| 49 | ============================== |
| 50 | |
| 51 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 52 | .. c:type:: Py_buffer |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 53 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 54 | .. c:member:: void *buf |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 55 | |
| 56 | A pointer to the start of the memory for the object. |
| 57 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 58 | .. c:member:: Py_ssize_t len |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 59 | :noindex: |
| 60 | |
| 61 | The total length of the memory in bytes. |
| 62 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 63 | .. c:member:: int readonly |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 64 | |
| 65 | An indicator of whether the buffer is read only. |
| 66 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 67 | .. c:member:: const char *format |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 68 | :noindex: |
| 69 | |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 70 | A *NULL* terminated string in :mod:`struct` module style syntax giving |
| 71 | the contents of the elements available through the buffer. If this is |
| 72 | *NULL*, ``"B"`` (unsigned bytes) is assumed. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 73 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 74 | .. c:member:: int ndim |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 75 | |
| 76 | The number of dimensions the memory represents as a multi-dimensional |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 77 | array. If it is 0, :c:data:`strides` and :c:data:`suboffsets` must be |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 78 | *NULL*. |
| 79 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 80 | .. c:member:: Py_ssize_t *shape |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 81 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 82 | An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 83 | shape of the memory as a multi-dimensional array. Note that |
| 84 | ``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 85 | :c:data:`len`. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 86 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 87 | .. c:member:: Py_ssize_t *strides |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 88 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 89 | An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 90 | number of bytes to skip to get to a new element in each dimension. |
| 91 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 92 | .. c:member:: Py_ssize_t *suboffsets |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 93 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 94 | An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim`. If these |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 95 | suboffset numbers are greater than or equal to 0, then the value stored |
| 96 | along the indicated dimension is a pointer and the suboffset value |
| 97 | dictates how many bytes to add to the pointer after de-referencing. A |
| 98 | suboffset value that it negative indicates that no de-referencing should |
| 99 | occur (striding in a contiguous memory block). |
| 100 | |
| 101 | Here is a function that returns a pointer to the element in an N-D array |
| 102 | pointed to by an N-dimesional index when there are both non-NULL strides |
| 103 | and suboffsets:: |
| 104 | |
| 105 | void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides, |
| 106 | Py_ssize_t *suboffsets, Py_ssize_t *indices) { |
| 107 | char *pointer = (char*)buf; |
| 108 | int i; |
| 109 | for (i = 0; i < ndim; i++) { |
| 110 | pointer += strides[i] * indices[i]; |
| 111 | if (suboffsets[i] >=0 ) { |
| 112 | pointer = *((char**)pointer) + suboffsets[i]; |
| 113 | } |
| 114 | } |
| 115 | return (void*)pointer; |
| 116 | } |
| 117 | |
| 118 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 119 | .. c:member:: Py_ssize_t itemsize |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 120 | |
| 121 | This is a storage for the itemsize (in bytes) of each element of the |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 122 | shared memory. It is technically un-necessary as it can be obtained |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 123 | using :c:func:`PyBuffer_SizeFromFormat`, however an exporter may know |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 124 | this information without parsing the format string and it is necessary |
| 125 | to know the itemsize for proper interpretation of striding. Therefore, |
| 126 | storing it is more convenient and faster. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 127 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 128 | .. c:member:: void *internal |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 129 | |
| 130 | This is for use internally by the exporting object. For example, this |
| 131 | might be re-cast as an integer by the exporter and used to store flags |
| 132 | about whether or not the shape, strides, and suboffsets arrays must be |
| 133 | freed when the buffer is released. The consumer should never alter this |
| 134 | value. |
| 135 | |
| 136 | |
| 137 | Buffer related functions |
| 138 | ======================== |
| 139 | |
| 140 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 141 | .. c:function:: int PyObject_CheckBuffer(PyObject *obj) |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 142 | |
| 143 | Return 1 if *obj* supports the buffer interface otherwise 0. |
| 144 | |
| 145 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 146 | .. c:function:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 147 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 148 | Export *obj* into a :c:type:`Py_buffer`, *view*. These arguments must |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 149 | never be *NULL*. The *flags* argument is a bit field indicating what |
| 150 | kind of buffer the caller is prepared to deal with and therefore what |
| 151 | kind of buffer the exporter is allowed to return. The buffer interface |
| 152 | allows for complicated memory sharing possibilities, but some caller may |
Georg Brandl | 4a46e1c | 2009-08-06 17:43:55 +0000 | [diff] [blame] | 153 | not be able to handle all the complexity but may want to see if the |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 154 | exporter will let them take a simpler view to its memory. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 155 | |
| 156 | Some exporters may not be able to share memory in every possible way and |
| 157 | may need to raise errors to signal to some consumers that something is |
| 158 | just not possible. These errors should be a :exc:`BufferError` unless |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 159 | there is another error that is actually causing the problem. The |
| 160 | exporter can use flags information to simplify how much of the |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 161 | :c:data:`Py_buffer` structure is filled in with non-default values and/or |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 162 | raise an error if the object can't support a simpler view of its memory. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 163 | |
| 164 | 0 is returned on success and -1 on error. |
| 165 | |
| 166 | The following table gives possible values to the *flags* arguments. |
| 167 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 168 | +-------------------------------+---------------------------------------------------+ |
| 169 | | Flag | Description | |
| 170 | +===============================+===================================================+ |
| 171 | | :c:macro:`PyBUF_SIMPLE` | This is the default flag state. The returned | |
| 172 | | | buffer may or may not have writable memory. The | |
| 173 | | | format of the data will be assumed to be unsigned | |
| 174 | | | bytes. This is a "stand-alone" flag constant. It | |
| 175 | | | never needs to be '|'d to the others. The exporter| |
| 176 | | | will raise an error if it cannot provide such a | |
| 177 | | | contiguous buffer of bytes. | |
| 178 | | | | |
| 179 | +-------------------------------+---------------------------------------------------+ |
| 180 | | :c:macro:`PyBUF_WRITABLE` | The returned buffer must be writable. If it is | |
| 181 | | | not writable, then raise an error. | |
| 182 | +-------------------------------+---------------------------------------------------+ |
| 183 | | :c:macro:`PyBUF_STRIDES` | This implies :c:macro:`PyBUF_ND`. The returned | |
| 184 | | | buffer must provide strides information (i.e. the | |
| 185 | | | strides cannot be NULL). This would be used when | |
| 186 | | | the consumer can handle strided, discontiguous | |
| 187 | | | arrays. Handling strides automatically assumes | |
| 188 | | | you can handle shape. The exporter can raise an | |
| 189 | | | error if a strided representation of the data is | |
| 190 | | | not possible (i.e. without the suboffsets). | |
| 191 | | | | |
| 192 | +-------------------------------+---------------------------------------------------+ |
| 193 | | :c:macro:`PyBUF_ND` | The returned buffer must provide shape | |
| 194 | | | information. The memory will be assumed C-style | |
| 195 | | | contiguous (last dimension varies the | |
| 196 | | | fastest). The exporter may raise an error if it | |
| 197 | | | cannot provide this kind of contiguous buffer. If | |
| 198 | | | this is not given then shape will be *NULL*. | |
| 199 | | | | |
| 200 | | | | |
| 201 | | | | |
| 202 | +-------------------------------+---------------------------------------------------+ |
| 203 | |:c:macro:`PyBUF_C_CONTIGUOUS` | These flags indicate that the contiguity returned | |
| 204 | |:c:macro:`PyBUF_F_CONTIGUOUS` | buffer must be respectively, C-contiguous (last | |
| 205 | |:c:macro:`PyBUF_ANY_CONTIGUOUS`| dimension varies the fastest), Fortran contiguous | |
| 206 | | | (first dimension varies the fastest) or either | |
| 207 | | | one. All of these flags imply | |
| 208 | | | :c:macro:`PyBUF_STRIDES` and guarantee that the | |
| 209 | | | strides buffer info structure will be filled in | |
| 210 | | | correctly. | |
| 211 | | | | |
| 212 | +-------------------------------+---------------------------------------------------+ |
| 213 | | :c:macro:`PyBUF_INDIRECT` | This flag indicates the returned buffer must have | |
| 214 | | | suboffsets information (which can be NULL if no | |
| 215 | | | suboffsets are needed). This can be used when | |
| 216 | | | the consumer can handle indirect array | |
| 217 | | | referencing implied by these suboffsets. This | |
| 218 | | | implies :c:macro:`PyBUF_STRIDES`. | |
| 219 | | | | |
| 220 | | | | |
| 221 | | | | |
| 222 | +-------------------------------+---------------------------------------------------+ |
| 223 | | :c:macro:`PyBUF_FORMAT` | The returned buffer must have true format | |
| 224 | | | information if this flag is provided. This would | |
| 225 | | | be used when the consumer is going to be checking | |
| 226 | | | for what 'kind' of data is actually stored. An | |
| 227 | | | exporter should always be able to provide this | |
| 228 | | | information if requested. If format is not | |
| 229 | | | explicitly requested then the format must be | |
| 230 | | | returned as *NULL* (which means ``'B'``, or | |
| 231 | | | unsigned bytes) | |
| 232 | +-------------------------------+---------------------------------------------------+ |
| 233 | | :c:macro:`PyBUF_STRIDED` | This is equivalent to ``(PyBUF_STRIDES | | |
| 234 | | | PyBUF_WRITABLE)``. | |
| 235 | +-------------------------------+---------------------------------------------------+ |
| 236 | | :c:macro:`PyBUF_STRIDED_RO` | This is equivalent to ``(PyBUF_STRIDES)``. | |
| 237 | | | | |
| 238 | +-------------------------------+---------------------------------------------------+ |
| 239 | | :c:macro:`PyBUF_RECORDS` | This is equivalent to ``(PyBUF_STRIDES | | |
| 240 | | | PyBUF_FORMAT | PyBUF_WRITABLE)``. | |
| 241 | +-------------------------------+---------------------------------------------------+ |
| 242 | | :c:macro:`PyBUF_RECORDS_RO` | This is equivalent to ``(PyBUF_STRIDES | | |
| 243 | | | PyBUF_FORMAT)``. | |
| 244 | +-------------------------------+---------------------------------------------------+ |
| 245 | | :c:macro:`PyBUF_FULL` | This is equivalent to ``(PyBUF_INDIRECT | | |
| 246 | | | PyBUF_FORMAT | PyBUF_WRITABLE)``. | |
| 247 | +-------------------------------+---------------------------------------------------+ |
| 248 | | :c:macro:`PyBUF_FULL_RO` | This is equivalent to ``(PyBUF_INDIRECT | | |
| 249 | | | PyBUF_FORMAT)``. | |
| 250 | +-------------------------------+---------------------------------------------------+ |
| 251 | | :c:macro:`PyBUF_CONTIG` | This is equivalent to ``(PyBUF_ND | | |
| 252 | | | PyBUF_WRITABLE)``. | |
| 253 | +-------------------------------+---------------------------------------------------+ |
| 254 | | :c:macro:`PyBUF_CONTIG_RO` | This is equivalent to ``(PyBUF_ND)``. | |
| 255 | | | | |
| 256 | +-------------------------------+---------------------------------------------------+ |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 257 | |
| 258 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 259 | .. c:function:: void PyBuffer_Release(Py_buffer *view) |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 260 | |
Georg Brandl | eb4781c | 2009-09-02 20:37:16 +0000 | [diff] [blame] | 261 | Release the buffer *view*. This should be called when the buffer |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 262 | is no longer being used as it may free memory from it. |
| 263 | |
| 264 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 265 | .. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *) |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 266 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 267 | Return the implied :c:data:`~Py_buffer.itemsize` from the struct-stype |
| 268 | :c:data:`~Py_buffer.format`. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 269 | |
| 270 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 271 | .. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran) |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 272 | |
| 273 | Return 1 if the memory defined by the *view* is C-style (*fortran* is |
| 274 | ``'C'``) or Fortran-style (*fortran* is ``'F'``) contiguous or either one |
| 275 | (*fortran* is ``'A'``). Return 0 otherwise. |
| 276 | |
| 277 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 278 | .. c:function:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran) |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 279 | |
| 280 | Fill the *strides* array with byte-strides of a contiguous (C-style if |
Ezio Melotti | 52f63ea | 2011-05-20 15:04:38 +0300 | [diff] [blame] | 281 | *fortran* is ``'C'`` or Fortran-style if *fortran* is ``'F'``) array of the |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 282 | given shape with the given number of bytes per element. |
| 283 | |
| 284 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 285 | .. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags) |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 286 | |
| 287 | Fill in a buffer-info structure, *view*, correctly for an exporter that can |
| 288 | only share a contiguous chunk of memory of "unsigned bytes" of the given |
| 289 | length. Return 0 on success and -1 (with raising an error) on error. |
| 290 | |
| 291 | |
| 292 | MemoryView objects |
| 293 | ================== |
| 294 | |
Antoine Pitrou | 64fb940 | 2010-09-28 15:35:18 +0000 | [diff] [blame] | 295 | .. versionadded:: 2.7 |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | 64fb940 | 2010-09-28 15:35:18 +0000 | [diff] [blame] | 297 | A :class:`memoryview` object exposes the new C level buffer interface as a |
| 298 | Python object which can then be passed around like any other object. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 299 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 300 | .. c:function:: PyObject *PyMemoryView_FromObject(PyObject *obj) |
Antoine Pitrou | 64fb940 | 2010-09-28 15:35:18 +0000 | [diff] [blame] | 301 | |
| 302 | Create a memoryview object from an object that defines the new buffer |
| 303 | interface. |
| 304 | |
| 305 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 306 | .. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view) |
Antoine Pitrou | 64fb940 | 2010-09-28 15:35:18 +0000 | [diff] [blame] | 307 | |
| 308 | Create a memoryview object wrapping the given buffer-info structure *view*. |
| 309 | The memoryview object then owns the buffer, which means you shouldn't |
| 310 | try to release it yourself: it will be released on deallocation of the |
| 311 | memoryview object. |
| 312 | |
| 313 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 314 | .. c:function:: PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order) |
Antoine Pitrou | 64fb940 | 2010-09-28 15:35:18 +0000 | [diff] [blame] | 315 | |
| 316 | Create a memoryview object to a contiguous chunk of memory (in either |
| 317 | 'C' or 'F'ortran *order*) from an object that defines the buffer |
| 318 | interface. If memory is contiguous, the memoryview object points to the |
| 319 | original memory. Otherwise copy is made and the memoryview points to a |
| 320 | new bytes object. |
| 321 | |
| 322 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 323 | .. c:function:: int PyMemoryView_Check(PyObject *obj) |
Antoine Pitrou | 64fb940 | 2010-09-28 15:35:18 +0000 | [diff] [blame] | 324 | |
| 325 | Return true if the object *obj* is a memoryview object. It is not |
| 326 | currently allowed to create subclasses of :class:`memoryview`. |
| 327 | |
| 328 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 329 | .. c:function:: Py_buffer *PyMemoryView_GET_BUFFER(PyObject *obj) |
Antoine Pitrou | 64fb940 | 2010-09-28 15:35:18 +0000 | [diff] [blame] | 330 | |
| 331 | Return a pointer to the buffer-info structure wrapped by the given |
| 332 | object. The object **must** be a memoryview instance; this macro doesn't |
| 333 | check its type, you must do it yourself or you will risk crashes. |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 334 | |
| 335 | |
| 336 | Old-style buffer objects |
| 337 | ======================== |
| 338 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 339 | .. index:: single: PyBufferProcs |
| 340 | |
Antoine Pitrou | 789be0c | 2009-04-02 21:18:34 +0000 | [diff] [blame] | 341 | More information on the old buffer interface is provided in the section |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 342 | :ref:`buffer-structs`, under the description for :c:type:`PyBufferProcs`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 343 | |
| 344 | A "buffer object" is defined in the :file:`bufferobject.h` header (included by |
| 345 | :file:`Python.h`). These objects look very similar to string objects at the |
| 346 | Python programming level: they support slicing, indexing, concatenation, and |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 347 | some other standard string operations. However, their data can come from one |
| 348 | of two sources: from a block of memory, or from another object which exports |
| 349 | the buffer interface. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 350 | |
| 351 | Buffer objects are useful as a way to expose the data from another object's |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 352 | buffer interface to the Python programmer. They can also be used as a |
| 353 | zero-copy slicing mechanism. Using their ability to reference a block of |
| 354 | memory, it is possible to expose any data to the Python programmer quite |
| 355 | easily. The memory could be a large, constant array in a C extension, it could |
| 356 | be a raw block of memory for manipulation before passing to an operating |
| 357 | system library, or it could be used to pass around structured data in its |
| 358 | native, in-memory format. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 359 | |
| 360 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 361 | .. c:type:: PyBufferObject |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 362 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 363 | This subtype of :c:type:`PyObject` represents a buffer object. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 364 | |
| 365 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 366 | .. c:var:: PyTypeObject PyBuffer_Type |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 367 | |
| 368 | .. index:: single: BufferType (in module types) |
| 369 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 370 | The instance of :c:type:`PyTypeObject` which represents the Python buffer type; |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 371 | it is the same object as ``buffer`` and ``types.BufferType`` in the Python |
| 372 | layer. . |
| 373 | |
| 374 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 375 | .. c:var:: int Py_END_OF_BUFFER |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 376 | |
| 377 | This constant may be passed as the *size* parameter to |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 378 | :c:func:`PyBuffer_FromObject` or :c:func:`PyBuffer_FromReadWriteObject`. It |
| 379 | indicates that the new :c:type:`PyBufferObject` should refer to *base* |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 380 | object from the specified *offset* to the end of its exported buffer. |
| 381 | Using this enables the caller to avoid querying the *base* object for its |
| 382 | length. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 383 | |
| 384 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 385 | .. c:function:: int PyBuffer_Check(PyObject *p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 386 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 387 | Return true if the argument has type :c:data:`PyBuffer_Type`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 388 | |
| 389 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 390 | .. c:function:: PyObject* PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 391 | |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 392 | Return a new read-only buffer object. This raises :exc:`TypeError` if |
| 393 | *base* doesn't support the read-only buffer protocol or doesn't provide |
| 394 | exactly one buffer segment, or it raises :exc:`ValueError` if *offset* is |
| 395 | less than zero. The buffer will hold a reference to the *base* object, and |
| 396 | the buffer's contents will refer to the *base* object's buffer interface, |
| 397 | starting as position *offset* and extending for *size* bytes. If *size* is |
| 398 | :const:`Py_END_OF_BUFFER`, then the new buffer's contents extend to the |
| 399 | length of the *base* object's exported buffer data. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 400 | |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 401 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 402 | This function used an :c:type:`int` type for *offset* and *size*. This |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 403 | might require changes in your code for properly supporting 64-bit |
| 404 | systems. |
| 405 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 406 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 407 | .. c:function:: PyObject* PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 408 | |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 409 | Return a new writable buffer object. Parameters and exceptions are similar |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 410 | to those for :c:func:`PyBuffer_FromObject`. If the *base* object does not |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 411 | export the writeable buffer protocol, then :exc:`TypeError` is raised. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 412 | |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 413 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 414 | This function used an :c:type:`int` type for *offset* and *size*. This |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 415 | might require changes in your code for properly supporting 64-bit |
| 416 | systems. |
| 417 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 418 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 419 | .. c:function:: PyObject* PyBuffer_FromMemory(void *ptr, Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 420 | |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 421 | Return a new read-only buffer object that reads from a specified location |
| 422 | in memory, with a specified size. The caller is responsible for ensuring |
| 423 | that the memory buffer, passed in as *ptr*, is not deallocated while the |
| 424 | returned buffer object exists. Raises :exc:`ValueError` if *size* is less |
| 425 | than zero. Note that :const:`Py_END_OF_BUFFER` may *not* be passed for the |
| 426 | *size* parameter; :exc:`ValueError` will be raised in that case. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 427 | |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 428 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 429 | This function used an :c:type:`int` type for *size*. This might require |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 430 | changes in your code for properly supporting 64-bit systems. |
| 431 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 432 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 433 | .. c:function:: PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 434 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 435 | Similar to :c:func:`PyBuffer_FromMemory`, but the returned buffer is |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 436 | writable. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 437 | |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 438 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 439 | This function used an :c:type:`int` type for *size*. This might require |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 440 | changes in your code for properly supporting 64-bit systems. |
| 441 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 442 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 443 | .. c:function:: PyObject* PyBuffer_New(Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 444 | |
| 445 | Return a new writable buffer object that maintains its own memory buffer of |
Jeroen Ruigrok van der Werven | 1ae8c88 | 2009-04-25 19:04:15 +0000 | [diff] [blame] | 446 | *size* bytes. :exc:`ValueError` is returned if *size* is not zero or |
| 447 | positive. Note that the memory buffer (as returned by |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 448 | :c:func:`PyObject_AsWriteBuffer`) is not specifically aligned. |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 449 | |
| 450 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 451 | This function used an :c:type:`int` type for *size*. This might require |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 452 | changes in your code for properly supporting 64-bit systems. |