[Cython] Problem with Py_buffer struct definition in Builtin.py

Stefan Behnel stefan_ml at behnel.de
Fri Mar 2 11:09:14 CET 2012

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

"__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. */
        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?


More information about the cython-devel mailing list