Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl<'a, 'py, T: Element + 'a, D: Dimension + 'a> FromPyObject<'a, 'py>

fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
let array = PyArray::<T, D>::extract::<PyErr>(obj, npyffi::PyArray_Check)?;
Ok(array.readonly())
Ok(array.try_readonly()?)
}
}

Expand Down Expand Up @@ -488,7 +488,7 @@ impl<'a, 'py, T: Element + 'a, D: Dimension + 'a> FromPyObject<'a, 'py>

fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
let array = PyArray::<T, D>::extract::<PyErr>(obj, npyffi::PyArray_Check)?;
Ok(array.readwrite())
Ok(array.try_readwrite()?)
}
}

Expand Down
10 changes: 7 additions & 3 deletions tests/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use numpy::{
};
use pyo3::{
ffi::c_str,
py_run, pyclass, pymethods,
pyclass, pymethods,
types::{IntoPyDict, PyAnyMethods},
Py, Python,
};
Expand Down Expand Up @@ -97,16 +97,20 @@ impl Borrower {
}

#[test]
#[should_panic(expected = "AlreadyBorrowed")]
fn borrows_span_frames() {
Python::attach(|py| {
let borrower = Py::new(py, Borrower).unwrap();
let borrower = borrower.bind(py);

let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false);

let _exclusive = array.readwrite();

py_run!(py, borrower array, "borrower.exclusive(array)");
let locals = [("borrower", borrower.as_any()), ("array", array.as_any())]
.into_py_dict(py)
.unwrap();
py.run(c"borrower.exclusive(array)", Some(&locals), None)
.expect_err("already borrowed");
});
}

Expand Down
Loading