I am implementing the Python C extension and I want my custom objects to support the buffer protocol . The buffer protocol essentially allows containers to map raw pointers to their memory under control and in a well-defined way. The consumer passes several flags indicating which memory it is ready to process, and the exporter returns a structure that describes the memory.
I am particularly interested in the PyBUF_WRITABLE flag:
PyBUF_WRITABLE
Controls the readonly field. If set, the exporter MUST provide a rewritable buffer or report an error. Otherwise, the exporter MAY provide either a read-only or writable buffer, but the selection MUST be consistent for all consumers.
My objects are observable , but this naturally conflicts with the distribution of writable pointers to raw memory, so if I have active watchers, I can only distribute read-only buffers, and if I have active writable buffers, I cannot register which any observers.
I would like to send read-only buffers by default and provide rewritable buffers only on request, but I'm not sure if this is legal. I see two possible interpretations of this sentence:
- All non-flag consumers must receive the same. It is legal to provide these consumers with read-only buffers and to give consumers that send buffers available for flag writing.
- All consumers should receive the same thing, regardless of whether they skip the flag. If a writable buffer can be returned, then a rewritable buffer must be returned in each case. The sole purpose of the flag is to throw an error if it is not possible to create a writeable buffer.
Which interpretation is correct?
Kevin
source share