Skip to content
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

Closed
stukowski opened this issue Nov 4, 2016 · 8 comments
Closed

Marking a pybind11::array as read-only #481

stukowski opened this issue Nov 4, 2016 · 8 comments

Comments

@stukowski
Copy link
Contributor

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:

const double* data = ...
size_t num_elements = ...

py::array_t<double> array({ num_elements }, data);

reinterpret_cast<py::detail::PyArray_Proxy*>(array.ptr())->flags &= ~py::detail::npy_api::NPY_ARRAY_WRITEABLE_;

My suggestion is to add a method to the pybind11::array class that allows modifying the array flags (either all or specifically the writable 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.

@aldanor
Copy link
Member

aldanor commented Nov 4, 2016

IIRC when constructing an array, the NPY_ARRAY_WRITEABLE flag is a writeability requirement; so, if this flag is missing and the input array is writeable, it won't make it non-writeable. (we could change the semantics of that, I guess).

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.

@aldanor
Copy link
Member

aldanor commented Nov 22, 2016

@wjakob I was thinking, for this to work properly (i.e. to roundtrip buffer -> array -> buffer), it would make sense to add a readonly (or writeable) flag to buffer_info.

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 writeable set to false by default.

@wjakob
Copy link
Member

wjakob commented Nov 23, 2016

@aldanor This sounds good to me -- feel free to add such a flag.

@chaoran
Copy link

chaoran commented Apr 28, 2019

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.

@chaoran
Copy link

chaoran commented Apr 28, 2019

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.

@jiapei100
Copy link

@stukowski
Hey, did you find a solution? I'm having the same issue, please take a look here: #3181

@stukowski
Copy link
Contributor Author

@stukowski
Hey, did you find a solution? I'm having the same issue, please take a look here: #3181

I am still using the workaround I described in my initial question to modify the flags of the PyArray_Proxy structure and clear the NPY_ARRAY_WRITEABLE bit. But I am not sure, maybe PR #1466 enables a better way to do this nowadays.

@jiapei100
Copy link

@stukowski

Great... Thank you so much... This problem is NOW resolved. Thanks.
However, I've got some new issues. Can you please help to take a look at fwilliams/numpyeigen#54 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants