Skip to content

Commit

Permalink
Add PyDowncastErrorArguments to delay formatting downcast errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Apr 6, 2022
1 parent b14015c commit 9d89056
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/err/mod.rs
Expand Up @@ -663,10 +663,34 @@ impl<'a> IntoPy<PyObject> for &'a PyErr {
}
}

struct PyDowncastErrorArguments {
from: Py<PyType>,
to: Cow<'static, str>,
}

impl PyErrArguments for PyDowncastErrorArguments {
fn arguments(self, py: Python<'_>) -> PyObject {
format!(
"'{}' object cannot be converted to '{}'",
self.from
.as_ref(py)
.name()
.unwrap_or("<failed to extract type name>"),
self.to
)
.to_object(py)
}
}

/// Convert `PyDowncastError` to Python `TypeError`.
impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
fn from(err: PyDowncastError<'_>) -> PyErr {
exceptions::PyTypeError::new_err(err.to_string())
let args = PyDowncastErrorArguments {
from: err.from.get_type().into(),
to: err.to,
};

exceptions::PyTypeError::new_err(args)
}
}

Expand Down

0 comments on commit 9d89056

Please sign in to comment.