[Python-checkins] r54996 - peps/trunk/pep-3118.txt
georg.brandl
python-checkins at python.org
Fri Apr 27 08:58:35 CEST 2007
Author: georg.brandl
Date: Fri Apr 27 08:58:33 2007
New Revision: 54996
Modified:
peps/trunk/pep-3118.txt
Log:
Some reST sanitizing; change the Authors: header back to "Author:".
Modified: peps/trunk/pep-3118.txt
==============================================================================
--- peps/trunk/pep-3118.txt (original)
+++ peps/trunk/pep-3118.txt Fri Apr 27 08:58:33 2007
@@ -2,7 +2,7 @@
Title: Revising the buffer protocol
Version: $Revision$
Last-Modified: $Date$
-Authors: Travis Oliphant <oliphant at ee.byu.edu>, Carl Banks <pythondev at aerojockey.com>
+Author: Travis Oliphant <oliphant at ee.byu.edu>, Carl Banks <pythondev at aerojockey.com>
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
@@ -12,7 +12,7 @@
Abstract
========
-This PEP proposes re-designing the buffer interface (PyBufferProcs
+This PEP proposes re-designing the buffer interface (``PyBufferProcs``
function pointers) to improve the way Python allows memory sharing in
Python 3.0
@@ -87,7 +87,7 @@
libraries and because it is so simple it makes sense to support
memory sharing using this model. The PIL memory model is sometimes
used in C-code where a 2-d array can be then accessed using double
- pointer indirection: e.g. image[i][j].
+ pointer indirection: e.g. ``image[i][j]``.
The buffer interface should allow the object to export either of these
memory models. Consumers are free to either require contiguous memory
@@ -139,26 +139,23 @@
to use the provided C-API to obtain a chunk of memory.
-Change the PyBufferProcs structure to
-
-::
+Change the ``PyBufferProcs`` structure to ::
typedef struct {
getbufferproc bf_getbuffer;
releasebufferproc bf_releasebuffer;
- }
-
+ } PyBufferProcs;
::
typedef int (*getbufferproc)(PyObject *obj, PyBuffer *view, int flags)
-This function returns 0 on success and -1 on failure (and raises an
+This function returns ``0`` on success and ``-1`` on failure (and raises an
error). The first variable is the "exporting" object. The second
-argument is the address to a bufferinfo structure. If view is NULL,
+argument is the address to a bufferinfo structure. If view is ``NULL``,
then no information is returned but a lock on the memory is still
obtained. In this case, the corresponding releasebuffer should also
-be called with NULL.
+be called with ``NULL``.
The third argument indicates what kind of buffer the consumer is
prepared to deal with and therefore what kind of buffer the exporter
@@ -170,7 +167,7 @@
In addition, 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
-PyErr_BufferError unless there is another error that is actually
+``PyErr_BufferError`` unless there is another error that is actually
causing the problem. The exporter can use flags information to
simplify how much of the PyBuffer structure is filled in with
non-default values and/or raise an error if the object can't support a
@@ -183,10 +180,10 @@
segment, while others indicate to the exporter what kind of
information the consumer can deal with. If certain information is not
asked for by the consumer, but the exporter cannot share its memory
-without that information, then a PyErr_BufferError should be raised.
+without that information, then a ``PyErr_BufferError`` should be raised.
-PyBUF_SIMPLE
+``PyBUF_SIMPLE``
This is the default flag state (0). The returned buffer may or may
not have writeable memory. The format will be assumed to be
@@ -194,12 +191,12 @@
needs to be \|'d to the others. The exporter will raise an error if
it cannot provide such a contiguous buffer of bytes.
-PyBUF_REQ_WRITEABLE
+``PyBUF_REQ_WRITEABLE``
The returned buffer must be writeable. If it is not writeable,
then raise an error.
-PyBUF_REQ_LOCKDATA
+``PyBUF_REQ_LOCKDATA``
The returned buffer must be readonly. If the object is already
read-only or it can make its memory read-only (and there are no
@@ -207,28 +204,28 @@
buffer information. If the object does not have read-only memory
(or cannot make it read-only), then an error should be raised.
-PyBUF_REQ_FORMAT
+``PyBUF_REQ_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")
+ as ``NULL`` (which means "B")
-PyBUF_REQ_ALIGNED (implies PyBUF_REQ_FORMAT)
+``PyBUF_REQ_ALIGNED`` (implies ``PyBUF_REQ_FORMAT``)
The returned buffer must have all (primitive data-type entries)
aligned as the compiler would align them
-PyBUF_ALW_ND
+``PyBUF_ALW_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.
-PyBUF_ALW_STRIDES (implies PyBUF_ALW_ND)
+``PyBUF_ALW_STRIDES`` (implies ``PyBUF_ALW_ND``)
The returned buffer must provide strides information. This would be
used when the consumer can handle strided, discontiguous arrays.
@@ -236,9 +233,9 @@
The exporter may raise an error if cannot provide a strided-only
representation of the data (i.e. without the suboffsets).
-PyBUF_REQ_C_CONTIGUOUS
-PyBUF_REQ_F_CONTIGUOUS
-PyBUF_REQ_ANY_CONTIGUOUS
+| ``PyBUF_REQ_C_CONTIGUOUS``
+| ``PyBUF_REQ_F_CONTIGUOUS``
+| ``PyBUF_REQ_ANY_CONTIGUOUS``
These flags indicate that the returned buffer must be respectively,
C-contiguous (last dimension varies the fastest), Fortran
@@ -247,7 +244,7 @@
strides buffer info structure will be filled in correctly.
-PyBUF_ALW_INDIRECT (implies PyBUF_ALW_STRIDES)
+``PyBUF_ALW_INDIRECT`` (implies ``PyBUF_ALW_STRIDES``)
The returned buffer must have suboffsets information. This would
be used when the consumer can handle indirect array referencing
@@ -258,34 +255,34 @@
Multi-dimensional (but contiguous)
- PyBUF_CONTIG (PyBUF_ALW_ND | PyBUF_REQ_WRITEABLE | PyBUF_REQ_ALIGNED)
- PyBUF_CONTIG_RO (PyBUF_ALW_ND | PyBUF_REQ_ALIGNED)
- PyBUF_CONTIG_LCK (PyBUF_ALW_ND | PyBUF_REQ_LOCKDATA | PyBUF_REQ_ALIGNED)
+ | ``PyBUF_CONTIG`` (``PyBUF_ALW_ND | PyBUF_REQ_WRITEABLE | PyBUF_REQ_ALIGNED``)
+ | ``PyBUF_CONTIG_RO`` (``PyBUF_ALW_ND | PyBUF_REQ_ALIGNED``)
+ | ``PyBUF_CONTIG_LCK`` (``PyBUF_ALW_ND | PyBUF_REQ_LOCKDATA | PyBUF_REQ_ALIGNED``)
Multi-dimensional using strides but aligned
- PyBUF_STRIDED (PyBUF_ALW_STRIDES | PyBUF_REQ_WRITEABLE | PyBUF_REQ_ALIGNED)
- PyBUF_STRIDED_RO (PyBUF_ALW_STRIDES | PyBUF_REQ_ALIGNED)
- PyBUF_STRIDED_LCK (PyBUF_ALW_STRIDES | PyBUF_REQ_LOCKDATA | PyBUF_REQ_ALIGNED)
+ | ``PyBUF_STRIDED`` (``PyBUF_ALW_STRIDES | PyBUF_REQ_WRITEABLE | PyBUF_REQ_ALIGNED``)
+ | ``PyBUF_STRIDED_RO`` (``PyBUF_ALW_STRIDES | PyBUF_REQ_ALIGNED``)
+ | ``PyBUF_STRIDED_LCK`` (``PyBUF_ALW_STRIDES | PyBUF_REQ_LOCKDATA | PyBUF_REQ_ALIGNED``)
Multi-dimensional using strides and not necessarily aligned
- PyBUF_RECORDS (PyBUF_ALW_STRIDES | PyBUF_REQ_WRITEABLE | PyBUF_REQ_FORMAT)
- PyBUF_RECORDS_RO (PyBUF_ALW_STRIDES | PyBUF_REQ_FORMAT)
- PyBUF_RECORDS_LCK (PyBUF_ALW_STRIDES | PyBUF_REQ_LOCKDATA | PyBUF_REQ_FORMAT)
+ | ``PyBUF_RECORDS`` (``PyBUF_ALW_STRIDES | PyBUF_REQ_WRITEABLE | PyBUF_REQ_FORMAT``)
+ | ``PyBUF_RECORDS_RO`` (``PyBUF_ALW_STRIDES | PyBUF_REQ_FORMAT``)
+ | ``PyBUF_RECORDS_LCK`` (``PyBUF_ALW_STRIDES | PyBUF_REQ_LOCKDATA | PyBUF_REQ_FORMAT``)
Multi-dimensional using sub-offsets
- PyBUF_FULL (PyBUF_ALW_INDIRECT | PyBUF_REQ_WRITEABLE | PyBUF_REQ_FORMAT)
- PyBUF_FULL_RO (PyBUF_ALW_INDIRECT | PyBUF_REQ_FORMAT)
- PyBUF_FULL_LCK (PyBUF_ALW_INDIRECT | PyBUF_REQ_LOCKDATA | PyBUF_REQ_FORMAT)
+ | ``PyBUF_FULL`` (``PyBUF_ALW_INDIRECT | PyBUF_REQ_WRITEABLE | PyBUF_REQ_FORMAT``)
+ | ``PyBUF_FULL_RO`` (``PyBUF_ALW_INDIRECT | PyBUF_REQ_FORMAT``)
+ | ``PyBUF_FULL_LCK`` (``PyBUF_ALW_INDIRECT | PyBUF_REQ_LOCKDATA | PyBUF_REQ_FORMAT``)
Thus, the consumer simply wanting a contiguous chunk of bytes from
-the object would use PyBUF_SIMPLE, while a consumer that understands
-how to make use of the most complicated cases could use PyBUF_FULL
+the object would use ``PyBUF_SIMPLE``, while a consumer that understands
+how to make use of the most complicated cases could use ``PyBUF_FULL``.
The format information is only guaranteed to be non-NULL if
-PyBUF_REQ_FORMAT is in the flag argument, otherwise it is expected the
+``PyBUF_REQ_FORMAT`` is in the flag argument, otherwise it is expected the
consumer will assume unsigned bytes.
There is a C-API that simple exporting objects can use to fill-in the
@@ -319,15 +316,15 @@
The members of the bufferinfo structure are:
-buf
+``buf``
a pointer to the start of the memory for the object
-len
+``len``
the total bytes of memory the object uses. This should be the
same as the product of the shape array multiplied by the number of
bytes per item of memory.
-readonly
+``readonly``
an integer variable to hold whether or not the memory is readonly.
1 means the memory is readonly, zero means the memory is
writeable, and -1 means the memory was "locked" (changed from
@@ -335,33 +332,33 @@
and therefore should be unlocked when this PyBuffer structure is
"released" (this is supported only by some exporters).
-format
+``format``
a NULL-terminated format-string (following the struct-style syntax
including extensions) indicating what is in each element of
memory. The number of elements is len / itemsize, where itemsize
is the number of bytes implied by the format. This can be NULL which
implies standard unsigned bytes ("B").
-ndims
+``ndims``
a variable storing the number of dimensions the memory represents.
Must be >=0. A value of 0 means that shape and strides and suboffsets
- must be NULL (i.e. the memory represents a scalar).
+ must be ``NULL`` (i.e. the memory represents a scalar).
-shape
+``shape``
an array of ``Py_ssize_t`` of length ``ndims`` indicating the
shape of the memory as an N-D array. Note that ``((*shape)[0] *
... * (*shape)[ndims-1])*itemsize = len``. If ndims is 0 (indicating
- a scalar), then this must be NULL.
+ a scalar), then this must be ``NULL``.
-strides
+``strides``
address of a ``Py_ssize_t*`` variable that will be filled with a
- pointer to an array of ``Py_ssize_t`` of length ``ndims`` (or NULL
- if ndims is 0). indicating the number of bytes to skip to get to
+ pointer to an array of ``Py_ssize_t`` of length ``ndims`` (or ``NULL``
+ if ``ndims`` is 0). indicating the number of bytes to skip to get to
the next element in each dimension. If this is not requested by
- the caller (PyBUF_ALW_STRIDES is not set), then this should be set
+ the caller (``PyBUF_ALW_STRIDES`` is not set), then this should be set
to NULL which indicates a C-style contiguous array.
-suboffsets
+``suboffsets``
address of a ``Py_ssize_t *`` variable that will be filled with a
pointer to an array of ``Py_ssize_t`` of length ``*ndims``. If
these suboffset numbers are >=0, then the value stored along the
@@ -374,14 +371,14 @@
For clarity, here is a function that returns a pointer to the
element in an N-D array pointed to by an N-dimesional index when
- there are both non-NULL strides and suboffsets.::
+ there are both non-NULL strides 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;
+ 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];
+ pointer += strides[i] * indices[i];
if (suboffsets[i] >=0 ) {
pointer = *((char**)pointer) + suboffsets[i];
}
@@ -395,15 +392,15 @@
the location of the starting pointer directly (i.e. buf would
be modified).
-itemsize
+``itemsize``
This is a storage for the itemsize of each element of the shared
memory. It is strictly un-necessary as it can be obtained using
- PyBuffer_SizeFromFormat, however an exporter may know this
+ ``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.
-internal
+``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 about whether or not the shape, strides, and suboffsets
@@ -468,7 +465,9 @@
This is a C-API version of the getbuffer function call. It checks to
make sure object has the required function pointer and issues the
call. Returns -1 and raises an error on failure and returns 0 on
-success.::
+success.
+
+::
int PyObject_ReleaseBuffer(PyObject *obj, PyBuffer *view)
@@ -486,9 +485,7 @@
A memory-view object is an extended buffer object that could replace
the buffer object (but doesn't have to as that could be kept as a
-simple 1-d memory-view object). It's C-structure is
-
-::
+simple 1-d memory-view object). Its C-structure is ::
typedef struct {
PyObject_HEAD
@@ -521,29 +518,29 @@
The Python name will be
-__builtin__.memory
+``__builtin__.memory``
Methods:
-| lock
-| unlock
-| __getitem__ (will support multi-dimensional slicing)
-| __setitem__ (will support multi-dimensional slicing)
-| tobytes (obtain a bytes-object of everything in the memory).
-| tolist (obtain a "nested" list of the memory. Everything
- is interpreted into standard Python objects
- as the struct module unpack would do).
-
-Attributes (taken from the memory of the base object)
-
- format
- itemsize
- shape
- strides
- suboffsets
- size
- readonly
- ndim
+| ``lock``
+| ``unlock``
+| ``__getitem__`` (will support multi-dimensional slicing)
+| ``__setitem__`` (will support multi-dimensional slicing)
+| ``tobytes`` (obtain a bytes-object of everything in the memory).
+| ``tolist`` (obtain a "nested" list of the memory. Everything
+ is interpreted into standard Python objects
+ as the struct module unpack would do).
+
+Attributes (taken from the memory of the base object):
+
+* ``format``
+* ``itemsize``
+* ``shape``
+* ``strides``
+* ``suboffsets``
+* ``size``
+* ``readonly``
+* ``ndim``
::
@@ -594,13 +591,15 @@
::
- int PyBuffer_IsContiguous(PyBuffer *view, char fortran);
+ int PyBuffer_IsContiguous(PyBuffer *view, char fortran)
Return 1 if the memory defined by the view object is C-style (fortran
= 'C') or Fortran-style (fortran = 'F') contiguous or either one
-(fortran = 'A'). Return 0 otherwise.::
+(fortran = 'A'). Return 0 otherwise.
+
+::
- int PyBuffer_IsAligned(PyBuffer *view);
+ int PyBuffer_IsAligned(PyBuffer *view)
Return 1 if the memory at all elements of the array implied by the
view object is aligned
@@ -613,7 +612,9 @@
Fill the strides array with byte-strides of a contiguous (C-style if
fortran is 0 or Fortran-style if fortran is 1) array of the given
-shape with the given number of bytes per element.::
+shape with the given number of bytes per element.
+
+::
int PyBuffer_FillInfo(PyBuffer *view, void *buf,
Py_ssize_t len, int readonly, int infoflags)
@@ -623,7 +624,8 @@
given length. Returns 0 on success and -1 (with raising an error) on
error.
-::
+::
+
PyErr_BufferError
A new error object for returning buffer errors which arise because an
@@ -638,7 +640,7 @@
The struct string-syntax is missing some characters to fully
implement data-format descriptions already available elsewhere (in
ctypes and NumPy for example). The Python 2.5 specification is
-at http://docs.python.org/lib/module-struct.html
+at http://docs.python.org/lib/module-struct.html.
Here are the proposed additions:
@@ -682,11 +684,11 @@
previously-specified endian string is in force until changed. The
default endian is '@' which means native data-types and alignment. If
un-aligned, native data-types are requested, then the endian
-specification is '^'.
+specification is '^'.
According to the struct-module, a number can preceed a character
code to specify how many of that type there are. The
-(k1,k2,...,kn) extension also allows specifying if the data is
+``(k1,k2,...,kn)`` extension also allows specifying if the data is
supposed to be viewed as a (C-style contiguous, last-dimension
varies the fastest) multi-dimensional array of a particular format.
@@ -702,15 +704,15 @@
<named> is the constructor for a named-tuple (not-specified yet).
float
- 'f' <--> Python float
+ ``'f'`` <--> Python float
complex double
- 'Zd' <--> Python complex
+ ``'Zd'`` <--> Python complex
RGB Pixel data
- 'BBB' <--> (int, int, int)
- 'B:r: B:g: B:b:' <--> <named>((int, int, int), ('r','g','b'))
+ ``'BBB'`` <--> (int, int, int)
+ ``'B:r: B:g: B:b:'`` <--> <named>((int, int, int), ('r','g','b'))
Mixed endian (weird but possible)
- '>i:big: <i:little:' <--> <named>((int, int), ('big', 'little'))
+ ``'>i:big: <i:little:'`` <--> <named>((int, int), ('big', 'little'))
Nested structure
::
@@ -802,8 +804,9 @@
call more difficult. The flags variable allows the same
ability of consumers to be "simple" in how they call the protocol.
+
Code
-========
+====
The authors of the PEP promise to contribute and maintain the code for
this proposal but will welcome any help.
@@ -812,7 +815,7 @@
Examples
-=========
+========
Ex. 1
-----------
@@ -834,8 +837,8 @@
Py_ssize_t view_count;
};
-"lines" points to malloced 1-D array of (struct rgba*). Each pointer
-in THAT block points to a seperately malloced array of (struct rgba).
+"lines" points to malloced 1-D array of ``(struct rgba*)``. Each pointer
+in THAT block points to a seperately malloced array of ``(struct rgba)``.
In order to access, say, the red value of the pixel at x=30, y=50, you'd use "lines[50][30].r".
@@ -957,5 +960,5 @@
Copyright
=========
-This PEP is placed in the public domain
+This PEP is placed in the public domain.
More information about the Python-checkins
mailing list