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.)
Buffer structures (or simply “buffers”) are useful as a way to expose the binary data from another object to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily.
The Python buffer protocol, also known in the community as PEP 3118, is a framework in which Python objects can expose raw byte arrays to other Python objects. This can be extremely useful for scientific computing, where we often use packages such as NumPy to efficiently store and manipulate large arrays of data.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With