Skip to content

Commit

Permalink
Implement PyErr::is_instance_bound
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Feb 11, 2024
1 parent 1badb0e commit 8bef883
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,23 +566,37 @@ impl PyErr {
where
T: ToPyObject,
{
self.is_instance(py, exc.to_object(py).as_ref(py))
self.is_instance_bound(py, exc.to_object(py).bind(py).clone())
}

/// Returns true if the current exception is instance of `T`.
/// Deprecated form of `PyErr::is_instance_bound`.
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyErr::is_instance` will be replaced by `PyErr::is_instance_bound` in a future PyO3 version"
)
)]
#[inline]
pub fn is_instance(&self, py: Python<'_>, ty: &PyAny) -> bool {
let type_bound = self.get_type_bound(py);
(unsafe { ffi::PyErr_GivenExceptionMatches(type_bound.as_ptr(), ty.as_ptr()) }) != 0
}

/// Returns true if the current exception is instance of `T`.
#[inline]
pub fn is_instance_bound(&self, py: Python<'_>, ty: Bound<'_, PyAny>) -> bool {
let type_bound = self.get_type_bound(py);
(unsafe { ffi::PyErr_GivenExceptionMatches(type_bound.as_ptr(), ty.as_ptr()) }) != 0
}

/// Returns true if the current exception is instance of `T`.
#[inline]
pub fn is_instance_of<T>(&self, py: Python<'_>) -> bool
where
T: PyTypeInfo,
{
self.is_instance(py, T::type_object_bound(py).as_gil_ref())
self.is_instance_bound(py, T::type_object_bound(py).as_any().clone())
}

/// Writes the error back to the Python interpreter's global state.
Expand Down

0 comments on commit 8bef883

Please sign in to comment.