[Python-Dev] Assertion in _PyManagedBuffer_FromObject() (original) (raw)

Stefan Krah stefan at bytereef.org
Fri Mar 2 12:53:11 CET 2012


Stefan Behnel <stefan_ml at behnel.de> wrote:

if (PyObjectGetBuffer(base, &mbuf->master, PyBUFFULLRO) < 0) {_ _/* mbuf->master.obj must be NULL. */ PyDECREF(mbuf); return NULL; }

/* Assume that master.obj is a new reference to base. */ assert(mbuf->master.obj == base);

I'm not saying that this is likely to happen, but I could imagine code that wants to use a different object for the cleanup than itself, possibly for keeping a certain kind of state when it delivers more than one buffer, or for remembering what kind of allocation was used, or ...

I /think/ a different cleanup object would be possible, but memoryview now has the m.obj attribute that let's you see easily which object the view actually references. That attribute would then point to the cleanup handler.

Note that the complexity is such that I would have to go through the whole code again to be sure that it's possible.

So I'd rather see that people just don't use such schemes (unless there is a storm of protest).

The assumption is clearly documented in:

http://docs.python.org/dev/c-api/buffer.html#Py_buffer http://docs.python.org/dev/c-api/typeobj.html#buffer-object-structures

Since the Py_buffer.obj filed was undocumented in 3.2, I think we're within out rights to restrict the field to the exporter.

Stefan Krah



More information about the Python-Dev mailing list