Pyerr isinstance (#3826)

* Implement PyErr::is_instance_bound

* Update is_instance_bound to take a reference

Co-authored-by: David Hewitt <mail@davidhewitt.dev>

* Remove spurious clone

---------

Co-authored-by: David Hewitt <mail@davidhewitt.dev>
This commit is contained in:
Lily Foote 2024-02-12 08:32:51 +00:00 committed by GitHub
parent c359f5ca1d
commit 1279467d27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 3 deletions

View File

@ -538,12 +538,25 @@ 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))
}
/// 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 {
self.is_instance_bound(py, &ty.as_borrowed())
}
/// Returns true if the current exception is instance of `T`.
#[inline]
pub fn is_instance(&self, py: Python<'_>, ty: &PyAny) -> bool {
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
}
@ -554,7 +567,7 @@ impl PyErr {
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))
}
/// Writes the error back to the Python interpreter's global state.