I have an existing PyMemoryViewObject
that I want to "release" to invalidate the memoryview
object.
I am able to call the release
function through the PyObject_CallMethod
API:
if (PyMemoryView_Check(obj)) {
PyObject_CallMethod(obj, "release", NULL);
}
This works fine, but I would like to use a more direct approach.
I've tried this:
if (PyMemoryView_Check(obj)) {
PyBuffer_Release(PyMemoryView_GET_BUFFER(obj));
}
However, this doesn't work, and I'm still able to read/write from the buffer in Python. In fact, the documentation for PyBuffer_Release
says:
/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
There's no C API call for that. You have to go through a method call. (This is the case for a lot of methods you might like to call from C.)
Don't be tempted to call PyBuffer_Release
on the memoryview's underlying Py_buffer
. The memoryview is responsible for doing that, and for keeping track of whether it has been done. If you try to release the buffer yourself, the memoryview will be in an inconsistent state, still thinking it owns a valid buffer, and things will break.