-
Notifications
You must be signed in to change notification settings - Fork 2
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
fix: cleanup internal nparray_to_*
methods
#80
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8acc8a7
fix: remove unsound `py_untyped_array_to_array_object`
LDeakin 9bd2bfe
fix: move C contiguous check into `nparray_to_*` methods
LDeakin d471112
chore: bring back `py_untyped_array_to_array_object`
LDeakin 97d87f3
fix: test_nparray_to_unsafe_cell_slice_empty after 9bd2bfe
LDeakin fde11a8
use consistent lifetimes in py_untyped_array_to_array_object and npar…
LDeakin 75be391
more safety docs in py_untyped_array_to_array_object
LDeakin 49b9e22
fix: use ptr::NonNull in py_untyped_array_to_array_object
LDeakin f812fde
Merge branch 'main' into ld/fix_78
flying-sheep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oof good catch. this is why unsafe is scary, this was extremely wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, is there a tl;dr for why this was wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I’ll rename the lifetimes for easier understanding.
First,
Bound
is a GIL binding, that means it’s a kind of reference1 that lives as long as a certain part of our code holds Python’s GIL (Global Interpreter Lock). This guarantees that nothing in Python land tries to mutate that object while we’re accessing it at the same time. So:&'x Bound<'py, PyUntypedArray>
means “a reference with lifetime'x
to a GIL binding with lifetime'py
to a PyArrayObject”. So the reference needs to live (most likely) shorter than the GIL binding2 or maximally equally long.&'y PyArrayObject
means “a reference with lifetime'y
to a PyArrayObject”.The previous version returned a reference
&'y PyArrayObject
with'y: 'py
, which means that the returned reference is valid as long as the GIL binding is held. In reality the returned reference is derived from the input reference&'x
. Remember that this&'x
reference is probably shorter-lived than the GIL binding. So before this fix, nothing stopped us from dropping the&'x
reference (because as far as rustc is concerned, nothing derives from it), creating a new, mutable reference to the same object, and mutating the object through that, while other parts of our code are still allowed to read that object through the&'y
reference. Something like:Footnotes
I’ll only call regular Rust references “reference” after this to avoid confusion ↩
because before you reference something, you need to create it, and the thing can only be destroyed after you dropped your reference ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to say thanks for the detailed explanation! It still makes my head spin, which is a good reason to avoid unsafe in my own code 😅