[issue9602] PyObject_AsCharBuffer() should only accept read-only objects

Marc-Andre Lemburg report at bugs.python.org
Tue Aug 17 17:43:00 CEST 2010


Marc-Andre Lemburg <mal at egenix.com> added the comment:

STINNER Victor wrote:
> 
> STINNER Victor <victor.stinner at haypocalc.com> added the comment:
> 
>> Note that the buffer interface release API is meant to protect
>> against such modifications, so I don't see why rejecting objects
>> that do implement this API should be rejected.
> 
> As I explained, the release API is *not* used by PyObject_AsCharBuffer() in Python 2.7 and 3.2.
> 
> Pseudo-code example:
> ---
> PyObject_AsCharBuffer(obj, &str, &size)
> ... modify or destroy obj ...
> str is no more valid here
> ---

Right, but as I explained before: this doesn't really happen in
practice, otherwise we would have had issues with these APIs
long before the Py_buffers were introduced.

Note that the same comment applies to PyObject_AsReadBuffer() and
PyObject_AsWriteBuffer().

For Python 2.7 you can't change anything anymore. For Python 3.2
you could start a deprecation process as outlined in PEP 4, if you
feel this really is such a big issue.

>> Restricting the API to read-only buffers would seriously limit
>> it's functionality. I'm -1 on doing that.
> 
> PyObject_AsCharBuffer() is dangerous because the caller has to ensure that the object is not modified or destroyed. Antoine proposes to deprecated PyObject_AsCharBuffer().
> 
> PyObject_GetBuffer() can replace PyObject_AsCharBuffer(): it's easy to get the pointer to the buffer content (view.buf) and the size (view.len) using PyObject_GetBuffer(), and it does protect the buffer against modification or destuction thanks to the release API (PyBuffer_Release). But PyObject_GetBuffer() is maybe a little bit to low level, eg. it doesn't check that the buffer is contiguous, and it requires a flag argument. A new function is maybe needed to replace PyObject_AsCharBuffer(). Eg. PyObject_GetCharBuffer() which will call PyObject_GetBuffer() (the caller will then have to call PyBuffer_Release() to release the buffer).
> 
> Example:
> ---
> PyObject_GetCharBuffer(obj, &view, &str, &size)
> ... use str and size ...
> PyBuffer_Release(view);
> ---
> or just
> ---
> PyObject_GetCharBuffer(obj, &view)
> ... use view.buf and view.len ...
> PyBuffer_Release(view);
> ---

Why add a new function ? Python 3.2 doesn't provide a way to access
a *character* buffer version of an object anymore. Not that this is
good, or shouldn't be readded at some point, but right now, we don't
have a need for such an API.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9602>
_______________________________________


More information about the Python-bugs-list mailing list