blob: e36d01f3da2c401a20d61145bf5c0a7d281fed5b [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _bufferobjects:
4
Antoine Pitrou789be0c2009-04-02 21:18:34 +00005Buffers and Memoryview Objects
6------------------------------
Georg Brandlf6842722008-01-19 22:08:21 +00007
8.. sectionauthor:: Greg Stein <gstein@lyra.org>
Antoine Pitrou789be0c2009-04-02 21:18:34 +00009.. sectionauthor:: Benjamin Peterson
Georg Brandlf6842722008-01-19 22:08:21 +000010
11
12.. index::
13 object: buffer
14 single: buffer interface
15
16Python objects implemented in C can export a group of functions called the
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +000017"buffer interface." These functions can be used by an object to expose its
18data in a raw, byte-oriented format. Clients of the object can use the buffer
19interface to access the object data directly, without needing to copy it
20first.
Georg Brandlf6842722008-01-19 22:08:21 +000021
22Two examples of objects that support the buffer interface are strings and
23arrays. The string object exposes the character contents in the buffer
Stefan Krah32ae4372014-04-26 22:50:25 +020024interface's byte-oriented form. An array can only expose its contents via the
25old-style buffer interface. This limitation does not apply to Python 3,
26where :class:`memoryview` objects can be constructed from arrays, too.
27Array elements may be multi-byte values.
Georg Brandlf6842722008-01-19 22:08:21 +000028
29An example user of the buffer interface is the file object's :meth:`write`
30method. Any object that can export a series of bytes through the buffer
31interface can be written to a file. There are a number of format codes to
Sandro Tosi98ed08f2012-01-14 16:42:02 +010032:c:func:`PyArg_ParseTuple` that operate against an object's buffer interface,
Georg Brandlf6842722008-01-19 22:08:21 +000033returning data from the target object.
34
Antoine Pitrou789be0c2009-04-02 21:18:34 +000035Starting from version 1.6, Python has been providing Python-level buffer
Georg Brandld7d4fd72009-07-26 14:37:28 +000036objects and a C-level buffer API so that any built-in or used-defined type can
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +000037expose its characteristics. Both, however, have been deprecated because of
Ezio Melotti510ff542012-05-03 19:21:40 +030038various shortcomings, and have been officially removed in Python 3 in favour
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +000039of a new C-level buffer API and a new Python-level object named
Antoine Pitrou789be0c2009-04-02 21:18:34 +000040:class:`memoryview`.
41
42The 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
44advised to use them rather than the old APIs, unless you are blocked from
45doing so for compatibility reasons.
46
47
48The new-style Py_buffer struct
49==============================
50
51
Sandro Tosi98ed08f2012-01-14 16:42:02 +010052.. c:type:: Py_buffer
Antoine Pitrou789be0c2009-04-02 21:18:34 +000053
Sandro Tosi98ed08f2012-01-14 16:42:02 +010054 .. c:member:: void *buf
Antoine Pitrou789be0c2009-04-02 21:18:34 +000055
56 A pointer to the start of the memory for the object.
57
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058 .. c:member:: Py_ssize_t len
Antoine Pitrou789be0c2009-04-02 21:18:34 +000059 :noindex:
60
61 The total length of the memory in bytes.
62
Sandro Tosi98ed08f2012-01-14 16:42:02 +010063 .. c:member:: int readonly
Antoine Pitrou789be0c2009-04-02 21:18:34 +000064
65 An indicator of whether the buffer is read only.
66
Sandro Tosi98ed08f2012-01-14 16:42:02 +010067 .. c:member:: const char *format
Antoine Pitrou789be0c2009-04-02 21:18:34 +000068 :noindex:
69
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +000070 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 Pitrou789be0c2009-04-02 21:18:34 +000073
Sandro Tosi98ed08f2012-01-14 16:42:02 +010074 .. c:member:: int ndim
Antoine Pitrou789be0c2009-04-02 21:18:34 +000075
76 The number of dimensions the memory represents as a multi-dimensional
Sandro Tosi98ed08f2012-01-14 16:42:02 +010077 array. If it is 0, :c:data:`strides` and :c:data:`suboffsets` must be
Antoine Pitrou789be0c2009-04-02 21:18:34 +000078 *NULL*.
79
Sandro Tosi98ed08f2012-01-14 16:42:02 +010080 .. c:member:: Py_ssize_t *shape
Antoine Pitrou789be0c2009-04-02 21:18:34 +000081
Sandro Tosi98ed08f2012-01-14 16:42:02 +010082 An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
Antoine Pitrou789be0c2009-04-02 21:18:34 +000083 shape of the memory as a multi-dimensional array. Note that
84 ``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to
Sandro Tosi98ed08f2012-01-14 16:42:02 +010085 :c:data:`len`.
Antoine Pitrou789be0c2009-04-02 21:18:34 +000086
Sandro Tosi98ed08f2012-01-14 16:42:02 +010087 .. c:member:: Py_ssize_t *strides
Antoine Pitrou789be0c2009-04-02 21:18:34 +000088
Sandro Tosi98ed08f2012-01-14 16:42:02 +010089 An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
Antoine Pitrou789be0c2009-04-02 21:18:34 +000090 number of bytes to skip to get to a new element in each dimension.
91
Sandro Tosi98ed08f2012-01-14 16:42:02 +010092 .. c:member:: Py_ssize_t *suboffsets
Antoine Pitrou789be0c2009-04-02 21:18:34 +000093
Sandro Tosi98ed08f2012-01-14 16:42:02 +010094 An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim`. If these
Antoine Pitrou789be0c2009-04-02 21:18:34 +000095 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
Stefan Krah3f1ef5b2015-02-01 19:40:50 +0100101 If all suboffsets are negative (i.e. no de-referencing is needed, then
Stefan Krah1c9bf632015-02-01 19:49:38 +0100102 this field must be NULL (the default value).
Stefan Krah3f1ef5b2015-02-01 19:40:50 +0100103
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000104 Here is a function that returns a pointer to the element in an N-D array
Stefan Krah3f1ef5b2015-02-01 19:40:50 +0100105 pointed to by an N-dimensional index when there are both non-NULL strides
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000106 and suboffsets::
107
108 void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
109 Py_ssize_t *suboffsets, Py_ssize_t *indices) {
110 char *pointer = (char*)buf;
111 int i;
112 for (i = 0; i < ndim; i++) {
113 pointer += strides[i] * indices[i];
114 if (suboffsets[i] >=0 ) {
115 pointer = *((char**)pointer) + suboffsets[i];
116 }
117 }
118 return (void*)pointer;
119 }
120
121
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100122 .. c:member:: Py_ssize_t itemsize
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000123
124 This is a storage for the itemsize (in bytes) of each element of the
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000125 shared memory. It is technically un-necessary as it can be obtained
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100126 using :c:func:`PyBuffer_SizeFromFormat`, however an exporter may know
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000127 this information without parsing the format string and it is necessary
128 to know the itemsize for proper interpretation of striding. Therefore,
129 storing it is more convenient and faster.
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000130
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100131 .. c:member:: void *internal
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000132
133 This is for use internally by the exporting object. For example, this
134 might be re-cast as an integer by the exporter and used to store flags
135 about whether or not the shape, strides, and suboffsets arrays must be
136 freed when the buffer is released. The consumer should never alter this
137 value.
138
139
140Buffer related functions
141========================
142
143
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100144.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000145
146 Return 1 if *obj* supports the buffer interface otherwise 0.
147
148
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100149.. c:function:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000150
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100151 Export *obj* into a :c:type:`Py_buffer`, *view*. These arguments must
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000152 never be *NULL*. The *flags* argument is a bit field indicating what
153 kind of buffer the caller is prepared to deal with and therefore what
154 kind of buffer the exporter is allowed to return. The buffer interface
155 allows for complicated memory sharing possibilities, but some caller may
Georg Brandl4a46e1c2009-08-06 17:43:55 +0000156 not be able to handle all the complexity but may want to see if the
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000157 exporter will let them take a simpler view to its memory.
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000158
159 Some exporters may not be able to share memory in every possible way and
160 may need to raise errors to signal to some consumers that something is
161 just not possible. These errors should be a :exc:`BufferError` unless
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000162 there is another error that is actually causing the problem. The
163 exporter can use flags information to simplify how much of the
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100164 :c:data:`Py_buffer` structure is filled in with non-default values and/or
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000165 raise an error if the object can't support a simpler view of its memory.
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000166
167 0 is returned on success and -1 on error.
168
169 The following table gives possible values to the *flags* arguments.
170
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100171 +-------------------------------+---------------------------------------------------+
172 | Flag | Description |
173 +===============================+===================================================+
174 | :c:macro:`PyBUF_SIMPLE` | This is the default flag state. The returned |
175 | | buffer may or may not have writable memory. The |
176 | | format of the data will be assumed to be unsigned |
177 | | bytes. This is a "stand-alone" flag constant. It |
178 | | never needs to be '|'d to the others. The exporter|
179 | | will raise an error if it cannot provide such a |
180 | | contiguous buffer of bytes. |
181 | | |
182 +-------------------------------+---------------------------------------------------+
183 | :c:macro:`PyBUF_WRITABLE` | The returned buffer must be writable. If it is |
184 | | not writable, then raise an error. |
185 +-------------------------------+---------------------------------------------------+
186 | :c:macro:`PyBUF_STRIDES` | This implies :c:macro:`PyBUF_ND`. The returned |
187 | | buffer must provide strides information (i.e. the |
188 | | strides cannot be NULL). This would be used when |
189 | | the consumer can handle strided, discontiguous |
190 | | arrays. Handling strides automatically assumes |
191 | | you can handle shape. The exporter can raise an |
192 | | error if a strided representation of the data is |
193 | | not possible (i.e. without the suboffsets). |
194 | | |
195 +-------------------------------+---------------------------------------------------+
196 | :c:macro:`PyBUF_ND` | The returned buffer must provide shape |
197 | | information. The memory will be assumed C-style |
198 | | contiguous (last dimension varies the |
199 | | fastest). The exporter may raise an error if it |
200 | | cannot provide this kind of contiguous buffer. If |
201 | | this is not given then shape will be *NULL*. |
202 | | |
203 | | |
204 | | |
205 +-------------------------------+---------------------------------------------------+
206 |:c:macro:`PyBUF_C_CONTIGUOUS` | These flags indicate that the contiguity returned |
207 |:c:macro:`PyBUF_F_CONTIGUOUS` | buffer must be respectively, C-contiguous (last |
208 |:c:macro:`PyBUF_ANY_CONTIGUOUS`| dimension varies the fastest), Fortran contiguous |
209 | | (first dimension varies the fastest) or either |
210 | | one. All of these flags imply |
211 | | :c:macro:`PyBUF_STRIDES` and guarantee that the |
212 | | strides buffer info structure will be filled in |
213 | | correctly. |
214 | | |
215 +-------------------------------+---------------------------------------------------+
216 | :c:macro:`PyBUF_INDIRECT` | This flag indicates the returned buffer must have |
217 | | suboffsets information (which can be NULL if no |
218 | | suboffsets are needed). This can be used when |
219 | | the consumer can handle indirect array |
220 | | referencing implied by these suboffsets. This |
221 | | implies :c:macro:`PyBUF_STRIDES`. |
222 | | |
223 | | |
224 | | |
225 +-------------------------------+---------------------------------------------------+
226 | :c:macro:`PyBUF_FORMAT` | The returned buffer must have true format |
227 | | information if this flag is provided. This would |
228 | | be used when the consumer is going to be checking |
229 | | for what 'kind' of data is actually stored. An |
230 | | exporter should always be able to provide this |
231 | | information if requested. If format is not |
232 | | explicitly requested then the format must be |
233 | | returned as *NULL* (which means ``'B'``, or |
234 | | unsigned bytes) |
235 +-------------------------------+---------------------------------------------------+
236 | :c:macro:`PyBUF_STRIDED` | This is equivalent to ``(PyBUF_STRIDES | |
237 | | PyBUF_WRITABLE)``. |
238 +-------------------------------+---------------------------------------------------+
239 | :c:macro:`PyBUF_STRIDED_RO` | This is equivalent to ``(PyBUF_STRIDES)``. |
240 | | |
241 +-------------------------------+---------------------------------------------------+
242 | :c:macro:`PyBUF_RECORDS` | This is equivalent to ``(PyBUF_STRIDES | |
243 | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
244 +-------------------------------+---------------------------------------------------+
245 | :c:macro:`PyBUF_RECORDS_RO` | This is equivalent to ``(PyBUF_STRIDES | |
246 | | PyBUF_FORMAT)``. |
247 +-------------------------------+---------------------------------------------------+
248 | :c:macro:`PyBUF_FULL` | This is equivalent to ``(PyBUF_INDIRECT | |
249 | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
250 +-------------------------------+---------------------------------------------------+
251 | :c:macro:`PyBUF_FULL_RO` | This is equivalent to ``(PyBUF_INDIRECT | |
252 | | PyBUF_FORMAT)``. |
253 +-------------------------------+---------------------------------------------------+
254 | :c:macro:`PyBUF_CONTIG` | This is equivalent to ``(PyBUF_ND | |
255 | | PyBUF_WRITABLE)``. |
256 +-------------------------------+---------------------------------------------------+
257 | :c:macro:`PyBUF_CONTIG_RO` | This is equivalent to ``(PyBUF_ND)``. |
258 | | |
259 +-------------------------------+---------------------------------------------------+
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000260
261
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100262.. c:function:: void PyBuffer_Release(Py_buffer *view)
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000263
Georg Brandleb4781c2009-09-02 20:37:16 +0000264 Release the buffer *view*. This should be called when the buffer
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000265 is no longer being used as it may free memory from it.
266
267
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100268.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000269
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100270 Return the implied :c:data:`~Py_buffer.itemsize` from the struct-stype
271 :c:data:`~Py_buffer.format`.
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000272
273
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100274.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000275
276 Return 1 if the memory defined by the *view* is C-style (*fortran* is
277 ``'C'``) or Fortran-style (*fortran* is ``'F'``) contiguous or either one
278 (*fortran* is ``'A'``). Return 0 otherwise.
279
280
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100281.. c:function:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran)
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000282
283 Fill the *strides* array with byte-strides of a contiguous (C-style if
Ezio Melotti52f63ea2011-05-20 15:04:38 +0300284 *fortran* is ``'C'`` or Fortran-style if *fortran* is ``'F'``) array of the
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000285 given shape with the given number of bytes per element.
286
287
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100288.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000289
290 Fill in a buffer-info structure, *view*, correctly for an exporter that can
291 only share a contiguous chunk of memory of "unsigned bytes" of the given
292 length. Return 0 on success and -1 (with raising an error) on error.
293
294
295MemoryView objects
296==================
297
Antoine Pitrou64fb9402010-09-28 15:35:18 +0000298.. versionadded:: 2.7
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000299
Antoine Pitrou64fb9402010-09-28 15:35:18 +0000300A :class:`memoryview` object exposes the new C level buffer interface as a
301Python object which can then be passed around like any other object.
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000302
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100303.. c:function:: PyObject *PyMemoryView_FromObject(PyObject *obj)
Antoine Pitrou64fb9402010-09-28 15:35:18 +0000304
305 Create a memoryview object from an object that defines the new buffer
306 interface.
307
308
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100309.. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
Antoine Pitrou64fb9402010-09-28 15:35:18 +0000310
311 Create a memoryview object wrapping the given buffer-info structure *view*.
312 The memoryview object then owns the buffer, which means you shouldn't
313 try to release it yourself: it will be released on deallocation of the
314 memoryview object.
315
316
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100317.. c:function:: PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
Antoine Pitrou64fb9402010-09-28 15:35:18 +0000318
319 Create a memoryview object to a contiguous chunk of memory (in either
320 'C' or 'F'ortran *order*) from an object that defines the buffer
321 interface. If memory is contiguous, the memoryview object points to the
322 original memory. Otherwise copy is made and the memoryview points to a
323 new bytes object.
324
325
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100326.. c:function:: int PyMemoryView_Check(PyObject *obj)
Antoine Pitrou64fb9402010-09-28 15:35:18 +0000327
328 Return true if the object *obj* is a memoryview object. It is not
329 currently allowed to create subclasses of :class:`memoryview`.
330
331
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100332.. c:function:: Py_buffer *PyMemoryView_GET_BUFFER(PyObject *obj)
Antoine Pitrou64fb9402010-09-28 15:35:18 +0000333
334 Return a pointer to the buffer-info structure wrapped by the given
335 object. The object **must** be a memoryview instance; this macro doesn't
336 check its type, you must do it yourself or you will risk crashes.
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000337
338
339Old-style buffer objects
340========================
341
Georg Brandlf6842722008-01-19 22:08:21 +0000342.. index:: single: PyBufferProcs
343
Antoine Pitrou789be0c2009-04-02 21:18:34 +0000344More information on the old buffer interface is provided in the section
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100345:ref:`buffer-structs`, under the description for :c:type:`PyBufferProcs`.
Georg Brandlf6842722008-01-19 22:08:21 +0000346
347A "buffer object" is defined in the :file:`bufferobject.h` header (included by
348:file:`Python.h`). These objects look very similar to string objects at the
349Python programming level: they support slicing, indexing, concatenation, and
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000350some other standard string operations. However, their data can come from one
351of two sources: from a block of memory, or from another object which exports
352the buffer interface.
Georg Brandlf6842722008-01-19 22:08:21 +0000353
354Buffer objects are useful as a way to expose the data from another object's
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000355buffer interface to the Python programmer. They can also be used as a
356zero-copy slicing mechanism. Using their ability to reference a block of
357memory, it is possible to expose any data to the Python programmer quite
358easily. The memory could be a large, constant array in a C extension, it could
359be a raw block of memory for manipulation before passing to an operating
360system library, or it could be used to pass around structured data in its
361native, in-memory format.
Georg Brandlf6842722008-01-19 22:08:21 +0000362
363
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100364.. c:type:: PyBufferObject
Georg Brandlf6842722008-01-19 22:08:21 +0000365
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100366 This subtype of :c:type:`PyObject` represents a buffer object.
Georg Brandlf6842722008-01-19 22:08:21 +0000367
368
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100369.. c:var:: PyTypeObject PyBuffer_Type
Georg Brandlf6842722008-01-19 22:08:21 +0000370
371 .. index:: single: BufferType (in module types)
372
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100373 The instance of :c:type:`PyTypeObject` which represents the Python buffer type;
Georg Brandlf6842722008-01-19 22:08:21 +0000374 it is the same object as ``buffer`` and ``types.BufferType`` in the Python
375 layer. .
376
377
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100378.. c:var:: int Py_END_OF_BUFFER
Georg Brandlf6842722008-01-19 22:08:21 +0000379
380 This constant may be passed as the *size* parameter to
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100381 :c:func:`PyBuffer_FromObject` or :c:func:`PyBuffer_FromReadWriteObject`. It
382 indicates that the new :c:type:`PyBufferObject` should refer to *base*
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000383 object from the specified *offset* to the end of its exported buffer.
384 Using this enables the caller to avoid querying the *base* object for its
385 length.
Georg Brandlf6842722008-01-19 22:08:21 +0000386
387
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100388.. c:function:: int PyBuffer_Check(PyObject *p)
Georg Brandlf6842722008-01-19 22:08:21 +0000389
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100390 Return true if the argument has type :c:data:`PyBuffer_Type`.
Georg Brandlf6842722008-01-19 22:08:21 +0000391
392
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100393.. c:function:: PyObject* PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
Georg Brandlf6842722008-01-19 22:08:21 +0000394
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000395 Return a new read-only buffer object. This raises :exc:`TypeError` if
396 *base* doesn't support the read-only buffer protocol or doesn't provide
397 exactly one buffer segment, or it raises :exc:`ValueError` if *offset* is
398 less than zero. The buffer will hold a reference to the *base* object, and
399 the buffer's contents will refer to the *base* object's buffer interface,
400 starting as position *offset* and extending for *size* bytes. If *size* is
401 :const:`Py_END_OF_BUFFER`, then the new buffer's contents extend to the
402 length of the *base* object's exported buffer data.
Georg Brandlf6842722008-01-19 22:08:21 +0000403
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000404 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100405 This function used an :c:type:`int` type for *offset* and *size*. This
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000406 might require changes in your code for properly supporting 64-bit
407 systems.
408
Georg Brandlf6842722008-01-19 22:08:21 +0000409
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100410.. c:function:: PyObject* PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
Georg Brandlf6842722008-01-19 22:08:21 +0000411
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000412 Return a new writable buffer object. Parameters and exceptions are similar
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100413 to those for :c:func:`PyBuffer_FromObject`. If the *base* object does not
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000414 export the writeable buffer protocol, then :exc:`TypeError` is raised.
Georg Brandlf6842722008-01-19 22:08:21 +0000415
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000416 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100417 This function used an :c:type:`int` type for *offset* and *size*. This
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000418 might require changes in your code for properly supporting 64-bit
419 systems.
420
Georg Brandlf6842722008-01-19 22:08:21 +0000421
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100422.. c:function:: PyObject* PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
Georg Brandlf6842722008-01-19 22:08:21 +0000423
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000424 Return a new read-only buffer object that reads from a specified location
425 in memory, with a specified size. The caller is responsible for ensuring
426 that the memory buffer, passed in as *ptr*, is not deallocated while the
427 returned buffer object exists. Raises :exc:`ValueError` if *size* is less
428 than zero. Note that :const:`Py_END_OF_BUFFER` may *not* be passed for the
429 *size* parameter; :exc:`ValueError` will be raised in that case.
Georg Brandlf6842722008-01-19 22:08:21 +0000430
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000431 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100432 This function used an :c:type:`int` type for *size*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000433 changes in your code for properly supporting 64-bit systems.
434
Georg Brandlf6842722008-01-19 22:08:21 +0000435
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100436.. c:function:: PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
Georg Brandlf6842722008-01-19 22:08:21 +0000437
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100438 Similar to :c:func:`PyBuffer_FromMemory`, but the returned buffer is
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000439 writable.
Georg Brandlf6842722008-01-19 22:08:21 +0000440
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000441 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100442 This function used an :c:type:`int` type for *size*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000443 changes in your code for properly supporting 64-bit systems.
444
Georg Brandlf6842722008-01-19 22:08:21 +0000445
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100446.. c:function:: PyObject* PyBuffer_New(Py_ssize_t size)
Georg Brandlf6842722008-01-19 22:08:21 +0000447
448 Return a new writable buffer object that maintains its own memory buffer of
Jeroen Ruigrok van der Werven1ae8c882009-04-25 19:04:15 +0000449 *size* bytes. :exc:`ValueError` is returned if *size* is not zero or
450 positive. Note that the memory buffer (as returned by
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100451 :c:func:`PyObject_AsWriteBuffer`) is not specifically aligned.
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000452
453 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100454 This function used an :c:type:`int` type for *size*. This might require
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +0000455 changes in your code for properly supporting 64-bit systems.