Skip to content

Commit

Permalink
Refactor is_subclass_by_flag!(), is_subclass_by_type!()
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Feb 9, 2023
1 parent ff6f3bb commit 1bc65b2
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/serialize/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,18 @@ pub fn pyobject_to_obtype(obj: *mut pyo3_ffi::PyObject, opts: Opt) -> ObType {
}
}

macro_rules! is_subclass {
macro_rules! is_subclass_by_flag {
($ob_type:expr, $flag:ident) => {
(((*$ob_type).tp_flags & pyo3_ffi::$flag) != 0)
};
}

macro_rules! is_subclass_by_type {
($ob_type:expr, $type:ident) => {
(*($ob_type as *mut PyTypeObject)).ob_type == $type
};
}

#[cold]
#[cfg_attr(feature = "optimize", optimize(size))]
#[inline(never)]
Expand All @@ -119,22 +125,22 @@ pub fn pyobject_to_obtype_unlikely(obj: *mut pyo3_ffi::PyObject, opts: Opt) -> O
ObType::Tuple
} else if ob_type == UUID_TYPE {
ObType::Uuid
} else if (*(ob_type as *mut PyTypeObject)).ob_type == ENUM_TYPE {
} else if is_subclass_by_type!(ob_type, ENUM_TYPE) {
ObType::Enum
} else if opts & PASSTHROUGH_SUBCLASS == 0
&& is_subclass!(ob_type, Py_TPFLAGS_UNICODE_SUBCLASS)
&& is_subclass_by_flag!(ob_type, Py_TPFLAGS_UNICODE_SUBCLASS)
{
ObType::StrSubclass
} else if opts & PASSTHROUGH_SUBCLASS == 0
&& is_subclass!(ob_type, Py_TPFLAGS_LONG_SUBCLASS)
&& is_subclass_by_flag!(ob_type, Py_TPFLAGS_LONG_SUBCLASS)
{
ObType::Int
} else if opts & PASSTHROUGH_SUBCLASS == 0
&& is_subclass!(ob_type, Py_TPFLAGS_LIST_SUBCLASS)
&& is_subclass_by_flag!(ob_type, Py_TPFLAGS_LIST_SUBCLASS)
{
ObType::List
} else if opts & PASSTHROUGH_SUBCLASS == 0
&& is_subclass!(ob_type, Py_TPFLAGS_DICT_SUBCLASS)
&& is_subclass_by_flag!(ob_type, Py_TPFLAGS_DICT_SUBCLASS)
{
ObType::Dict
} else if opts & PASSTHROUGH_DATACLASS == 0
Expand Down

0 comments on commit 1bc65b2

Please sign in to comment.