What does “choice must be consistent for all consumers”?

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?

+7
c python
source share
1 answer

An important part:

Otherwise, the exporter can provide either read-only or writable buffers, but the selection must be consistent

Choice .. this choice, because the first case is not a choice ... must be consistent so that you do for each consumer, not a thing.

If set, you must provide a rewritable buffer. There is no choice, because the technical inability to do this for some reason is not a choice. If it is not installed, it is still writable, but then it should be writable to everyone who does not have the flag set.

+1
source share

All Articles