[Python-Dev] PEP 3118: Extended buffer protocol (new version) (original) (raw)

Lisandro Dalcin dalcinl at gmail.com
Thu Apr 12 02:21:57 CEST 2007


On 4/9/07, Travis Oliphant <oliphant.travis at ieee.org> wrote:

Travis, all this is far better and simpler than previous approaches... Just a few comments

The bufferinfo structure is::

struct bufferinfo { void *buf; Pyssizet len; int readonly; char *format; int ndims; Pyssizet *shape; Pyssizet *strides; Pyssizet *suboffsets; void *internal; };

  1. I assume that 'bufferinfo' structure will be part of public Python API. In such a case, I think it should be renamed and prefixed. Something like 'PyBufferInfo' sounds good?

  2. I also think 'bufferinfo' could also have an 'itemsize' field filled if Py_BUF_ITEMSIZE flag is passed. What do you think? Exporters can possibly fill this field more efficiently than next parsing 'format' string, it can also save consumers from an API call.

  3. It does make sense to make 'format' be 'const char *' ?

  4. I am not sure about this, but perhaps 'buferingo' should save the flags passed to 'getbuffer' in a 'flags' field. This can be possibly needed at 'releasebuffer' call.

typedef struct { PyObjectHEAD PyObject *base; struct bufferinfo view; int itemsize; int flags; } PyMemoryViewObject;

  1. If my previous comments go in, so 'PyMemoryViewObject' will not need 'itemsize' and 'flags' fields (they are in 'bufferinfo' structure).

  2. Perhaps 'view' field can be renamed to 'info'.

int PyObjectSizeFromFormat(char *)

  1. Why not 'const char *' here?

int PyObjectGetContiguous(PyObject *obj, void **buf, Pyssizet *len, int fortran)

Return a contiguous chunk of memory representing the buffer. If a copy is made then return 1. If no copy was needed return 0.

  1. If a copy was made, What should consumers call to free memory?

If the object is multi-dimensional, then if fortran is 1, the first dimension of the underlying array will vary the fastest in the buffer. If fortran is 0, then the last dimension will vary the fastest (C-style contiguous). If fortran is -1, then it does not matter and you will get whatever the object decides is more efficient.

  1. What about using a char, like 'c' or 'C', and 'f' or 'F', and 0 or 'a' or 'A' (any) ?

int PyObjectCopyToObject(PyObject *obj, void *buf, Pyssizet len, int fortran)

  1. Better name? Perhaps PyObject_CopyBuffer or PyObject_CopyMemory?

int PyObjectSizeFromFormat(char *)

int PyObjectIsContiguous(struct bufferinfo *view, int fortran); void PyObjectFillContiguousStrides(int *ndims, Pyssizet *shape, int itemsize, Pyssizet *strides, int fortran) int PyObjectFillBufferInfo(struct bufferinfo *view, void *buf, Pyssizet len, int readonly, int infoflags)

  1. Perhaps the 'PyObject_' prefix is wrong, as those functions does not operate with Python objects.

Regards,

-- Lisandro Dalcín

Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594



More information about the Python-Dev mailing list