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

Add method to clear WRITEABLE flag from PyArray #462

Merged
merged 2 commits into from
Oct 27, 2024

Conversation

jakelishman
Copy link
Contributor

The PyReadwriteArray understands the flag during the dynamic borrow checking, this just adds a way to set it on an array as well. This is a safe subset of what can be achieved unsafely by mutating the flags field on some *mut PyArray.

The discussion in #456 suggested having the method return a PyReadonlyArray. I feel like I might be missing something very obvious here, but I couldn't spot an existing way to consume an exclusive reference into a shared one. I added a From impl and then used that for now, but happy to switch it to a better version if one already exists, or drop it if we decide we don't want users to always pay the cost of re-acquiring the shared borrow (maybe there's some mem::forget shenanigans in conjunction with an extra "switch in place" operation that could be added to Shared to reduce the cost).

The dynamic borrow checker for `PyReadwriteArray` understood the flag,
but the crate offered no public way to set it.  General manipulation of
the `flags` field is unsafe (things like `OWNDATA` should never be
modified by hand), but flipping the `WRITEABLE` flag to `false` is safe.
The ordering of drops is important for the dynamic checker, so this is a
convenience for conversion.
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks and sorry for the slow review!

@davidhewitt davidhewitt merged commit 327ab10 into PyO3:main Oct 27, 2024
51 checks passed
@jakelishman jakelishman deleted the nonwriteable branch October 27, 2024 07:08
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

Successfully merging this pull request may close these issues.

2 participants