Skip to content
Open
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
10 changes: 5 additions & 5 deletions src/borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self, Self::Error> {
let array = obj.cast::<PyArray<T, D>>()?;
Ok(array.readonly())
Ok(array.try_readonly()?)
}
}

Expand Down Expand Up @@ -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<Self, Self::Error> {
let array = obj.cast::<PyArray<T, D>>()?;
Ok(array.readwrite())
Ok(array.try_readwrite()?)
}
}

Expand Down
7 changes: 4 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,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::<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