2019-05-17 06:55:34 -03:00
|
|
|
.. highlight:: c
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2012-11-20 21:45:51 -04:00
|
|
|
.. index::
|
|
|
|
single: buffer protocol
|
|
|
|
single: buffer interface; (see buffer protocol)
|
|
|
|
single: buffer object; (see buffer protocol)
|
|
|
|
|
2008-01-20 05:30:57 -04:00
|
|
|
.. _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
|
2012-02-25 07:24:21 -04:00
|
|
|
.. sectionauthor:: Stefan Krah
|
2008-01-20 05:30:57 -04:00
|
|
|
|
|
|
|
|
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
|
2012-02-25 07:24:21 -04:00
|
|
|
then desirable, in some situations, to access that buffer directly and
|
2010-12-12 15:59:47 -04:00
|
|
|
without intermediate copying.
|
|
|
|
|
2012-11-20 21:45:51 -04:00
|
|
|
Python provides such a facility at the C level in the form of the :ref:`buffer
|
|
|
|
protocol <bufferobjects>`. This protocol has two sides:
|
2010-12-12 15:59:47 -04:00
|
|
|
|
|
|
|
.. 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,
|
2015-11-01 23:37:02 -04:00
|
|
|
the elements exposed by an :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
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
.. _buffer-structure:
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2012-03-06 09:55:06 -04:00
|
|
|
For short instructions how to write an exporting object, see
|
|
|
|
:ref:`Buffer Object Structures <buffer-structs>`. For obtaining
|
|
|
|
a buffer, see :c:func:`PyObject_GetBuffer`.
|
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
|
|
|
|
2020-08-13 17:11:50 -03:00
|
|
|
.. c:member:: void *buf
|
2014-06-25 00:37:17 -03:00
|
|
|
|
|
|
|
A pointer to the start of the logical structure described by the buffer
|
|
|
|
fields. This can be any location within the underlying physical memory
|
|
|
|
block of the exporter. For example, with negative :c:member:`~Py_buffer.strides`
|
|
|
|
the value may point to the end of the memory block.
|
|
|
|
|
2015-08-08 09:33:28 -03:00
|
|
|
For :term:`contiguous` arrays, the value points to the beginning of
|
|
|
|
the memory block.
|
2014-06-25 00:37:17 -03:00
|
|
|
|
2022-10-09 21:55:53 -03:00
|
|
|
.. c:member:: PyObject *obj
|
2012-02-25 07:24:21 -04:00
|
|
|
|
2012-03-06 09:55:06 -04:00
|
|
|
A new reference to the exporting object. The reference is owned by
|
2019-10-30 07:03:20 -03:00
|
|
|
the consumer and automatically decremented and set to ``NULL`` by
|
2012-03-06 09:55:06 -04:00
|
|
|
:c:func:`PyBuffer_Release`. The field is the equivalent of the return
|
|
|
|
value of any standard C-API function.
|
2012-02-25 07:24:21 -04:00
|
|
|
|
2012-03-06 09:55:06 -04:00
|
|
|
As a special case, for *temporary* buffers that are wrapped by
|
|
|
|
:c:func:`PyMemoryView_FromBuffer` or :c:func:`PyBuffer_FillInfo`
|
2019-10-30 07:03:20 -03:00
|
|
|
this field is ``NULL``. In general, exporting objects MUST NOT
|
2012-03-06 09:55:06 -04:00
|
|
|
use this scheme.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:member:: Py_ssize_t len
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
``product(shape) * itemsize``. For contiguous arrays, this is the length
|
|
|
|
of the underlying memory block. For non-contiguous arrays, it is the length
|
|
|
|
that the logical structure would have if it were copied to a contiguous
|
|
|
|
representation.
|
|
|
|
|
|
|
|
Accessing ``((char *)buf)[0] up to ((char *)buf)[len-1]`` is only valid
|
|
|
|
if the buffer has been obtained by a request that guarantees contiguity. In
|
|
|
|
most cases such a request will be :c:macro:`PyBUF_SIMPLE` or :c:macro:`PyBUF_WRITABLE`.
|
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
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
An indicator of whether the buffer is read-only. This field is controlled
|
|
|
|
by the :c:macro:`PyBUF_WRITABLE` flag.
|
|
|
|
|
|
|
|
.. c:member:: Py_ssize_t itemsize
|
|
|
|
|
|
|
|
Item size in bytes of a single element. Same as the value of :func:`struct.calcsize`
|
2019-10-30 16:37:16 -03:00
|
|
|
called on non-``NULL`` :c:member:`~Py_buffer.format` values.
|
2012-02-25 07:24:21 -04:00
|
|
|
|
|
|
|
Important exception: If a consumer requests a buffer without the
|
2015-08-18 03:38:34 -03:00
|
|
|
:c:macro:`PyBUF_FORMAT` flag, :c:member:`~Py_buffer.format` will
|
2019-10-30 07:03:20 -03:00
|
|
|
be set to ``NULL``, but :c:member:`~Py_buffer.itemsize` still has
|
2012-02-25 07:24:21 -04:00
|
|
|
the value for the original format.
|
|
|
|
|
2015-08-18 03:38:34 -03:00
|
|
|
If :c:member:`~Py_buffer.shape` is present, the equality
|
2012-02-25 07:24:21 -04:00
|
|
|
``product(shape) * itemsize == len`` still holds and the consumer
|
|
|
|
can use :c:member:`~Py_buffer.itemsize` to navigate the buffer.
|
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
If :c:member:`~Py_buffer.shape` is ``NULL`` as a result of a :c:macro:`PyBUF_SIMPLE`
|
2012-02-25 07:24:21 -04:00
|
|
|
or a :c:macro:`PyBUF_WRITABLE` request, the consumer must disregard
|
|
|
|
:c:member:`~Py_buffer.itemsize` and assume ``itemsize == 1``.
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2020-08-13 17:11:50 -03:00
|
|
|
.. c:member:: const char *format
|
2008-01-20 05:30:57 -04:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
A *NUL* terminated string in :mod:`struct` module style syntax describing
|
2019-10-30 07:03:20 -03:00
|
|
|
the contents of a single item. If this is ``NULL``, ``"B"`` (unsigned bytes)
|
2012-02-25 07:24:21 -04:00
|
|
|
is assumed.
|
|
|
|
|
|
|
|
This field is controlled by the :c:macro:`PyBUF_FORMAT` flag.
|
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
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
The number of dimensions the memory represents as an n-dimensional array.
|
2016-10-27 15:41:19 -03:00
|
|
|
If it is ``0``, :c:member:`~Py_buffer.buf` points to a single item representing
|
2012-02-25 07:24:21 -04:00
|
|
|
a scalar. In this case, :c:member:`~Py_buffer.shape`, :c:member:`~Py_buffer.strides`
|
2019-10-30 07:03:20 -03:00
|
|
|
and :c:member:`~Py_buffer.suboffsets` MUST be ``NULL``.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
The macro :c:macro:`PyBUF_MAX_NDIM` limits the maximum number of dimensions
|
|
|
|
to 64. Exporters MUST respect this limit, consumers of multi-dimensional
|
|
|
|
buffers SHOULD be able to handle up to :c:macro:`PyBUF_MAX_NDIM` dimensions.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2020-08-13 17:11:50 -03:00
|
|
|
.. c:member:: Py_ssize_t *shape
|
2012-02-25 07:24:21 -04:00
|
|
|
|
|
|
|
An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
|
|
|
|
indicating the shape of the memory as an n-dimensional array. Note that
|
|
|
|
``shape[0] * ... * shape[ndim-1] * itemsize`` MUST be equal to
|
|
|
|
:c:member:`~Py_buffer.len`.
|
|
|
|
|
|
|
|
Shape values are restricted to ``shape[n] >= 0``. The case
|
|
|
|
``shape[n] == 0`` requires special attention. See `complex arrays`_
|
|
|
|
for further information.
|
|
|
|
|
|
|
|
The shape array is read-only for the consumer.
|
|
|
|
|
2020-08-13 17:11:50 -03:00
|
|
|
.. c:member:: Py_ssize_t *strides
|
2012-02-25 07:24:21 -04:00
|
|
|
|
|
|
|
An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`
|
|
|
|
giving the number of bytes to skip to get to a new element in each
|
|
|
|
dimension.
|
|
|
|
|
|
|
|
Stride values can be any integer. For regular arrays, strides are
|
|
|
|
usually positive, but a consumer MUST be able to handle the case
|
|
|
|
``strides[n] <= 0``. See `complex arrays`_ for further information.
|
|
|
|
|
|
|
|
The strides array is read-only for the consumer.
|
|
|
|
|
2020-08-13 17:11:50 -03:00
|
|
|
.. c:member:: Py_ssize_t *suboffsets
|
2012-02-25 07:24:21 -04:00
|
|
|
|
|
|
|
An array of :c:type:`Py_ssize_t` of length :c:member:`~Py_buffer.ndim`.
|
|
|
|
If ``suboffsets[n] >= 0``, the values stored along the nth dimension are
|
|
|
|
pointers and the suboffset value dictates how many bytes to add to each
|
|
|
|
pointer after de-referencing. A suboffset value that is negative
|
|
|
|
indicates that no de-referencing should occur (striding in a contiguous
|
|
|
|
memory block).
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2018-12-05 15:45:30 -04:00
|
|
|
If all suboffsets are negative (i.e. no de-referencing is needed), then
|
2019-10-30 16:37:16 -03:00
|
|
|
this field must be ``NULL`` (the default value).
|
2015-02-01 14:42:12 -04:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
This type of array representation is used by the Python Imaging Library
|
|
|
|
(PIL). See `complex arrays`_ for further information how to access elements
|
|
|
|
of such an array.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
The suboffsets array is read-only for the consumer.
|
|
|
|
|
2020-08-13 17:11:50 -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
|
2012-02-25 07:24:21 -04:00
|
|
|
freed when the buffer is released. The consumer MUST NOT alter this
|
2008-09-15 23:24:31 -03:00
|
|
|
value.
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
.. _buffer-request-types:
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Buffer request types
|
|
|
|
====================
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Buffers are usually obtained by sending a buffer request to an exporting
|
|
|
|
object via :c:func:`PyObject_GetBuffer`. Since the complexity of the logical
|
|
|
|
structure of the memory can vary drastically, the consumer uses the *flags*
|
|
|
|
argument to specify the exact buffer type it can handle.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
All :c:data:`Py_buffer` fields are unambiguously defined by the request
|
|
|
|
type.
|
|
|
|
|
|
|
|
request-independent fields
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
The following fields are not influenced by *flags* and must always be filled in
|
|
|
|
with the correct values: :c:member:`~Py_buffer.obj`, :c:member:`~Py_buffer.buf`,
|
|
|
|
:c:member:`~Py_buffer.len`, :c:member:`~Py_buffer.itemsize`, :c:member:`~Py_buffer.ndim`.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
readonly, format
|
|
|
|
~~~~~~~~~~~~~~~~
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
.. c:macro:: PyBUF_WRITABLE
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Controls the :c:member:`~Py_buffer.readonly` field. If set, the exporter
|
|
|
|
MUST provide a writable buffer or else report failure. Otherwise, the
|
|
|
|
exporter MAY provide either a read-only or writable buffer, but the choice
|
|
|
|
MUST be consistent for all consumers.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
.. c:macro:: PyBUF_FORMAT
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Controls the :c:member:`~Py_buffer.format` field. If set, this field MUST
|
2019-10-30 07:03:20 -03:00
|
|
|
be filled in correctly. Otherwise, this field MUST be ``NULL``.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
:c:macro:`PyBUF_WRITABLE` can be \|'d to any of the flags in the next section.
|
|
|
|
Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE`
|
|
|
|
can be used as a stand-alone flag to request a simple writable buffer.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
:c:macro:`PyBUF_FORMAT` can be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`.
|
|
|
|
The latter already implies format ``B`` (unsigned bytes).
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
shape, strides, suboffsets
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
The flags that control the logical structure of the memory are listed
|
|
|
|
in decreasing order of complexity. Note that each flag contains all bits
|
|
|
|
of the flags below it.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2013-03-28 09:28:44 -03:00
|
|
|
.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
+-----------------------------+-------+---------+------------+
|
|
|
|
| Request | shape | strides | suboffsets |
|
|
|
|
+=============================+=======+=========+============+
|
|
|
|
| .. c:macro:: PyBUF_INDIRECT | yes | yes | if needed |
|
|
|
|
+-----------------------------+-------+---------+------------+
|
|
|
|
| .. c:macro:: PyBUF_STRIDES | yes | yes | NULL |
|
|
|
|
+-----------------------------+-------+---------+------------+
|
|
|
|
| .. c:macro:: PyBUF_ND | yes | NULL | NULL |
|
|
|
|
+-----------------------------+-------+---------+------------+
|
|
|
|
| .. c:macro:: PyBUF_SIMPLE | NULL | NULL | NULL |
|
|
|
|
+-----------------------------+-------+---------+------------+
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
|
2015-08-08 09:33:28 -03:00
|
|
|
.. index:: contiguous, C-contiguous, Fortran contiguous
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
contiguity requests
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2015-08-08 09:33:28 -03:00
|
|
|
C or Fortran :term:`contiguity <contiguous>` can be explicitly requested,
|
|
|
|
with and without stride information. Without stride information, the buffer
|
|
|
|
must be C-contiguous.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2013-03-28 09:28:44 -03:00
|
|
|
.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
+-----------------------------------+-------+---------+------------+--------+
|
|
|
|
| Request | shape | strides | suboffsets | contig |
|
|
|
|
+===================================+=======+=========+============+========+
|
|
|
|
| .. c:macro:: PyBUF_C_CONTIGUOUS | yes | yes | NULL | C |
|
|
|
|
+-----------------------------------+-------+---------+------------+--------+
|
|
|
|
| .. c:macro:: PyBUF_F_CONTIGUOUS | yes | yes | NULL | F |
|
|
|
|
+-----------------------------------+-------+---------+------------+--------+
|
|
|
|
| .. c:macro:: PyBUF_ANY_CONTIGUOUS | yes | yes | NULL | C or F |
|
|
|
|
+-----------------------------------+-------+---------+------------+--------+
|
2020-08-13 14:16:02 -03:00
|
|
|
| :c:macro:`PyBUF_ND` | yes | NULL | NULL | C |
|
2012-02-25 07:24:21 -04:00
|
|
|
+-----------------------------------+-------+---------+------------+--------+
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
compound requests
|
|
|
|
~~~~~~~~~~~~~~~~~
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
All possible requests are fully defined by some combination of the flags in
|
|
|
|
the previous section. For convenience, the buffer protocol provides frequently
|
|
|
|
used combinations as single flags.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
In the following table *U* stands for undefined contiguity. The consumer would
|
|
|
|
have to call :c:func:`PyBuffer_IsContiguous` to determine contiguity.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2013-03-28 09:28:44 -03:00
|
|
|
.. tabularcolumns:: |p{0.35\linewidth}|l|l|l|l|l|l|
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| Request | shape | strides | suboffsets | contig | readonly | format |
|
|
|
|
+===============================+=======+=========+============+========+==========+========+
|
|
|
|
| .. c:macro:: PyBUF_FULL | yes | yes | if needed | U | 0 | yes |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| .. c:macro:: PyBUF_FULL_RO | yes | yes | if needed | U | 1 or 0 | yes |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| .. c:macro:: PyBUF_RECORDS | yes | yes | NULL | U | 0 | yes |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| .. c:macro:: PyBUF_RECORDS_RO | yes | yes | NULL | U | 1 or 0 | yes |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| .. c:macro:: PyBUF_STRIDED | yes | yes | NULL | U | 0 | NULL |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| .. c:macro:: PyBUF_STRIDED_RO | yes | yes | NULL | U | 1 or 0 | NULL |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| .. c:macro:: PyBUF_CONTIG | yes | NULL | NULL | C | 0 | NULL |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
|
|
|
| .. c:macro:: PyBUF_CONTIG_RO | yes | NULL | NULL | C | 1 or 0 | NULL |
|
|
|
|
+-------------------------------+-------+---------+------------+--------+----------+--------+
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Complex arrays
|
|
|
|
==============
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
NumPy-style: shape and strides
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The logical structure of NumPy-style arrays is defined by :c:member:`~Py_buffer.itemsize`,
|
|
|
|
:c:member:`~Py_buffer.ndim`, :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides`.
|
|
|
|
|
|
|
|
If ``ndim == 0``, the memory location pointed to by :c:member:`~Py_buffer.buf` is
|
|
|
|
interpreted as a scalar of size :c:member:`~Py_buffer.itemsize`. In that case,
|
2019-10-30 07:03:20 -03:00
|
|
|
both :c:member:`~Py_buffer.shape` and :c:member:`~Py_buffer.strides` are ``NULL``.
|
2012-02-25 07:24:21 -04:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
If :c:member:`~Py_buffer.strides` is ``NULL``, the array is interpreted as
|
2012-02-25 07:24:21 -04:00
|
|
|
a standard n-dimensional C-array. Otherwise, the consumer must access an
|
|
|
|
n-dimensional array as follows:
|
|
|
|
|
2019-07-17 05:13:01 -03:00
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1];
|
|
|
|
item = *((typeof(item) *)ptr);
|
2012-02-25 07:24:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
As noted above, :c:member:`~Py_buffer.buf` can point to any location within
|
|
|
|
the actual memory block. An exporter can check the validity of a buffer with
|
|
|
|
this function:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
|
|
|
|
"""Verify that the parameters represent a valid array within
|
|
|
|
the bounds of the allocated memory:
|
|
|
|
char *mem: start of the physical memory block
|
|
|
|
memlen: length of the physical memory block
|
|
|
|
offset: (char *)buf - mem
|
|
|
|
"""
|
|
|
|
if offset % itemsize:
|
|
|
|
return False
|
|
|
|
if offset < 0 or offset+itemsize > memlen:
|
|
|
|
return False
|
|
|
|
if any(v % itemsize for v in strides):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if ndim <= 0:
|
|
|
|
return ndim == 0 and not shape and not strides
|
|
|
|
if 0 in shape:
|
|
|
|
return True
|
|
|
|
|
|
|
|
imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
|
|
|
|
if strides[j] <= 0)
|
|
|
|
imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
|
|
|
|
if strides[j] > 0)
|
|
|
|
|
|
|
|
return 0 <= offset+imin and offset+imax+itemsize <= memlen
|
|
|
|
|
|
|
|
|
|
|
|
PIL-style: shape, strides and suboffsets
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
In addition to the regular items, PIL-style arrays can contain pointers
|
|
|
|
that must be followed in order to get to the next element in a dimension.
|
|
|
|
For example, the regular three-dimensional C-array ``char v[2][2][3]`` can
|
|
|
|
also be viewed as an array of 2 pointers to 2 two-dimensional arrays:
|
|
|
|
``char (*v[2])[2][3]``. In suboffsets representation, those two pointers
|
|
|
|
can be embedded at the start of :c:member:`~Py_buffer.buf`, pointing
|
|
|
|
to two ``char x[2][3]`` arrays that can be located anywhere in memory.
|
|
|
|
|
|
|
|
|
|
|
|
Here is a function that returns a pointer to the element in an N-D array
|
2019-10-30 16:37:16 -03:00
|
|
|
pointed to by an N-dimensional index when there are both non-``NULL`` strides
|
2012-02-25 07:24:21 -04: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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (void*)pointer;
|
|
|
|
}
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Buffer-related functions
|
|
|
|
========================
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2016-10-27 15:41:19 -03:00
|
|
|
Return ``1`` if *obj* supports the buffer interface otherwise ``0``. When ``1`` is
|
2012-02-25 07:24:21 -04:00
|
|
|
returned, it doesn't guarantee that :c:func:`PyObject_GetBuffer` will
|
2018-12-18 07:57:17 -04:00
|
|
|
succeed. This function always succeeds.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
.. c:function:: int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Send a request to *exporter* to fill in *view* as specified by *flags*.
|
|
|
|
If the exporter cannot provide a buffer of the exact type, it MUST raise
|
2020-08-13 17:11:50 -03:00
|
|
|
:c:data:`PyExc_BufferError`, set ``view->obj`` to ``NULL`` and
|
2016-10-27 15:41:19 -03:00
|
|
|
return ``-1``.
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2020-08-13 17:11:50 -03:00
|
|
|
On success, fill in *view*, set ``view->obj`` to a new reference
|
2012-03-06 09:55:06 -04:00
|
|
|
to *exporter* and return 0. In the case of chained buffer providers
|
2020-08-13 17:11:50 -03:00
|
|
|
that redirect requests to a single object, ``view->obj`` MAY
|
2012-03-06 09:55:06 -04:00
|
|
|
refer to this object instead of *exporter* (See :ref:`Buffer Object Structures <buffer-structs>`).
|
2010-10-01 02:38:10 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Successful calls to :c:func:`PyObject_GetBuffer` must be paired with calls
|
|
|
|
to :c:func:`PyBuffer_Release`, similar to :c:func:`malloc` and :c:func:`free`.
|
|
|
|
Thus, after the consumer is done with the buffer, :c:func:`PyBuffer_Release`
|
|
|
|
must be called exactly once.
|
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
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Release the buffer *view* and decrement the reference count for
|
2020-08-13 17:11:50 -03:00
|
|
|
``view->obj``. This function MUST be called when the buffer
|
2012-02-25 07:24:21 -04:00
|
|
|
is no longer being used, otherwise reference leaks may occur.
|
|
|
|
|
|
|
|
It is an error to call this function on a buffer that was not obtained via
|
|
|
|
:c:func:`PyObject_GetBuffer`.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2019-08-20 11:46:36 -03:00
|
|
|
.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *format)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
Return the implied :c:data:`~Py_buffer.itemsize` from :c:data:`~Py_buffer.format`.
|
2019-08-20 11:46:36 -03:00
|
|
|
On error, raise an exception and return -1.
|
|
|
|
|
|
|
|
.. versionadded:: 3.9
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2021-12-22 09:07:46 -04:00
|
|
|
.. c:function:: int PyBuffer_IsContiguous(const Py_buffer *view, char order)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2016-10-27 15:41:19 -03:00
|
|
|
Return ``1`` if the memory defined by the *view* is C-style (*order* is
|
2015-08-08 09:33:28 -03:00
|
|
|
``'C'``) or Fortran-style (*order* is ``'F'``) :term:`contiguous` or either one
|
2018-12-18 07:57:17 -04:00
|
|
|
(*order* is ``'A'``). Return ``0`` otherwise. This function always succeeds.
|
2008-09-15 23:24:31 -03:00
|
|
|
|
|
|
|
|
2021-12-22 09:07:46 -04:00
|
|
|
.. c:function:: void* PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices)
|
2019-07-31 11:48:15 -03:00
|
|
|
|
|
|
|
Get the memory area pointed to by the *indices* inside the given *view*.
|
|
|
|
*indices* must point to an array of ``view->ndim`` indices.
|
|
|
|
|
|
|
|
|
2021-12-22 09:07:46 -04:00
|
|
|
.. c:function:: int PyBuffer_FromContiguous(const Py_buffer *view, const void *buf, Py_ssize_t len, char fort)
|
2019-09-11 13:38:47 -03:00
|
|
|
|
|
|
|
Copy contiguous *len* bytes from *buf* to *view*.
|
|
|
|
*fort* can be ``'C'`` or ``'F'`` (for C-style or Fortran-style ordering).
|
|
|
|
``0`` is returned on success, ``-1`` on error.
|
|
|
|
|
|
|
|
|
2021-12-22 09:07:46 -04:00
|
|
|
.. c:function:: int PyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)
|
2018-03-28 12:26:32 -03:00
|
|
|
|
|
|
|
Copy *len* bytes from *src* to its contiguous representation in *buf*.
|
2019-09-11 14:25:55 -03:00
|
|
|
*order* can be ``'C'`` or ``'F'`` or ``'A'`` (for C-style or Fortran-style
|
|
|
|
ordering or either one). ``0`` is returned on success, ``-1`` on error.
|
2018-03-28 12:26:32 -03:00
|
|
|
|
|
|
|
This function fails if *len* != *src->len*.
|
|
|
|
|
|
|
|
|
2023-03-03 13:16:50 -04:00
|
|
|
.. c:function:: int PyObject_CopyData(PyObject *dest, PyObject *src)
|
2022-02-02 11:03:10 -04:00
|
|
|
|
|
|
|
Copy data from *src* to *dest* buffer. Can convert between C-style and
|
|
|
|
or Fortran-style buffers.
|
|
|
|
|
|
|
|
``0`` is returned on success, ``-1`` on error.
|
|
|
|
|
2017-10-15 04:31:36 -03:00
|
|
|
.. c:function:: void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)
|
2008-09-15 23:24:31 -03:00
|
|
|
|
2015-08-08 09:33:28 -03:00
|
|
|
Fill the *strides* array with byte-strides of a :term:`contiguous` (C-style if
|
2012-02-25 07:24:21 -04:00
|
|
|
*order* is ``'C'`` or Fortran-style if *order* is ``'F'``) array of the
|
2008-09-15 23:24:31 -03:00
|
|
|
given shape with the given number of bytes per element.
|
|
|
|
|
|
|
|
|
2012-02-25 07:24:21 -04:00
|
|
|
.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags)
|
|
|
|
|
|
|
|
Handle buffer requests for an exporter that wants to expose *buf* of size *len*
|
|
|
|
with writability set according to *readonly*. *buf* is interpreted as a sequence
|
|
|
|
of unsigned bytes.
|
|
|
|
|
|
|
|
The *flags* argument indicates the request type. This function always fills in
|
|
|
|
*view* as specified by flags, unless *buf* has been designated as read-only
|
|
|
|
and :c:macro:`PyBUF_WRITABLE` is set in *flags*.
|
|
|
|
|
2020-08-13 17:11:50 -03:00
|
|
|
On success, set ``view->obj`` to a new reference to *exporter* and
|
2012-02-25 07:24:21 -04:00
|
|
|
return 0. Otherwise, raise :c:data:`PyExc_BufferError`, set
|
2020-08-13 17:11:50 -03:00
|
|
|
``view->obj`` to ``NULL`` and return ``-1``;
|
2012-02-25 07:24:21 -04:00
|
|
|
|
|
|
|
If this function is used as part of a :ref:`getbufferproc <buffer-structs>`,
|
2014-06-29 19:15:45 -03:00
|
|
|
*exporter* MUST be set to the exporting object and *flags* must be passed
|
2019-10-30 16:37:16 -03:00
|
|
|
unmodified. Otherwise, *exporter* MUST be ``NULL``.
|