I am implementing a Python C extension and I want my custom objects to support the buffer protocol. The buffer protocol essentially allows container objects to expose raw pointers to their memory in a controlled and well-defined fashion. The consumer passes a number of flags indicating what kind of memory it is prepared to deal with, and the exporter returns a struct describing the memory.
I'm particularly interested in the PyBUF_WRITABLE
flag:
PyBUF_WRITABLE
Controls the readonly field. If set, the exporter MUST provide a writable buffer or else report failure. Otherwise, the exporter MAY provide either a read-only or writable buffer, but the choice MUST be consistent for all consumers.
My objects are observable, but this naturally conflicts with handing out writable pointers into raw memory, so if I have any active observers, I can only hand out read-only buffers, and if I have any active writable buffers, I can't register any observers.
I would like to hand out read-only buffers by default, and provide writable buffers only when asked, but I'm not certain this is legal. I can see two possible interpretations of this sentence:
Which interpretation is correct?
The important part is:
Otherwise, the exporter may provide either a read-only or writable buffer, but the choice must be consistent
The choice..this choice, because the first case is no choice...must be consistent you make for every consumer, not the thing.
If the field is set, you must provide a writable buffer. There is no choice, because the technical inability to do so for whatever reason is no choice. If it is not set, it can still be writable, but then it must be writable for all who don't have the flag set.
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