Skip to content

Commit

Permalink
convert TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored and adamreichold committed Mar 23, 2024
1 parent fad4e18 commit 5e0793e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
let src_dtype = array.dtype();
let dst_dtype = T::get_dtype_bound(ob.py());
if !src_dtype.is_equiv_to(&dst_dtype) {
return Err(TypeError::new(src_dtype.into_gil_ref(), dst_dtype.into_gil_ref()).into());
return Err(TypeError::new(src_dtype, dst_dtype).into());
}

Ok(array)
Expand Down
18 changes: 10 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use std::error::Error;
use std::fmt;

use pyo3::{exceptions::PyTypeError, Py, PyErr, PyErrArguments, PyObject, Python, ToPyObject};
use pyo3::{
exceptions::PyTypeError, Bound, Py, PyErr, PyErrArguments, PyObject, Python, ToPyObject,
};

use crate::dtype::PyArrayDescr;

Expand Down Expand Up @@ -59,13 +61,13 @@ impl_pyerr!(DimensionalityError);

/// Represents that types of the given arrays do not match.
#[derive(Debug)]
pub struct TypeError<'a> {
from: &'a PyArrayDescr,
to: &'a PyArrayDescr,
pub struct TypeError<'py> {
from: Bound<'py, PyArrayDescr>,
to: Bound<'py, PyArrayDescr>,
}

impl<'a> TypeError<'a> {
pub(crate) fn new(from: &'a PyArrayDescr, to: &'a PyArrayDescr) -> Self {
impl<'py> TypeError<'py> {
pub(crate) fn new(from: Bound<'py, PyArrayDescr>, to: Bound<'py, PyArrayDescr>) -> Self {
Self { from, to }
}
}
Expand All @@ -86,8 +88,8 @@ struct TypeErrorArguments {
impl PyErrArguments for TypeErrorArguments {
fn arguments<'py>(self, py: Python<'py>) -> PyObject {
let err = TypeError {
from: self.from.as_ref(py),
to: self.to.as_ref(py),
from: self.from.into_bound(py),
to: self.to.into_bound(py),
};

err.to_string().to_object(py)
Expand Down

0 comments on commit 5e0793e

Please sign in to comment.