Closed
Description
The docs say:
Some formats require a read-only bytes-like object, and set a pointer instead of a buffer structure. They work by checking that the object’s PyBufferProcs.bf_releasebuffer field is NULL, which disallows mutable objects such as bytearray.
This can easily be understood that bf_releasebuffer
is directly linked to (im)mutability.
In reality this check is there to ensure whether the buffer can be safely "borrowed". For simple buffer exporters (like most of the stdlib ones), this correlates with immutability -- but not always, e.g.:
>>> import unicodedata
>>> unicodedata.lookup(b'SNAKE') # `lookup` happens to use the s# format internally
'🐍'
>>> view = memoryview(b'SNAKE')
>>> view.readonly
True
>>> unicodedata.lookup(view)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: lookup() argument must be read-only bytes-like object, not memoryview
The docs should explain the semantics precisely, without overwhelming the user that just wants to take str
/bytes
.