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

Josiah Carlson jcarlson at uci.edu
Wed Feb 28 03:48:03 CET 2007


"Daniel Stutzbach" <daniel at stutzbachenterprises.com> wrote:
[snip]
> The container cannot reallocate the memory unless the reference
> counter on the PyArray is exactly 1.

Alternatively, some objects could opt to create a new PyArray of
sufficient size, copy data as necessary, and leave all previous views to
point to the old data.  If done periodically, this could lead to an
interesting versioning mechanism (especially if we could teach Python to
virtualize itself and pull memory from a specific buffer), but that is
a different discussion for a different day :)


About the only issue I can see with implementing the mechanism as you
describe is that everything that wants to offer the buffer interface
would need to store its data in a PyArray structure.  Bytes, unicode,
array.array, mmap, etc.  Most of the difference will essentially be a
call to PyArray_New() rather than PyMalloc(), and an indirection via
macro of PyArray_ASSTRINGANDSIZE() to get the pointer and length of the
buffer. I would suspect that such overhead would be minimal, but without
implementing and testing it on something that is used often (maybe
Python 2.x strings as the simplest example?), it would be hard to say.

The benefit to implementing the interface as described by Travis is that
if an object is read-only (like unicode), the acquire/release is (as in
the PyArray version) an incref/decref, and no other structural changes
are necessary.

Then again, after switching to PyArrays, all views are more or less an
incref or decref and the allocation of a "view" object to describe
memory layout.


 - Josiah



More information about the Python-3000 mailing list