You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pybind11 provides a reasonable interface to the buffer protocol using the buffer_protocol tag and def_buffer. However, it does not, AFAICT, provide a way to lock a mutable object for the duration of the buffer's existence.
For example, if you use class from the docs, and add a mutating method resize:
In the Python standard library, they simply increment a counter in bf_getbuffer and decrement the counter in bf_releasebuffer.
However, since def_buffer returns static data in the buffer_info struct, and there is no (AFAICT) analogue to bf_releasebuffer, I don't see how one can do the same in pybind11.
I would suggest that pybind11 handles this buffer counting itself, and then add a tag for functions that might modify the buffer structure. So for the above example, if you did something like:
py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
.def(py::init<size_t, size_t>())
.def_buffer(....)
.def("resize", &Matrix::resize, py::lock_buffer()) # Name to be determined.
then pybind11 could check on calls to Matrix.resize that the buffer count is 0 and if not, raise an error like array.extend does.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
pybind11 provides a reasonable interface to the buffer protocol using the
buffer_protocol
tag anddef_buffer
. However, it does not, AFAICT, provide a way to lock a mutable object for the duration of the buffer's existence.For example, if you use class from the docs, and add a mutating method
resize
:then if you create a buffer and resize it:
the
view
will now be pointing to invalid memory:In the standard library, the closest analogue is
array
, and if you try to modify it, it will fail instead:In the Python standard library, they simply increment a counter in
bf_getbuffer
and decrement the counter inbf_releasebuffer
.However, since
def_buffer
returns static data in thebuffer_info
struct, and there is no (AFAICT) analogue tobf_releasebuffer
, I don't see how one can do the same in pybind11.I would suggest that pybind11 handles this buffer counting itself, and then add a tag for functions that might modify the buffer structure. So for the above example, if you did something like:
then pybind11 could check on calls to
Matrix.resize
that the buffer count is 0 and if not, raise an error likearray.extend
does.Beta Was this translation helpful? Give feedback.
All reactions