Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "the choice must be consistent for all consumers" mean?

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:

  1. All consumers not passing the flag should receive the same thing. It is legal to give these consumers read-only buffers and give consumers who do pass the flag writable buffers.
  2. All consumers should receive the same thing regardless of whether they pass the flag. If it is possible to return a writable buffer, then a writable buffer must be returned in every case. The sole purpose of the flag is to throw an error if a writable buffer cannot be provided.

Which interpretation is correct?

like image 784
Kevin Avatar asked Sep 12 '15 02:09

Kevin


1 Answers

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.

like image 172
John Hammond Avatar answered Oct 19 '22 22:10

John Hammond