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

Fixes and refinements to dynamic borrow checking #302

Merged
merged 3 commits into from
Mar 22, 2022

Conversation

adamreichold
Copy link
Member

The thing here is making PyArray::resize unsafe as it may reallocate the array in-place thereby invalidate all existing pointers into its interior. The safe replacement is PyReadwriteArray::resize which will release and re-acquire an exclusive borrow of the array.

Does anybody now of other "destructive" methods which would need the same treatment?

I think was arguably unsound for the previous PyReadonlyArray as well as the following appears to work:

use numpy::{npyffi::NPY_ARRAY_WRITEABLE, PyArray};
use pyo3::Python;

Python::with_gil(|py| {
    let pyarray = PyArray::arange(py, 0, 10, 1);
    assert_eq!(pyarray.len(), 10);

    unsafe {
        (*pyarray.as_array_ptr()).flags &= !NPY_ARRAY_WRITEABLE;
        pyarray.resize(100).unwrap();
    }
    assert_eq!(pyarray.len(), 100);
});

@adamreichold adamreichold force-pushed the borrow-checking-redux branch 2 times, most recently from b81fbe5 to 30aaaa8 Compare March 21, 2022 11:09
@adamreichold adamreichold force-pushed the borrow-checking-redux branch from 30aaaa8 to a013e7b Compare March 22, 2022 07:05
Copy link
Member

@kngwyu kngwyu left a comment

Choose a reason for hiding this comment

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

Thanks, sounds reasonable. I feel it a bit of shame for me to remain this as is.

Does anybody now of other "destructive" methods which would need the same treatment?

No, I found no other API.

@adamreichold adamreichold merged commit fff48b8 into main Mar 22, 2022
@adamreichold adamreichold deleted the borrow-checking-redux branch March 22, 2022 10:15
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