-
-
Notifications
You must be signed in to change notification settings - Fork 199
Change writeable flag after moving array from C++ #2629
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
Conversation
See pybind/pybind11#1466. |
I cannot get that to work with a |
@michalhabera suggested the new approach, which is influenced by the C++ assign flag comment |
This should be wrapped up in a helper function, otherwise we'll have low-level code everywhere. |
@jorgensd I think Garth wrote wrappers/array.h for this purpose. My suggested approach might be still flawed wrt. lifetime management, |
How about: py::array_t<T> array(self.values().size(), self.values().data(),
py::cast(self));
reinterpret_cast<py::detail::PyArray_Proxy*>(array.ptr())->flags
&= ~py::detail::npy_api::NPY_ARRAY_WRITEABLE_;
return array; I guess we could add this to the |
I think there is a mis-understanding of
|
Not if you look at the C++ signature, which returns a const span https://github.com/FEniCS/dolfinx/blob/main/cpp/dolfinx/mesh/MeshTags.h#L98 |
For the Python doesn't generally have the concept of |
The values are const span: https://github.com/FEniCS/dolfinx/blob/main/cpp/dolfinx/mesh/MeshTags.h#L101 |
Indeed, my mistake. Still, I don't think it's unreasonable to be able change the values from Python, even if it risks breaking something else. |
Im in favor of avoiding alot of unexpected behavior with the interaction with dolfinx::fem::Form, which has major assumptions on the const-ness of integration entities specified in MeshTags. As I mentioned in an earlier commit, we could also force this purely on the Python side (inside the property functions). |
This should really be fixed at the pybind11 level. Having said that, I fall on the side that the numpy array should be set as non-writeable, reflecting the underlying const specification in the C++ span. I don't have a strong view on whether the flag should be set at the C++/pybind11 level, or in the pure Python wrappers. |
I agree with that, and I am happy with setting it in the pybinding, as done in this PR currently. |
I'm not too fussed either way. In any case, a helper function should be added in Some lifetime/reference counting tests are need too since we're not 100% sure what is happing behind the scenes with pybind11 and what this PR does. |
Note: |
This will be addressed when we switch to nanobind, see #2820. |
Resolves #2625.
Not the prettiest of fixes, but I cannot see a nice workaround for this.