Skip to content

Commit

Permalink
Merge pull request #3312 from davidhewitt/opt-baseexception
Browse files Browse the repository at this point in the history
optimize is_instance for PyBaseException
  • Loading branch information
adamreichold authored Jul 14, 2023
2 parents d2c690b + 2729975 commit 65312b4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,10 @@ impl PyErr {
/// });
/// ```
pub fn from_value(obj: &PyAny) -> PyErr {
let ptr = obj.as_ptr();

let state = if unsafe { ffi::PyExceptionInstance_Check(ptr) } != 0 {
let state = if let Ok(obj) = obj.downcast::<PyBaseException>() {
PyErrState::Normalized(PyErrStateNormalized {
ptype: obj.get_type().into(),
pvalue: unsafe { Py::from_borrowed_ptr(obj.py(), obj.as_ptr()) },
pvalue: obj.into(),
ptraceback: None,
})
} else if unsafe { ffi::PyExceptionClass_Check(obj.as_ptr()) } != 0 {
Expand Down
8 changes: 5 additions & 3 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ macro_rules! create_exception_type_object {
}

macro_rules! impl_native_exception (
($name:ident, $exc_name:ident, $doc:expr, $layout:path) => (
($name:ident, $exc_name:ident, $doc:expr, $layout:path $(, #checkfunction=$checkfunction:path)?) => (
#[doc = $doc]
#[allow(clippy::upper_case_acronyms)]
pub struct $name($crate::PyAny);

$crate::impl_exception_boilerplate!($name);
$crate::pyobject_native_type!($name, $layout, |_py| unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject });
$crate::pyobject_native_type!($name, $layout, |_py| unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject } $(, #checkfunction=$checkfunction)?);
);
($name:ident, $exc_name:ident, $doc:expr) => (
impl_native_exception!($name, $exc_name, $doc, $crate::ffi::PyBaseExceptionObject);
Expand Down Expand Up @@ -359,7 +359,9 @@ Python::with_gil(|py| {
impl_native_exception!(
PyBaseException,
PyExc_BaseException,
native_doc!("BaseException")
native_doc!("BaseException"),
ffi::PyBaseExceptionObject,
#checkfunction=ffi::PyExceptionInstance_Check
);
impl_native_exception!(PyException, PyExc_Exception, native_doc!("Exception"));
impl_native_exception!(
Expand Down

0 comments on commit 65312b4

Please sign in to comment.