[Cython] Problem with Py_buffer struct definition in Builtin.py
mark florisson
markflorisson88 at gmail.com
Fri Mar 2 17:33:45 CET 2012
On 2 March 2012 10:09, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Stefan Behnel, 02.03.2012 10:45:
>> the builtin Py_buffer struct type is currently defined as follows:
>>
>> """
>> builtin_structs_table = [
>> ('Py_buffer', 'Py_buffer',
>> [("buf", PyrexTypes.c_void_ptr_type),
>> ("obj", PyrexTypes.py_object_type),
>> ("len", PyrexTypes.c_py_ssize_t_type),
>> ...
>> """
>>
>> I hadn't noticed this before, but when you actually use it in a
>> "__getbuffer__()" special method, you have to assign the buffer owner (i.e.
>> self) to the .obj field, which is currently defined as "object".
>
> Oh, well, I should really learn to read code before composing a lengthy
> e-mail...
>
> "__getbuffer__()" is already special-cased and sets the value to None. I
> think I even recall that we discussed this back when Dag implemented
> support for buffers. The reason I had originally noticed this was this
> recent change in Py3.3:
>
> """
> static PyObject *
> _PyManagedBuffer_FromObject(PyObject *base)
> {
> _PyManagedBufferObject *mbuf;
>
> mbuf = mbuf_alloc();
> if (mbuf == NULL)
> return NULL;
>
> if (PyObject_GetBuffer(base, &mbuf->master, PyBUF_FULL_RO) < 0) {
> /* mbuf->master.obj must be NULL. */
> Py_DECREF(mbuf);
> return NULL;
> }
>
> /* Assume that master.obj is a new reference to base. */
> assert(mbuf->master.obj == base);
>
> return (PyObject *)mbuf;
> }
> """
>
> Note the assertion (which is unreleased as of now, so it may still be
> subject to changes). Is there any reason the value should be set to None by
> Cython's special casing code instead of self?
It sets it to None to it can later reset it to NULL. Python 3.3 is the
first version to document the 'obj' Py_buffer attribute, and it
mentions that the exporter may set it to NULL. The code above is not
the PyObject_GetBuffer function, so setting it to NULL should still
work. Defaulting it to 'self' instead would work equally well I'd
think.
> Stefan
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel
More information about the cython-devel
mailing list