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:
parent
c359f5ca1d
commit
1279467d27
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue