blob: d14fd80c2825b6aa191bd3f86cbc9a6bc85ac161 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _bufferobjects:
4
5Buffer Objects
6--------------
7
8.. sectionauthor:: Greg Stein <gstein@lyra.org>
Benjamin Peterson9d0ced32008-09-16 02:24:31 +00009.. sectionauthor:: Benjamin Peterson
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
11
12.. index::
Georg Brandl54a3faa2008-01-20 09:30:57 +000013 single: buffer interface
14
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000015Python objects implemented in C can export a "buffer interface." These
16functions can be used by an object to expose its data in a raw, byte-oriented
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000017format. Clients of the object can use the buffer interface to access the
18object data directly, without needing to copy it first.
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000020Two examples of objects that support the buffer interface are bytes and
21arrays. The bytes object exposes the character contents in the buffer
Georg Brandl54a3faa2008-01-20 09:30:57 +000022interface's byte-oriented form. An array can also expose its contents, but it
23should be noted that array elements may be multi-byte values.
24
25An example user of the buffer interface is the file object's :meth:`write`
26method. Any object that can export a series of bytes through the buffer
27interface can be written to a file. There are a number of format codes to
28:cfunc:`PyArg_ParseTuple` that operate against an object's buffer interface,
29returning data from the target object.
30
31.. index:: single: PyBufferProcs
32
Georg Brandl48310cd2009-01-03 21:18:54 +000033More information on the buffer interface is provided in the section
Georg Brandl54a3faa2008-01-20 09:30:57 +000034:ref:`buffer-structs`, under the description for :ctype:`PyBufferProcs`.
35
Georg Brandl54a3faa2008-01-20 09:30:57 +000036Buffer objects are useful as a way to expose the data from another object's
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000037buffer interface to the Python programmer. They can also be used as a zero-copy
38slicing mechanism. Using their ability to reference a block of memory, it is
39possible to expose any data to the Python programmer quite easily. The memory
Georg Brandl54a3faa2008-01-20 09:30:57 +000040could be a large, constant array in a C extension, it could be a raw block of
41memory for manipulation before passing to an operating system library, or it
42could be used to pass around structured data in its native, in-memory format.
43
44
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000045.. ctype:: Py_buffer
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000047 .. cmember:: void *buf
48
49 A pointer to the start of the memory for the object.
50
51 .. cmember:: Py_ssize_t len
Benjamin Petersonf2fa87b2008-09-17 22:59:21 +000052 :noindex:
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000053
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
Benjamin Petersonf2fa87b2008-09-17 22:59:21 +000061 :noindex:
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000062
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000063 A *NULL* terminated string in :mod:`struct` module style syntax giving
64 the contents of the elements available through the buffer. If this is
65 *NULL*, ``"B"`` (unsigned bytes) is assumed.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000066
67 .. cmember:: int ndim
68
69 The number of dimensions the memory represents as a multi-dimensional
70 array. If it is 0, :cdata:`strides` and :cdata:`suboffsets` must be
71 *NULL*.
72
73 .. cmember:: Py_ssize_t *shape
74
75 An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim` giving the
76 shape of the memory as a multi-dimensional array. Note that
77 ``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to
78 :cdata:`len`.
79
80 .. cmember:: Py_ssize_t *strides
81
82 An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim` giving the
83 number of bytes to skip to get to a new element in each dimension.
84
85 .. cmember:: Py_ssize_t *suboffsets
86
87 An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim`. If these
88 suboffset numbers are greater than or equal to 0, then the value stored
89 along the indicated dimension is a pointer and the suboffset value
90 dictates how many bytes to add to the pointer after de-referencing. A
91 suboffset value that it negative indicates that no de-referencing should
92 occur (striding in a contiguous memory block).
93
94 Here is a function that returns a pointer to the element in an N-D array
Georg Brandlae2dbe22009-03-13 19:04:40 +000095 pointed to by an N-dimensional index when there are both non-NULL strides
Benjamin Peterson9d0ced32008-09-16 02:24:31 +000096 and suboffsets::
97
98 void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
99 Py_ssize_t *suboffsets, Py_ssize_t *indices) {
100 char *pointer = (char*)buf;
101 int i;
102 for (i = 0; i < ndim; i++) {
103 pointer += strides[i] * indices[i];
104 if (suboffsets[i] >=0 ) {
105 pointer = *((char**)pointer) + suboffsets[i];
Georg Brandl48310cd2009-01-03 21:18:54 +0000106 }
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000107 }
108 return (void*)pointer;
109 }
Georg Brandl54a3faa2008-01-20 09:30:57 +0000110
111
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000112 .. cmember:: Py_ssize_t itemsize
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000114 This is a storage for the itemsize (in bytes) of each element of the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000115 shared memory. It is technically un-necessary as it can be obtained
116 using :cfunc:`PyBuffer_SizeFromFormat`, however an exporter may know
117 this information without parsing the format string and it is necessary
118 to know the itemsize for proper interpretation of striding. Therefore,
119 storing it is more convenient and faster.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000120
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000121 .. cmember:: void *internal
122
123 This is for use internally by the exporting object. For example, this
124 might be re-cast as an integer by the exporter and used to store flags
125 about whether or not the shape, strides, and suboffsets arrays must be
126 freed when the buffer is released. The consumer should never alter this
127 value.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000128
129
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000130Buffer related functions
131========================
Georg Brandl54a3faa2008-01-20 09:30:57 +0000132
133
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000134.. cfunction:: int PyObject_CheckBuffer(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000135
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000136 Return 1 if *obj* supports the buffer interface otherwise 0.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137
138
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000139.. cfunction:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000141 Export *obj* into a :ctype:`Py_buffer`, *view*. These arguments must
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000142 never be *NULL*. The *flags* argument is a bit field indicating what
143 kind of buffer the caller is prepared to deal with and therefore what
144 kind of buffer the exporter is allowed to return. The buffer interface
145 allows for complicated memory sharing possibilities, but some caller may
Georg Brandl1b5ab452009-08-13 07:56:35 +0000146 not be able to handle all the complexity but may want to see if the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000147 exporter will let them take a simpler view to its memory.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000148
149 Some exporters may not be able to share memory in every possible way and
150 may need to raise errors to signal to some consumers that something is
151 just not possible. These errors should be a :exc:`BufferError` unless
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000152 there is another error that is actually causing the problem. The
153 exporter can use flags information to simplify how much of the
154 :cdata:`Py_buffer` structure is filled in with non-default values and/or
155 raise an error if the object can't support a simpler view of its memory.
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000156
157 0 is returned on success and -1 on error.
158
159 The following table gives possible values to the *flags* arguments.
160
Benjamin Petersonc1f44af2008-11-20 22:38:20 +0000161 +------------------------------+---------------------------------------------------+
162 | Flag | Description |
163 +==============================+===================================================+
164 | :cmacro:`PyBUF_SIMPLE` | This is the default flag state. The returned |
165 | | buffer may or may not have writable memory. The |
166 | | format of the data will be assumed to be unsigned |
167 | | bytes. This is a "stand-alone" flag constant. It |
168 | | never needs to be '|'d to the others. The exporter|
169 | | will raise an error if it cannot provide such a |
170 | | contiguous buffer of bytes. |
171 | | |
172 +------------------------------+---------------------------------------------------+
173 | :cmacro:`PyBUF_WRITABLE` | The returned buffer must be writable. If it is |
174 | | not writable, then raise an error. |
175 +------------------------------+---------------------------------------------------+
176 | :cmacro:`PyBUF_STRIDES` | This implies :cmacro:`PyBUF_ND`. The returned |
177 | | buffer must provide strides information (i.e. the |
178 | | strides cannot be NULL). This would be used when |
179 | | the consumer can handle strided, discontiguous |
180 | | arrays. Handling strides automatically assumes |
181 | | you can handle shape. The exporter can raise an |
182 | | error if a strided representation of the data is |
183 | | not possible (i.e. without the suboffsets). |
184 | | |
185 +------------------------------+---------------------------------------------------+
186 | :cmacro:`PyBUF_ND` | The returned buffer must provide shape |
187 | | information. The memory will be assumed C-style |
188 | | contiguous (last dimension varies the |
189 | | fastest). The exporter may raise an error if it |
190 | | cannot provide this kind of contiguous buffer. If |
191 | | this is not given then shape will be *NULL*. |
192 | | |
193 | | |
194 | | |
195 +------------------------------+---------------------------------------------------+
196 |:cmacro:`PyBUF_C_CONTIGUOUS` | These flags indicate that the contiguity returned |
197 |:cmacro:`PyBUF_F_CONTIGUOUS` | buffer must be respectively, C-contiguous (last |
198 |:cmacro:`PyBUF_ANY_CONTIGUOUS`| dimension varies the fastest), Fortran contiguous |
199 | | (first dimension varies the fastest) or either |
200 | | one. All of these flags imply |
201 | | :cmacro:`PyBUF_STRIDES` and guarantee that the |
202 | | strides buffer info structure will be filled in |
203 | | correctly. |
204 | | |
205 +------------------------------+---------------------------------------------------+
206 | :cmacro:`PyBUF_INDIRECT` | This flag indicates the returned buffer must have |
207 | | suboffsets information (which can be NULL if no |
208 | | suboffsets are needed). This can be used when |
209 | | the consumer can handle indirect array |
210 | | referencing implied by these suboffsets. This |
211 | | implies :cmacro:`PyBUF_STRIDES`. |
212 | | |
213 | | |
214 | | |
215 +------------------------------+---------------------------------------------------+
216 | :cmacro:`PyBUF_FORMAT` | The returned buffer must have true format |
217 | | information if this flag is provided. This would |
218 | | be used when the consumer is going to be checking |
219 | | for what 'kind' of data is actually stored. An |
220 | | exporter should always be able to provide this |
221 | | information if requested. If format is not |
222 | | explicitly requested then the format must be |
223 | | returned as *NULL* (which means ``'B'``, or |
224 | | unsigned bytes) |
225 +------------------------------+---------------------------------------------------+
226 | :cmacro:`PyBUF_STRIDED` | This is equivalent to ``(PyBUF_STRIDES | |
227 | | PyBUF_WRITABLE)``. |
228 +------------------------------+---------------------------------------------------+
229 | :cmacro:`PyBUF_STRIDED_RO` | This is equivalent to ``(PyBUF_STRIDES)``. |
230 | | |
231 +------------------------------+---------------------------------------------------+
232 | :cmacro:`PyBUF_RECORDS` | This is equivalent to ``(PyBUF_STRIDES | |
233 | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
234 +------------------------------+---------------------------------------------------+
235 | :cmacro:`PyBUF_RECORDS_RO` | This is equivalent to ``(PyBUF_STRIDES | |
236 | | PyBUF_FORMAT)``. |
237 +------------------------------+---------------------------------------------------+
238 | :cmacro:`PyBUF_FULL` | This is equivalent to ``(PyBUF_INDIRECT | |
239 | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
240 +------------------------------+---------------------------------------------------+
Benjamin Petersonef3e4c22009-04-11 19:48:14 +0000241 | :cmacro:`PyBUF_FULL_RO` | This is equivalent to ``(PyBUF_INDIRECT | |
Benjamin Petersonc1f44af2008-11-20 22:38:20 +0000242 | | PyBUF_FORMAT)``. |
243 +------------------------------+---------------------------------------------------+
244 | :cmacro:`PyBUF_CONTIG` | This is equivalent to ``(PyBUF_ND | |
245 | | PyBUF_WRITABLE)``. |
246 +------------------------------+---------------------------------------------------+
247 | :cmacro:`PyBUF_CONTIG_RO` | This is equivalent to ``(PyBUF_ND)``. |
248 | | |
249 +------------------------------+---------------------------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000250
251
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000252.. cfunction:: void PyBuffer_Release(PyObject *obj, Py_buffer *view)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000253
Hirokazu Yamamoto4d4975c2009-03-02 05:49:44 +0000254 Release the buffer *view* over *obj*. This should be called when the buffer
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000255 is no longer being used as it may free memory from it.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000256
257
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000258.. cfunction:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000259
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000260 Return the implied :cdata:`~Py_buffer.itemsize` from the struct-stype
261 :cdata:`~Py_buffer.format`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000262
263
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000264.. cfunction:: int PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fortran)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000265
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000266 Copy *len* bytes of data pointed to by the contiguous chunk of memory
267 pointed to by *buf* into the buffer exported by obj. The buffer must of
268 course be writable. Return 0 on success and return -1 and raise an error
269 on failure. If the object does not have a writable buffer, then an error
270 is raised. If *fortran* is ``'F'``, then if the object is
271 multi-dimensional, then the data will be copied into the array in
272 Fortran-style (first dimension varies the fastest). If *fortran* is
273 ``'C'``, then the data will be copied into the array in C-style (last
274 dimension varies the fastest). If *fortran* is ``'A'``, then it does not
275 matter and the copy will be made in whatever way is more efficient.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000276
277
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000278.. cfunction:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000279
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000280 Return 1 if the memory defined by the *view* is C-style (*fortran* is
281 ``'C'``) or Fortran-style (*fortran* is ``'F'``) contiguous or either one
282 (*fortran* is ``'A'``). Return 0 otherwise.
283
284
285.. cfunction:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran)
286
287 Fill the *strides* array with byte-strides of a contiguous (C-style if
288 *fortran* is ``'C'`` or Fortran-style if *fortran* is ``'F'`` array of the
289 given shape with the given number of bytes per element.
290
291
Georg Brandl8668c222009-12-28 08:00:47 +0000292.. cfunction:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000293
294 Fill in a buffer-info structure, *view*, correctly for an exporter that can
295 only share a contiguous chunk of memory of "unsigned bytes" of the given
296 length. Return 0 on success and -1 (with raising an error) on error.
297
298
Benjamin Peterson0f46ffd2009-08-26 07:35:45 +0000299.. index::
300 object: memoryview
301
302
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000303MemoryView objects
304==================
305
Benjamin Peterson0f46ffd2009-08-26 07:35:45 +0000306A memoryview object exposes the C level buffer interface to Python.
307
Benjamin Peterson9d0ced32008-09-16 02:24:31 +0000308
309.. cfunction:: PyObject* PyMemoryView_FromObject(PyObject *obj)
310
311 Return a memoryview object from an object that defines the buffer interface.
Benjamin Peterson0f46ffd2009-08-26 07:35:45 +0000312
313
314.. cfunction:: PyObject * PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
315
316 Return 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.