feature request: version of from_buffer that verifies the buffer is writable
Created originally on Bitbucket by njs (Nathaniel Smith)
I'm making readinto
-style function, using cffi. So my Python function takes any writable buffer-object, and then passes it to a C function, which writes into the buffer, like:
def my_readinto(data):
buf = ffi.from_buffer(data)
return somelib.read(buf, len(data))
But of course this is buggy, because from_buffer
will happily except a readonly bytes
object, and write to it. I could add some awkward hacks in the Python code to validate that data
is a writable buffer, but this is actually somewhat non-trivial, because the only fully reliable way to do that is to call PyObject_GetContiguousBuffer
and then check the Py_buffer.readonly
field.
Give that from_buffer
is already doing the tricky parts here, I wish I could do:
buf = ffi.from_buffer(data, require_writable=True)
and then CFFI would check the buffer's readonly
flag and raise an error if it was set.