[Python-3000] PEP Draft: Enhancing the buffer protcol

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Feb 28 05:20:07 CET 2007

Travis E. Oliphant wrote:

>     typedef char *(*formatbufferproc)(PyObject *view, int *itemsize)
>       Get the format-string of the memory using the struct-module
>       string syntax

I'm not sure whether a struct-format string would be
the most convenient form for use by C-level code, as
it could require some tedious parsing to extract
useful information from it.

>     typedef PyObject *(*shapebufferproc)(PyObject *view)
>       Return a 2-tuple of lists containing shape information: (shape,
>       strides).

I'm also not sure about using Python data structures
to represent this, as it will force C-level code to
use Python API calls to pull it apart. What would be
wrong with C array of structs containing two integers

The buffer API is for the use of C code, and it should
be designed with the convenience of C code in mind.
Using Python data structures unnecessarily seems like
the wrong way to go about that.

The following alternative would seem to provide most of
the things that Travis's proposal does without involving
Python objects:

     struct pybuffer_shape {
        Py_ssize_t length;
        Py_ssize_t stride;

     typedef int (*getbufferproc)(PyObject *obj,
        void **buf, Py_ssize_t *len,
        char **format,
        struct pybuffer_shape **shape, int *ndim);

        /* Any of buf, format and shape may be NULL if you're
           not interested in them. */

     typedef int (*releasebufferproc)(PyObject *obj);


More information about the Python-3000 mailing list