Skip to content

Commit

Permalink
port PyErr::from_type to Bound API (#3885)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Feb 23, 2024
1 parent 5ca8102 commit 6a81587
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ impl PyErr {
})))
}

/// Deprecated form of [`PyErr::from_type_bound`]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyErr::from_type` will be replaced by `PyErr::from_type_bound` in a future PyO3 version"
)
)]
pub fn from_type<A>(ty: &PyType, args: A) -> PyErr
where
A: PyErrArguments + Send + Sync + 'static,
{
PyErr::from_state(PyErrState::lazy(ty.into(), args))
}

/// Constructs a new PyErr from the given Python type and arguments.
///
/// `ty` is the exception type; usually one of the standard exceptions
Expand All @@ -192,11 +207,11 @@ impl PyErr {
/// If `ty` does not inherit from `BaseException`, then a `TypeError` will be returned.
///
/// If calling `ty` with `args` raises an exception, that exception will be returned.
pub fn from_type<A>(ty: &PyType, args: A) -> PyErr
pub fn from_type_bound<A>(ty: Bound<'_, PyType>, args: A) -> PyErr
where
A: PyErrArguments + Send + Sync + 'static,
{
PyErr::from_state(PyErrState::lazy(ty.into(), args))
PyErr::from_state(PyErrState::lazy(ty.unbind().into_any(), args))
}

/// Deprecated form of [`PyErr::from_value_bound`].
Expand Down Expand Up @@ -1231,10 +1246,8 @@ mod tests {
assert!(!err.matches(py, PyTypeError::type_object_bound(py)));

// String is not a valid exception class, so we should get a TypeError
let err: PyErr = PyErr::from_type(
crate::types::PyString::type_object_bound(py).as_gil_ref(),
"foo",
);
let err: PyErr =
PyErr::from_type_bound(crate::types::PyString::type_object_bound(py), "foo");
assert!(err.matches(py, PyTypeError::type_object_bound(py)));
})
}
Expand Down

0 comments on commit 6a81587

Please sign in to comment.