diff --git a/src/borrow/mod.rs b/src/borrow/mod.rs index 7ffd4faa6..590fc678d 100644 --- a/src/borrow/mod.rs +++ b/src/borrow/mod.rs @@ -175,7 +175,7 @@ use std::ops::Deref; use ndarray::{ ArrayView, ArrayViewMut, Dimension, IntoDimension, Ix0, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn, }; -use pyo3::{Borrowed, Bound, CastError, FromPyObject, PyAny, PyResult}; +use pyo3::{Borrowed, Bound, FromPyObject, PyAny, PyErr, PyResult}; use crate::array::{PyArray, PyArrayMethods}; use crate::convert::NpyIndex; @@ -240,11 +240,11 @@ where impl<'a, 'py, T: Element + 'a, D: Dimension + 'a> FromPyObject<'a, 'py> for PyReadonlyArray<'py, T, D> { - type Error = CastError<'a, 'py>; + type Error = PyErr; fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result { let array = obj.cast::>()?; - Ok(array.readonly()) + Ok(array.try_readonly()?) } } @@ -483,11 +483,11 @@ where impl<'a, 'py, T: Element + 'a, D: Dimension + 'a> FromPyObject<'a, 'py> for PyReadwriteArray<'py, T, D> { - type Error = CastError<'a, 'py>; + type Error = PyErr; fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result { let array = obj.cast::>()?; - Ok(array.readwrite()) + Ok(array.try_readwrite()?) } } diff --git a/tests/borrow.rs b/tests/borrow.rs index 52c1ad8fb..06c3d9e62 100644 --- a/tests/borrow.rs +++ b/tests/borrow.rs @@ -6,7 +6,7 @@ use numpy::{ }; use pyo3::{ ffi::c_str, - py_run, pyclass, pymethods, + pyclass, pymethods, types::{IntoPyDict, PyAnyMethods}, Py, Python, }; @@ -97,16 +97,17 @@ 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::::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"); }); }