Skip to content

Commit

Permalink
Add ToPyErr::to_py_err
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Feb 25, 2024
1 parent ccde801 commit 749265c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bound<'py, T>> 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()
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
};
}

Expand Down

0 comments on commit 749265c

Please sign in to comment.