summaryrefslogtreecommitdiffstats
path: root/Doc/c-api/buffer.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/c-api/buffer.rst')
-rw-r--r--Doc/c-api/buffer.rst296
1 files changed, 148 insertions, 148 deletions
diff --git a/Doc/c-api/buffer.rst b/Doc/c-api/buffer.rst
index 21e8c4a..7b6d1ae 100644
--- a/Doc/c-api/buffer.rst
+++ b/Doc/c-api/buffer.rst
@@ -27,7 +27,7 @@ should be noted that array elements may be multi-byte values.
An example user of the buffer interface is the file object's :meth:`write`
method. Any object that can export a series of bytes through the buffer
interface can be written to a file. There are a number of format codes to
-:cfunc:`PyArg_ParseTuple` that operate against an object's buffer interface,
+:c:func:`PyArg_ParseTuple` that operate against an object's buffer interface,
returning data from the target object.
Starting from version 1.6, Python has been providing Python-level buffer
@@ -47,49 +47,49 @@ The new-style Py_buffer struct
==============================
-.. ctype:: Py_buffer
+.. c:type:: Py_buffer
- .. cmember:: void *buf
+ .. c:member:: void *buf
A pointer to the start of the memory for the object.
- .. cmember:: Py_ssize_t len
+ .. c:member:: Py_ssize_t len
:noindex:
The total length of the memory in bytes.
- .. cmember:: int readonly
+ .. c:member:: int readonly
An indicator of whether the buffer is read only.
- .. cmember:: const char *format
+ .. c:member:: const char *format
:noindex:
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.
- .. cmember:: int ndim
+ .. c:member:: int ndim
The number of dimensions the memory represents as a multi-dimensional
- array. If it is 0, :cdata:`strides` and :cdata:`suboffsets` must be
+ array. If it is 0, :c:data:`strides` and :c:data:`suboffsets` must be
*NULL*.
- .. cmember:: Py_ssize_t *shape
+ .. c:member:: Py_ssize_t *shape
- An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim` giving the
+ An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
shape of the memory as a multi-dimensional array. Note that
``((*shape)[0] * ... * (*shape)[ndims-1])*itemsize`` should be equal to
- :cdata:`len`.
+ :c:data:`len`.
- .. cmember:: Py_ssize_t *strides
+ .. c:member:: Py_ssize_t *strides
- An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim` giving the
+ An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim` giving the
number of bytes to skip to get to a new element in each dimension.
- .. cmember:: Py_ssize_t *suboffsets
+ .. c:member:: Py_ssize_t *suboffsets
- An array of :ctype:`Py_ssize_t`\s the length of :cdata:`ndim`. If these
+ An array of :c:type:`Py_ssize_t`\s the length of :c:data:`ndim`. If these
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
@@ -114,16 +114,16 @@ The new-style Py_buffer struct
}
- .. cmember:: Py_ssize_t itemsize
+ .. c:member:: Py_ssize_t itemsize
This is a storage for the itemsize (in bytes) of each element of the
shared memory. It is technically un-necessary as it can be obtained
- using :cfunc:`PyBuffer_SizeFromFormat`, however an exporter may know
+ using :c:func:`PyBuffer_SizeFromFormat`, however an exporter may know
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.
- .. cmember:: void *internal
+ .. c:member:: void *internal
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
@@ -136,14 +136,14 @@ Buffer related functions
========================
-.. cfunction:: int PyObject_CheckBuffer(PyObject *obj)
+.. c:function:: int PyObject_CheckBuffer(PyObject *obj)
Return 1 if *obj* supports the buffer interface otherwise 0.
-.. cfunction:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
+.. c:function:: int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
- Export *obj* into a :ctype:`Py_buffer`, *view*. These arguments must
+ Export *obj* into a :c:type:`Py_buffer`, *view*. These arguments must
never be *NULL*. The *flags* argument is a bit field indicating what
kind of buffer the caller is prepared to deal with and therefore what
kind of buffer the exporter is allowed to return. The buffer interface
@@ -156,131 +156,131 @@ Buffer related functions
just not possible. These errors should be a :exc:`BufferError` unless
there is another error that is actually causing the problem. The
exporter can use flags information to simplify how much of the
- :cdata:`Py_buffer` structure is filled in with non-default values and/or
+ :c:data:`Py_buffer` structure is filled in with non-default values and/or
raise an error if the object can't support a simpler view of its memory.
0 is returned on success and -1 on error.
The following table gives possible values to the *flags* arguments.
- +------------------------------+---------------------------------------------------+
- | Flag | Description |
- +==============================+===================================================+
- | :cmacro:`PyBUF_SIMPLE` | This is the default flag state. The returned |
- | | buffer may or may not have writable memory. The |
- | | format of the data will be assumed to be unsigned |
- | | bytes. 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. |
- | | |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_WRITABLE` | The returned buffer must be writable. If it is |
- | | not writable, then raise an error. |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_STRIDES` | This implies :cmacro:`PyBUF_ND`. The returned |
- | | buffer must provide 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). |
- | | |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_ND` | 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*. |
- | | |
- | | |
- | | |
- +------------------------------+---------------------------------------------------+
- |:cmacro:`PyBUF_C_CONTIGUOUS` | These flags indicate that the contiguity returned |
- |:cmacro:`PyBUF_F_CONTIGUOUS` | buffer must be respectively, C-contiguous (last |
- |:cmacro:`PyBUF_ANY_CONTIGUOUS`| dimension varies the fastest), Fortran contiguous |
- | | (first dimension varies the fastest) or either |
- | | one. All of these flags imply |
- | | :cmacro:`PyBUF_STRIDES` and guarantee that the |
- | | strides buffer info structure will be filled in |
- | | correctly. |
- | | |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_INDIRECT` | 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 by these suboffsets. This |
- | | implies :cmacro:`PyBUF_STRIDES`. |
- | | |
- | | |
- | | |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_FORMAT` | 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) |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_STRIDED` | This is equivalent to ``(PyBUF_STRIDES | |
- | | PyBUF_WRITABLE)``. |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_STRIDED_RO` | This is equivalent to ``(PyBUF_STRIDES)``. |
- | | |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_RECORDS` | This is equivalent to ``(PyBUF_STRIDES | |
- | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_RECORDS_RO` | This is equivalent to ``(PyBUF_STRIDES | |
- | | PyBUF_FORMAT)``. |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_FULL` | This is equivalent to ``(PyBUF_INDIRECT | |
- | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_FULL_RO` | This is equivalent to ``(PyBUF_INDIRECT | |
- | | PyBUF_FORMAT)``. |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_CONTIG` | This is equivalent to ``(PyBUF_ND | |
- | | PyBUF_WRITABLE)``. |
- +------------------------------+---------------------------------------------------+
- | :cmacro:`PyBUF_CONTIG_RO` | This is equivalent to ``(PyBUF_ND)``. |
- | | |
- +------------------------------+---------------------------------------------------+
-
-
-.. cfunction:: void PyBuffer_Release(Py_buffer *view)
+ +-------------------------------+---------------------------------------------------+
+ | Flag | Description |
+ +===============================+===================================================+
+ | :c:macro:`PyBUF_SIMPLE` | This is the default flag state. The returned |
+ | | buffer may or may not have writable memory. The |
+ | | format of the data will be assumed to be unsigned |
+ | | bytes. 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. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_WRITABLE` | The returned buffer must be writable. If it is |
+ | | not writable, then raise an error. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_STRIDES` | This implies :c:macro:`PyBUF_ND`. The returned |
+ | | buffer must provide 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). |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_ND` | 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*. |
+ | | |
+ | | |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ |:c:macro:`PyBUF_C_CONTIGUOUS` | These flags indicate that the contiguity returned |
+ |:c:macro:`PyBUF_F_CONTIGUOUS` | buffer must be respectively, C-contiguous (last |
+ |:c:macro:`PyBUF_ANY_CONTIGUOUS`| dimension varies the fastest), Fortran contiguous |
+ | | (first dimension varies the fastest) or either |
+ | | one. All of these flags imply |
+ | | :c:macro:`PyBUF_STRIDES` and guarantee that the |
+ | | strides buffer info structure will be filled in |
+ | | correctly. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_INDIRECT` | 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 by these suboffsets. This |
+ | | implies :c:macro:`PyBUF_STRIDES`. |
+ | | |
+ | | |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_FORMAT` | 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) |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_STRIDED` | This is equivalent to ``(PyBUF_STRIDES | |
+ | | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_STRIDED_RO` | This is equivalent to ``(PyBUF_STRIDES)``. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_RECORDS` | This is equivalent to ``(PyBUF_STRIDES | |
+ | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_RECORDS_RO` | This is equivalent to ``(PyBUF_STRIDES | |
+ | | PyBUF_FORMAT)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_FULL` | This is equivalent to ``(PyBUF_INDIRECT | |
+ | | PyBUF_FORMAT | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_FULL_RO` | This is equivalent to ``(PyBUF_INDIRECT | |
+ | | PyBUF_FORMAT)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_CONTIG` | This is equivalent to ``(PyBUF_ND | |
+ | | PyBUF_WRITABLE)``. |
+ +-------------------------------+---------------------------------------------------+
+ | :c:macro:`PyBUF_CONTIG_RO` | This is equivalent to ``(PyBUF_ND)``. |
+ | | |
+ +-------------------------------+---------------------------------------------------+
+
+
+.. c:function:: void PyBuffer_Release(Py_buffer *view)
Release the buffer *view*. This should be called when the buffer
is no longer being used as it may free memory from it.
-.. cfunction:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
+.. c:function:: Py_ssize_t PyBuffer_SizeFromFormat(const char *)
- Return the implied :cdata:`~Py_buffer.itemsize` from the struct-stype
- :cdata:`~Py_buffer.format`.
+ Return the implied :c:data:`~Py_buffer.itemsize` from the struct-stype
+ :c:data:`~Py_buffer.format`.
-.. cfunction:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
+.. c:function:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
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.
-.. cfunction:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran)
+.. c:function:: void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char fortran)
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.
-.. cfunction:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
+.. c:function:: int PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int infoflags)
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
@@ -295,13 +295,13 @@ MemoryView objects
A :class:`memoryview` object exposes the new C level buffer interface as a
Python object which can then be passed around like any other object.
-.. cfunction:: PyObject *PyMemoryView_FromObject(PyObject *obj)
+.. c:function:: PyObject *PyMemoryView_FromObject(PyObject *obj)
Create a memoryview object from an object that defines the new buffer
interface.
-.. cfunction:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
+.. c:function:: PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
Create a memoryview object wrapping the given buffer-info structure *view*.
The memoryview object then owns the buffer, which means you shouldn't
@@ -309,7 +309,7 @@ Python object which can then be passed around like any other object.
memoryview object.
-.. cfunction:: PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
+.. c:function:: PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)
Create a memoryview object to a contiguous chunk of memory (in either
'C' or 'F'ortran *order*) from an object that defines the buffer
@@ -318,13 +318,13 @@ Python object which can then be passed around like any other object.
new bytes object.
-.. cfunction:: int PyMemoryView_Check(PyObject *obj)
+.. c:function:: int PyMemoryView_Check(PyObject *obj)
Return true if the object *obj* is a memoryview object. It is not
currently allowed to create subclasses of :class:`memoryview`.
-.. cfunction:: Py_buffer *PyMemoryView_GET_BUFFER(PyObject *obj)
+.. c:function:: Py_buffer *PyMemoryView_GET_BUFFER(PyObject *obj)
Return a pointer to the buffer-info structure wrapped by the given
object. The object **must** be a memoryview instance; this macro doesn't
@@ -337,7 +337,7 @@ Old-style buffer objects
.. index:: single: PyBufferProcs
More information on the old buffer interface is provided in the section
-:ref:`buffer-structs`, under the description for :ctype:`PyBufferProcs`.
+:ref:`buffer-structs`, under the description for :c:type:`PyBufferProcs`.
A "buffer object" is defined in the :file:`bufferobject.h` header (included by
:file:`Python.h`). These objects look very similar to string objects at the
@@ -356,36 +356,36 @@ system library, or it could be used to pass around structured data in its
native, in-memory format.
-.. ctype:: PyBufferObject
+.. c:type:: PyBufferObject
- This subtype of :ctype:`PyObject` represents a buffer object.
+ This subtype of :c:type:`PyObject` represents a buffer object.
-.. cvar:: PyTypeObject PyBuffer_Type
+.. c:var:: PyTypeObject PyBuffer_Type
.. index:: single: BufferType (in module types)
- The instance of :ctype:`PyTypeObject` which represents the Python buffer type;
+ The instance of :c:type:`PyTypeObject` which represents the Python buffer type;
it is the same object as ``buffer`` and ``types.BufferType`` in the Python
layer. .
-.. cvar:: int Py_END_OF_BUFFER
+.. c:var:: int Py_END_OF_BUFFER
This constant may be passed as the *size* parameter to
- :cfunc:`PyBuffer_FromObject` or :cfunc:`PyBuffer_FromReadWriteObject`. It
- indicates that the new :ctype:`PyBufferObject` should refer to *base*
+ :c:func:`PyBuffer_FromObject` or :c:func:`PyBuffer_FromReadWriteObject`. It
+ indicates that the new :c:type:`PyBufferObject` should refer to *base*
object from the specified *offset* to the end of its exported buffer.
Using this enables the caller to avoid querying the *base* object for its
length.
-.. cfunction:: int PyBuffer_Check(PyObject *p)
+.. c:function:: int PyBuffer_Check(PyObject *p)
- Return true if the argument has type :cdata:`PyBuffer_Type`.
+ Return true if the argument has type :c:data:`PyBuffer_Type`.
-.. cfunction:: PyObject* PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
+.. c:function:: PyObject* PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
Return a new read-only buffer object. This raises :exc:`TypeError` if
*base* doesn't support the read-only buffer protocol or doesn't provide
@@ -397,24 +397,24 @@ native, in-memory format.
length of the *base* object's exported buffer data.
.. versionchanged:: 2.5
- This function used an :ctype:`int` type for *offset* and *size*. This
+ This function used an :c:type:`int` type for *offset* and *size*. This
might require changes in your code for properly supporting 64-bit
systems.
-.. cfunction:: PyObject* PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
+.. c:function:: PyObject* PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
Return a new writable buffer object. Parameters and exceptions are similar
- to those for :cfunc:`PyBuffer_FromObject`. If the *base* object does not
+ to those for :c:func:`PyBuffer_FromObject`. If the *base* object does not
export the writeable buffer protocol, then :exc:`TypeError` is raised.
.. versionchanged:: 2.5
- This function used an :ctype:`int` type for *offset* and *size*. This
+ This function used an :c:type:`int` type for *offset* and *size*. This
might require changes in your code for properly supporting 64-bit
systems.
-.. cfunction:: PyObject* PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
+.. c:function:: PyObject* PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
Return a new read-only buffer object that reads from a specified location
in memory, with a specified size. The caller is responsible for ensuring
@@ -424,27 +424,27 @@ native, in-memory format.
*size* parameter; :exc:`ValueError` will be raised in that case.
.. versionchanged:: 2.5
- This function used an :ctype:`int` type for *size*. This might require
+ This function used an :c:type:`int` type for *size*. This might require
changes in your code for properly supporting 64-bit systems.
-.. cfunction:: PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
+.. c:function:: PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
- Similar to :cfunc:`PyBuffer_FromMemory`, but the returned buffer is
+ Similar to :c:func:`PyBuffer_FromMemory`, but the returned buffer is
writable.
.. versionchanged:: 2.5
- This function used an :ctype:`int` type for *size*. This might require
+ This function used an :c:type:`int` type for *size*. This might require
changes in your code for properly supporting 64-bit systems.
-.. cfunction:: PyObject* PyBuffer_New(Py_ssize_t size)
+.. c:function:: PyObject* PyBuffer_New(Py_ssize_t size)
Return a new writable buffer object that maintains its own memory buffer of
*size* bytes. :exc:`ValueError` is returned if *size* is not zero or
positive. Note that the memory buffer (as returned by
- :cfunc:`PyObject_AsWriteBuffer`) is not specifically aligned.
+ :c:func:`PyObject_AsWriteBuffer`) is not specifically aligned.
.. versionchanged:: 2.5
- This function used an :ctype:`int` type for *size*. This might require
+ This function used an :c:type:`int` type for *size*. This might require
changes in your code for properly supporting 64-bit systems.