Definition of PyBufferProcs in Python 2.7 when class implements PEP 3118

570 views Asked by At

I am in the process of extending the classes in our library (which supports Python 2.7) to support PEP 3118, which has been back-ported to 2.7.

From the documentation, I need to initialize the tp_as_buffer field to point to a PyBufferProcs. From the documentation for 2.7, however, the description of this structure only contains entries for the old buffer protocol. From the sources, I gather that PyBufferProcs has some additional entries for the new protocol (bf_getbuffer and bf_releasebuffer).

The questions remain:

  • Do I have to do something special to tell Python that these new entries are valid?

  • Do I have to fill in the entries for the old protocol? (The documentation for 2.7 says, for example, that bf_getsegcount may not be null. But this entry shouldn't be used if I'm supporting PEP 3118.)

1

There are 1 answers

1
Bakuriu On

You can just fill the last two fields of PyBufferProcs but you have to add the Py_TPFLAGS_HAVE_NEWBUFFER flag to the tp_flags of your types. This is the special thing that was introduced in python2 to make the new protocol available together with the old one.

I have no idea why this isn't documented anywhere, but you can see it used in the definition of the bytearray type for python 2.7 (see here):

    &bytearray_as_buffer,               /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
    Py_TPFLAGS_HAVE_NEWBUFFER,          /* tp_flags */

This content was already posted in comments, but it deserves an answer.