2008-01-20 05:30:57 -04:00
|
|
|
.. highlightlang:: c
|
|
|
|
|
|
|
|
.. _bufferobjects:
|
|
|
|
|
2010-09-28 20:39:41 -03:00
|
|
|
Buffer Protocol
|
|
|
|
---------------
|
2008-01-20 05:30:57 -04:00
|
|
|
|
|
|
|
.. sectionauthor:: Greg Stein <gstein@lyra.org>
|
2008-09-15 23:24:31 -03:00
|
|
|
.. sectionauthor:: Benjamin Peterson
|
2008-01-20 05:30:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: buffer interface
|
|
|
|
|
2010-12-12 15:59:47 -04:00
|
|
|
Certain objects available in Python wrap access to an underlying memory
|
|
|
|
array or *buffer*. Such objects include the built-in :class:`bytes` and
|
|
|
|
:class:`bytearray`, and some extension types like :class:`array.array`.
|
|
|
|
Third-party libraries may define their own types for special purposes, such
|
|
|
|
as image processing or numeric analysis.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-12-12 15:59:47 -04:00
|
|
|
While each of these types have their own semantics, they share the common
|
|
|
|
characteristic of being backed by a possibly large memory buffer. It is
|
|
|
|
then desireable, in some situations, to access that buffer directly and
|
|
|
|
without intermediate copying.
|
|
|
|
|
|
|
|
Python provides such a facility at the C level in the form of the *buffer
|
|
|
|
protocol*. This protocol has two sides:
|
|
|
|
|
|
|
|
.. index:: single: PyBufferProcs
|
|
|
|
|
|
|
|
- on the producer side, a type can export a "buffer interface" which allows
|
|
|
|
objects of that type to expose information about their underlying buffer.
|
|
|
|
This interface is described in the section :ref:`buffer-structs`;
|
|
|
|
|
|
|
|
- on the consumer side, several means are available to obtain a pointer to
|
|
|
|
the raw underlying data of an object (for example a method parameter).
|
|
|
|
|
|
|
|
Simple objects such as :class:`bytes` and :class:`bytearray` expose their
|
|
|
|
underlying buffer in byte-oriented form. Other forms are possible; for example,
|
|
|
|
the elements exposed by a :class:`array.array` can be multi-byte values.
|
2010-09-28 20:04:04 -03:00
|
|
|
|
|
|
|
An example consumer of the buffer interface is the :meth:`~io.BufferedIOBase.write`
|
|
|
|
method of file objects: any object that can export a series of bytes through
|
|
|
|
the buffer interface can be written to a file. While :meth:`write` only
|
|
|
|
needs read-only access to the internal contents of the object passed to it,
|
|
|
|
other methods such as :meth:`~io.BufferedIOBase.readinto` need write access
|
|
|
|
to the contents of their argument. The buffer interface allows objects to
|
|
|
|
selectively allow or reject exporting of read-write and read-only buffers.
|
|
|
|
|
|
|
|
There are two ways for a consumer of the buffer interface to acquire a buffer
|
|
|
|
over a target object:
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
* call :c:func:`PyObject_GetBuffer` with the right parameters;
|
2010-09-28 20:04:04 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
* call :c:func:`PyArg_ParseTuple` (or one of its siblings) with one of the
|
2010-09-28 20:04:04 -03:00
|
|
|
``y*``, ``w*`` or ``s*`` :ref:`format codes <arg-parsing>`.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
In both cases, :c:func:`PyBuffer_Release` must be called when the buffer
|
2010-09-28 20:04:04 -03:00
|
|
|
isn't needed anymore. Failure to do so could lead to various issues such as
|
|
|
|
resource leaks.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
|
|
|
|
2010-09-28 20:39:41 -03:00
|
|
|
The buffer structure
|
|
|
|
====================
|
2010-09-28 20:04:04 -03:00
|
|
|
|
2010-09-28 20:39:41 -03:00
|
|
|
Buffer structures (or simply "buffers") are useful as a way to expose the
|
|
|
|
binary data from another object to the Python programmer. They can also be
|
|
|
|
used as a zero-copy slicing mechanism. Using their ability to reference a
|
|
|
|
block of memory, it is possible to expose any data to the Python programmer
|
|
|
|
quite easily. The memory could be a large, constant array in a C extension,
|
|
|
|
it could be a raw block of memory for manipulation before passing to an
|
|
|
|
operating system library, or it could be used to pass around structured data
|
|
|
|
in its native, in-memory format.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-09-28 20:39:41 -03:00
|
|
|
Contrary to most data types exposed by the Python interpreter, buffers
|
2010-10-06 07:11:56 -03:00
|
|
|
are not :c:type:`PyObject` pointers but rather simple C structures. This
|
2010-09-28 20:04:04 -03:00
|
|
|
allows them to be created and copied very simply. When a generic wrapper
|
2010-09-28 20:59:51 -03:00
|
|
|
around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object
|
2010-09-28 20:04:04 -03:00
|
|
|
can be created.
|
|
|
|
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:type:: Py_buffer
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: void *buf
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2008-09-15 23:24:31 -03:00
|
|
|
A pointer to the start of the memory for the object.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: Py_ssize_t len
|
2008-09-17 19:59:21 -03:00
|
|
|
:noindex:
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2008-09-15 23:24:31 -03:00
|
|
|
The total length of the memory in bytes.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: int readonly
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2008-09-15 23:24:31 -03:00
|
|
|
An indicator of whether the buffer is read only.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: const char *format
|
2008-09-17 19:59:21 -03:00
|
|
|
:noindex:
|
2008-01-20 05:30:57 -04:00
|
|
|
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 18:06:15 -03:00
|
|
|
A *NULL* terminated string in :mod:`struct` module style syntax giving
|
|
|
|
the contents of the elements available through the buffer. If this is
|
|
|
|
*NULL*, ``"B"`` (unsigned bytes) is assumed.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: int ndim
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2008-09-15 23:24:31 -03:00
|
|
|
The number of dimensions the memory represents as a multi-dimensional
|
2010-10-06 07:11:56 -03:00
|
|
|
array. If it is 0, :c:data:`strides` and :c:data:`suboffsets` must be
|
2008-09-15 23:24:31 -03:00
|
|
|
*NULL*.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: Py_ssize_t *shape
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
|
2008-09-15 23:24:31 -03:00
|
|
|
shape of the memory as a multi-dimensional array. Note that
|
|
|
|
``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to
|
2010-10-06 07:11:56 -03:00
|
|
|
:c:data:`len`.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: Py_ssize_t *strides
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
|
2008-09-15 23:24:31 -03:00
|
|
|
number of bytes to skip to get to a new element in each dimension.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: Py_ssize_t *suboffsets
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim`. If these
|
2008-09-15 23:24:31 -03:00
|
|
|
suboffset numbers are greater than or equal to 0, then the value stored
|
|
|
|
along the indicated dimension is a pointer and the suboffset value
|
|
|
|
dictates how many bytes to add to the pointer after de-referencing. A
|
|
|
|
suboffset value that it negative indicates that no de-referencing should
|
|
|
|
occur (striding in a contiguous memory block).
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2008-09-15 23:24:31 -03:00
|
|
|
Here is a function that returns a pointer to the element in an N-D array
|
2009-03-13 16:04:40 -03:00
|
|
|
pointed to by an N-dimensional index when there are both non-NULL strides
|
2008-09-15 23:24:31 -03:00
|
|
|
and suboffsets::
|
|
|
|
|
|
|
|
void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
|
|
|
|
Py_ssize_t *suboffsets, Py_ssize_t *indices) {
|
|
|
|
char *pointer = (char*)buf;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ndim; i++) {
|
|
|
|
pointer += strides[i] * indices[i];
|
|
|
|
if (suboffsets[i] >=0 ) {
|
|
|
|
pointer = *((char**)pointer) + suboffsets[i];
|
2009-01-03 17:18:54 -04:00
|
|
|
}
|
2008-09-15 23:24:31 -03:00
|
|
|
}
|
|
|
|
return (void*)pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: Py_ssize_t itemsize
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
This is a storage for the itemsize (in bytes) of each element of the
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 18:06:15 -03:00
|
|
|
shared memory. It is technically un-necessary as it can be obtained
|
2010-10-06 07:11:56 -03:00
|
|
|
using :c:func:`PyBuffer_SizeFromFormat`, however an exporter may know
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 18:06:15 -03:00
|
|
|
this information without parsing the format string and it is necessary
|
|
|
|
to know the itemsize for proper interpretation of striding. Therefore,
|
|
|
|
storing it is more convenient and faster.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: void *internal
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
This is for use internally by the exporting object. For example, this
|
|
|
|
might be re-cast as an integer by the exporter and used to store flags
|
|
|
|
about whether or not the shape, strides, and suboffsets arrays must be
|
|
|
|
freed when the buffer is released. The consumer should never alter this
|
|
|
|
value.
|
|
|
|
|
|
|
|
|
2010-09-28 20:59:51 -03:00
|
|
|
Buffer-related functions
|
2008-09-15 23:24:31 -03:00
|
|
|
========================
|
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-09-28 20:04:04 -03:00
|
|
|
Return 1 if *obj* supports the buffer interface otherwise 0. When 1 is
|
2010-10-06 07:11:56 -03:00
|
|
|
returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
|
2010-09-28 20:04:04 -03:00
|
|
|
succeed.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-09-28 20:04:04 -03:00
|
|
|
Export a view over some internal data from the target object *obj*.
|
|
|
|
*obj* must not be NULL, and *view* must point to an existing
|
2010-10-06 07:11:56 -03:00
|
|
|
:c:type:`Py_buffer` structure allocated by the caller (most uses of
|
2010-09-28 20:04:04 -03:00
|
|
|
this function will simply declare a local variable of type
|
2010-10-06 07:11:56 -03:00
|
|
|
:c:type:`Py_buffer`). The *flags* argument is a bit field indicating
|
2010-09-28 20:04:04 -03:00
|
|
|
what kind of buffer is requested. The buffer interface allows
|
|
|
|
for complicated memory layout possibilities; however, some callers
|
|
|
|
won't want to handle all the complexity and instead request a simple
|
2010-10-06 07:11:56 -03:00
|
|
|
view of the target object (using :c:macro:`PyBUF_SIMPLE` for a read-only
|
|
|
|
view and :c:macro:`PyBUF_WRITABLE` for a read-write view).
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
Some exporters may not be able to share memory in every possible way and
|
|
|
|
may need to raise errors to signal to some consumers that something is
|
|
|
|
just not possible. These errors should be a :exc:`BufferError` unless
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 18:06:15 -03:00
|
|
|
there is another error that is actually causing the problem. The
|
|
|
|
exporter can use flags information to simplify how much of the
|
2010-10-06 07:11:56 -03:00
|
|
|
:c:data:`Py_buffer` structure is filled in with non-default values and/or
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 18:06:15 -03:00
|
|
|
raise an error if the object can't support a simpler view of its memory.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-09-28 20:04:04 -03:00
|
|
|
On success, 0 is returned and the *view* structure is filled with useful
|
|
|
|
values. On error, -1 is returned and an exception is raised; the *view*
|
|
|
|
is left in an undefined state.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-10-01 02:38:10 -03:00
|
|
|
The following are the possible values to the *flags* arguments.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_SIMPLE
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is the default flag. The returned buffer exposes a read-only
|
|
|
|
memory area. The format of data is assumed to be raw unsigned bytes,
|
|
|
|
without any particular structure. This is a "stand-alone" flag
|
|
|
|
constant. It never needs to be '|'d to the others. The exporter will
|
|
|
|
raise an error if it cannot provide such a contiguous buffer of bytes.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_WRITABLE
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
Like :c:macro:`PyBUF_SIMPLE`, but the returned buffer is writable. If
|
2010-10-01 02:38:10 -03:00
|
|
|
the exporter doesn't support writable buffers, an error is raised.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_STRIDES
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
This implies :c:macro:`PyBUF_ND`. The returned buffer must provide
|
2010-10-01 02:38:10 -03:00
|
|
|
strides information (i.e. the strides cannot be NULL). This would be
|
|
|
|
used when the consumer can handle strided, discontiguous arrays.
|
|
|
|
Handling strides automatically assumes you can handle shape. The
|
|
|
|
exporter can raise an error if a strided representation of the data is
|
|
|
|
not possible (i.e. without the suboffsets).
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_ND
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
The returned buffer must provide shape information. The memory will be
|
|
|
|
assumed C-style contiguous (last dimension varies the fastest). The
|
|
|
|
exporter may raise an error if it cannot provide this kind of
|
|
|
|
contiguous buffer. If this is not given then shape will be *NULL*.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_C_CONTIGUOUS
|
2010-10-01 02:38:10 -03:00
|
|
|
PyBUF_F_CONTIGUOUS
|
|
|
|
PyBUF_ANY_CONTIGUOUS
|
|
|
|
|
|
|
|
These flags indicate that the contiguity returned buffer must be
|
|
|
|
respectively, C-contiguous (last dimension varies the fastest), Fortran
|
|
|
|
contiguous (first dimension varies the fastest) or either one. All of
|
2010-10-06 07:11:56 -03:00
|
|
|
these flags imply :c:macro:`PyBUF_STRIDES` and guarantee that the
|
2010-10-01 02:38:10 -03:00
|
|
|
strides buffer info structure will be filled in correctly.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_INDIRECT
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This flag indicates the returned buffer must have suboffsets
|
|
|
|
information (which can be NULL if no suboffsets are needed). This can
|
|
|
|
be used when the consumer can handle indirect array referencing implied
|
2010-10-06 07:11:56 -03:00
|
|
|
by these suboffsets. This implies :c:macro:`PyBUF_STRIDES`.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_FORMAT
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
The returned buffer must have true format information if this flag is
|
|
|
|
provided. This would be used when the consumer is going to be checking
|
|
|
|
for what 'kind' of data is actually stored. An exporter should always
|
|
|
|
be able to provide this information if requested. If format is not
|
|
|
|
explicitly requested then the format must be returned as *NULL* (which
|
|
|
|
means ``'B'``, or unsigned bytes).
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_STRIDED
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_STRIDES | PyBUF_WRITABLE)``.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_STRIDED_RO
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_STRIDES)``.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_RECORDS
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_STRIDES | PyBUF_FORMAT |
|
|
|
|
PyBUF_WRITABLE)``.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_RECORDS_RO
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_STRIDES | PyBUF_FORMAT)``.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_FULL
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_INDIRECT | PyBUF_FORMAT |
|
|
|
|
PyBUF_WRITABLE)``.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_FULL_RO
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_INDIRECT | PyBUF_FORMAT)``.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_CONTIG
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_ND | PyBUF_WRITABLE)``.
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:macro:: PyBUF_CONTIG_RO
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
This is equivalent to ``(PyBUF_ND)``.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: void PyBuffer_Release(Py_buffer *view)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-06-08 19:27:07 -03:00
|
|
|
Release the buffer *view*. This should be called when the buffer is no
|
|
|
|
longer being used as it may free memory from it.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
Return the implied :c:data:`~Py_buffer.itemsize` from the struct-stype
|
|
|
|
:c:data:`~Py_buffer.format`.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
Return 1 if the memory defined by the *view* is C-style (*fortran* is
|
|
|
|
``'C'``) or Fortran-style (*fortran* is ``'F'``) contiguous or either one
|
|
|
|
(*fortran* is ``'A'``). Return 0 otherwise.
|
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
Fill the *strides* array with byte-strides of a contiguous (C-style if
|
|
|
|
*fortran* is ``'C'`` or Fortran-style if *fortran* is ``'F'`` array of the
|
|
|
|
given shape with the given number of bytes per element.
|
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
Fill in a buffer-info structure, *view*, correctly for an exporter that can
|
|
|
|
only share a contiguous chunk of memory of "unsigned bytes" of the given
|
|
|
|
length. Return 0 on success and -1 (with raising an error) on error.
|
|
|
|
|