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.)
bytearray_as_buffer
in 2.7, with the additional fields forbf_getbuffer
andbf_releasebuffer
. Also,PyByteArray_Type.tp_flags
setsPy_TPFLAGS_HAVE_NEWBUFFER
.PyObject_GetBuffer
uses thePyObject_CheckBuffer
macro to check that this flag is set. – Kummerbundbytearray
implementation, which doesn't use the Python 2 version ofPyBufferProcs
that has six fields, including the last two for the new buffer protocol. Using just the new protocol only requires those two fields. The Python 3 source also doesn't use thePy_TPFLAGS_HAVE_NEWBUFFER
flag; that's just in Python 2. Setting this flag is the "something special" that tellsPyObject_CheckBuffer
that the new entries are valid. – Kummerbund