[Python-Dev] Allocation of shape and strides fields in Py_buffer

Nick Coghlan ncoghlan at gmail.com
Mon Dec 8 21:46:53 CET 2008

Antoine Pitrou wrote:
> For the one-dimensional case, I had in mind a simple scheme where the Py_buffer
> struct has an additional two-member Py_ssize_t array. Then `shape` and `strides`
> can point to the first and second member of this array, respectively. This
> wouldn't solve the multi-dimensional case, however.
> Thanks for any ideas on how to solve this.

Actually, I think your suggested scheme for the one-dimensional case
shows the way forward: ownership of the shape and strides memory belongs
to the object issuing the Py_buffer struct, and that object needs to
deal with it when the buffer is released. Defining a larger memory chunk
with the Py_buffer as the first item and the shape and stride info
tacked onto the end and returning that from PyObject_GetBuffer() means
that the shape/stride info will be released automatically when the view
is released via PyBuffer_Release().

For more complicated cases, the object providing the views may need to
do some internally bookkeeping to map from Py_buffer pointers to
separately allocated shape/stride information and release those when the
views are released.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list