-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Marking a pybind11::array as read-only #481
Comments
IIRC when constructing an array, the Yes, probably makes sense to allow setting writeability, either through some setter or the ctor. I'll add this to the pending flags rework PR. |
@wjakob I was thinking, for this to work properly (i.e. to roundtrip Python buffer interface already supports it: https://docs.python.org/3/c-api/buffer.html#c.Py_buffer.readonly and I believe NumPy arrays would propagate this flag (needs checking). This way you could e.g. expose an STL vector (as proposed in #488) as a readonly-only buffer, and the corresponding numpy arrays would have |
@aldanor This sounds good to me -- feel free to add such a flag. |
It would be nice to have this feature. I was looking for a way to set an array non writable. I think array::ensure would do the trick but it doesn't. Also, it would be nice to set other flags on an array too, such as NPY_ARRAY_ALIGNED. |
In addition, I found out if I clear an array's writable flag in C++. Then return that array to python side. The array I got in python is still writable, meaning arr.flags.writable is True. |
@stukowski |
I am still using the workaround I described in my initial question to modify the flags of the |
Great... Thank you so much... This problem is NOW resolved. Thanks. |
The method
pybind11::array::writeable()
tells whether the array's data is mutable. It would be nice to have a corresponding method for marking the data in an array as read-only. Unless I overlooked something, it seems the only way to do this is with a hack like this:My suggestion is to add a method to the
pybind11::array
class that allows modifying the array flags (either all or specifically thewritable
flag).Background: I would like to implement a getter method binding that returns a NumPy array pointing to an internal C++ data buffer (without making a copy of the data). The Python side should not be able to modify the data, so I would like to mark the array as read-only.
The text was updated successfully, but these errors were encountered: