diff --git a/src/err/mod.rs b/src/err/mod.rs index 5e054449bc9..4bcac2b6280 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -988,15 +988,18 @@ impl PyErrArguments for PyDowncastErrorArguments { /// /// Users should not need to implement this trait directly. It is implemented automatically in the /// [`crate::import_exception!`] and [`crate::create_exception!`] macros. -pub trait ToPyErr {} +pub trait ToPyErr { + /// Convert self to PyErr + fn to_py_err(self) -> PyErr; +} impl<'py, T> std::convert::From> for PyErr where - T: ToPyErr, + Bound<'py, T>: ToPyErr, { #[inline] fn from(err: Bound<'py, T>) -> PyErr { - PyErr::from_value_bound(err.into_any()) + err.to_py_err() } } diff --git a/src/exceptions.rs b/src/exceptions.rs index ef55dcdd6e3..196d62e0cfd 100644 --- a/src/exceptions.rs +++ b/src/exceptions.rs @@ -53,7 +53,11 @@ macro_rules! impl_exception_boilerplate { } } - impl $crate::ToPyErr for $name {} + impl $crate::ToPyErr for $crate::Bound<'_, $name> { + fn to_py_err(self) -> $crate::PyErr { + $crate::PyErr::from_value_bound(self.into_any()) + } + } }; }