Skip to content

Commit

Permalink
Fix crash with python 3.12
Browse files Browse the repository at this point in the history
See python/cpython#105227

Signed-off-by: Emanuele Giaquinta <emanuele.giaquinta@gmail.com>
  • Loading branch information
exg committed Oct 3, 2023
1 parent 729272a commit 32f91dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/serialize/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Serialize for DataclassGenericSerializer {
self.default,
)
.serialize(serializer)
} else if ffi!(PyDict_Contains((*ob_type).tp_dict, SLOTS_STR)) == 1 {
} else if pydict_contains!(ob_type, SLOTS_STR) {
let ret = DataclassFallbackSerializer::new(
self.ptr,
self.opts,
Expand Down
8 changes: 3 additions & 5 deletions src/serialize/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,15 @@ pub fn pyobject_to_obtype_unlikely(obj: *mut pyo3::ffi::PyObject, opts: Opt) ->
ObType::List
} else if opts & PASSTHROUGH_SUBCLASS == 0 && is_subclass!(ob_type, Py_TPFLAGS_DICT_SUBCLASS) {
ObType::Dict
} else if opts & PASSTHROUGH_DATACLASS == 0
&& ffi!(PyDict_Contains((*ob_type).tp_dict, DATACLASS_FIELDS_STR)) == 1
{
} else if opts & PASSTHROUGH_DATACLASS == 0 && pydict_contains!(ob_type, DATACLASS_FIELDS_STR) {
ObType::Dataclass
} else if opts & SERIALIZE_NUMPY != 0 && is_numpy_scalar(ob_type) {
ObType::NumpyScalar
} else if opts & SERIALIZE_NUMPY != 0 && is_numpy_array(ob_type) {
ObType::NumpyArray
} else if opts & SERIALIZE_PYDANTIC != 0
&& (ffi!(PyDict_Contains((*ob_type).tp_dict, PYDANTIC_FIELDS_STR)) == 1
|| ffi!(PyDict_Contains((*ob_type).tp_dict, PYDANTIC2_FIELDS_STR)) == 1)
&& (pydict_contains!(ob_type, PYDANTIC_FIELDS_STR)
|| pydict_contains!(ob_type, PYDANTIC2_FIELDS_STR))
{
ObType::Pydantic
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,17 @@ macro_rules! call_method {
}
};
}

#[cfg(not(Py_3_12))]
macro_rules! pydict_contains {
($obj1:expr, $obj2:expr) => {
unsafe { pyo3::ffi::PyDict_Contains((*$obj1).tp_dict, $obj2) == 1 }
};
}

#[cfg(Py_3_12)]
macro_rules! pydict_contains {
($obj1:expr, $obj2:expr) => {
unsafe { pyo3::ffi::PyDict_Contains(pyo3::ffi::PyType_GetDict($obj1), $obj2) == 1 }
};
}

0 comments on commit 32f91dd

Please sign in to comment.