From f93f6d12455799640aeb98af540a654d1c1c4f76 Mon Sep 17 00:00:00 2001 From: Emanuele Giaquinta Date: Sun, 15 Dec 2024 18:54:23 +0200 Subject: [PATCH 1/2] Rename is_type macro to py_is Signed-off-by: Emanuele Giaquinta --- src/deserialize/deserializer.rs | 6 +++--- src/serialize/dataclass.rs | 4 ++-- src/serialize/dict.rs | 6 +++--- src/serialize/serializer.rs | 30 +++++++++++++++--------------- src/util.rs | 6 +++--- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/deserialize/deserializer.rs b/src/deserialize/deserializer.rs index 6c8d9bab..d62c6854 100644 --- a/src/deserialize/deserializer.rs +++ b/src/deserialize/deserializer.rs @@ -25,10 +25,10 @@ pub fn deserialize( let buffer: *const u8; let length: usize; - if is_type!(obj_type_ptr, BYTES_TYPE) { + if py_is!(obj_type_ptr, BYTES_TYPE) { buffer = unsafe { PyBytes_AS_STRING(ptr) as *const u8 }; length = unsafe { PyBytes_GET_SIZE(ptr) as usize }; - } else if is_type!(obj_type_ptr, MEMORYVIEW_TYPE) { + } else if py_is!(obj_type_ptr, MEMORYVIEW_TYPE) { let membuf = unsafe { PyMemoryView_GET_BUFFER(ptr) }; if unsafe { pyo3::ffi::PyBuffer_IsContiguous(membuf, b'C' as c_char) == 0 } { return Err(DeserializeError::new(Cow::Borrowed( @@ -37,7 +37,7 @@ pub fn deserialize( } buffer = unsafe { (*membuf).buf as *const u8 }; length = unsafe { (*membuf).len as usize }; - } else if is_type!(obj_type_ptr, BYTEARRAY_TYPE) { + } else if py_is!(obj_type_ptr, BYTEARRAY_TYPE) { buffer = ffi!(PyByteArray_AsString(ptr)) as *const u8; length = ffi!(PyByteArray_Size(ptr)) as usize; } else { diff --git a/src/serialize/dataclass.rs b/src/serialize/dataclass.rs index 9d41711d..d722edaa 100644 --- a/src/serialize/dataclass.rs +++ b/src/serialize/dataclass.rs @@ -65,7 +65,7 @@ impl Serialize for AttributeDict { let mut items: SmallVec<[(&str, *mut pyo3::ffi::PyObject); 8]> = SmallVec::with_capacity(len); for (key, value) in PyDictIter::from_pyobject(self.ptr) { - if unlikely!(!is_type!(ob_type!(key.as_ptr()), STR_TYPE)) { + if unlikely!(!py_is!(ob_type!(key.as_ptr()), STR_TYPE)) { err!(KEY_MUST_BE_STR) } let data = unicode_to_str(key.as_ptr()); @@ -128,7 +128,7 @@ impl Dataclass { fn is_pseudo_field(field: *mut pyo3::ffi::PyObject) -> bool { let field_type = ffi!(PyObject_GetAttr(field, FIELD_TYPE_STR)); ffi!(Py_DECREF(field_type)); - !is_type!(field_type as *mut pyo3::ffi::PyTypeObject, FIELD_TYPE) + !py_is!(field_type as *mut pyo3::ffi::PyTypeObject, FIELD_TYPE) } impl Serialize for Dataclass { diff --git a/src/serialize/dict.rs b/src/serialize/dict.rs index 6e5fafa6..a7861af2 100644 --- a/src/serialize/dict.rs +++ b/src/serialize/dict.rs @@ -114,7 +114,7 @@ impl Serialize for DictWithStrKeys { let len = ffi!(Py_SIZE(self.ptr)) as usize; let mut map = serializer.serialize_map(Some(len)).unwrap(); for (key, value) in PyDictIter::from_pyobject(self.ptr) { - if unlikely!(!is_type!(ob_type!(key.as_ptr()), STR_TYPE)) { + if unlikely!(!py_is!(ob_type!(key.as_ptr()), STR_TYPE)) { err!(KEY_MUST_BE_STR) } let data = unicode_to_str(key.as_ptr()); @@ -171,7 +171,7 @@ impl Serialize for DictWithSortedStrKeys { let mut items: SmallVec<[(&str, *mut pyo3::ffi::PyObject); 8]> = SmallVec::with_capacity(len); for (key, value) in PyDictIter::from_pyobject(self.ptr) { - if unlikely!(!is_type!(ob_type!(key.as_ptr()), STR_TYPE)) { + if unlikely!(!py_is!(ob_type!(key.as_ptr()), STR_TYPE)) { err!(KEY_MUST_BE_STR) } let data = unicode_to_str(key.as_ptr()); @@ -235,7 +235,7 @@ impl Serialize for DictWithNonStrKeys { let len = ffi!(Py_SIZE(self.ptr)) as usize; let mut map = serializer.serialize_map(Some(len)).unwrap(); for (key, value) in PyDictIter::from_pyobject(self.ptr) { - if is_type!(ob_type!(key.as_ptr()), STR_TYPE) { + if py_is!(ob_type!(key.as_ptr()), STR_TYPE) { let data = unicode_to_str(key.as_ptr()); if unlikely!(data.is_none()) { err!(INVALID_STR) diff --git a/src/serialize/serializer.rs b/src/serialize/serializer.rs index 987e0474..5d093342 100644 --- a/src/serialize/serializer.rs +++ b/src/serialize/serializer.rs @@ -552,11 +552,11 @@ pub enum ObType { pub fn pyobject_to_obtype(obj: *mut pyo3::ffi::PyObject, opts: Opt) -> ObType { let ob_type = ob_type!(obj); - if is_type!(ob_type, STR_TYPE) { + if py_is!(ob_type, STR_TYPE) { ObType::Str - } else if is_type!(ob_type, BYTES_TYPE) { + } else if py_is!(ob_type, BYTES_TYPE) { ObType::Bytes - } else if is_type!(ob_type, INT_TYPE) + } else if py_is!(ob_type, INT_TYPE) && (opts & PASSTHROUGH_BIG_INT == 0 || ffi!(_PyLong_NumBits(obj)) <= { if pylong_is_positive(obj) { @@ -567,17 +567,17 @@ pub fn pyobject_to_obtype(obj: *mut pyo3::ffi::PyObject, opts: Opt) -> ObType { }) { ObType::Int - } else if is_type!(ob_type, BOOL_TYPE) { + } else if py_is!(ob_type, BOOL_TYPE) { ObType::Bool - } else if is_type!(ob_type, NONE_TYPE) { + } else if py_is!(ob_type, NONE_TYPE) { ObType::None - } else if is_type!(ob_type, FLOAT_TYPE) { + } else if py_is!(ob_type, FLOAT_TYPE) { ObType::Float - } else if is_type!(ob_type, LIST_TYPE) { + } else if py_is!(ob_type, LIST_TYPE) { ObType::List - } else if is_type!(ob_type, DICT_TYPE) { + } else if py_is!(ob_type, DICT_TYPE) { ObType::Dict - } else if is_type!(ob_type, DATETIME_TYPE) && opts & PASSTHROUGH_DATETIME == 0 { + } else if py_is!(ob_type, DATETIME_TYPE) && opts & PASSTHROUGH_DATETIME == 0 { ObType::Datetime } else { pyobject_to_obtype_unlikely(obj, opts) @@ -593,15 +593,15 @@ macro_rules! is_subclass { #[inline(never)] pub fn pyobject_to_obtype_unlikely(obj: *mut pyo3::ffi::PyObject, opts: Opt) -> ObType { let ob_type = ob_type!(obj); - if is_type!(ob_type, DATE_TYPE) && opts & PASSTHROUGH_DATETIME == 0 { + if py_is!(ob_type, DATE_TYPE) && opts & PASSTHROUGH_DATETIME == 0 { ObType::Date - } else if is_type!(ob_type, TIME_TYPE) && opts & PASSTHROUGH_DATETIME == 0 { + } else if py_is!(ob_type, TIME_TYPE) && opts & PASSTHROUGH_DATETIME == 0 { ObType::Time - } else if is_type!(ob_type, TUPLE_TYPE) && opts & PASSTHROUGH_TUPLE == 0 { + } else if py_is!(ob_type, TUPLE_TYPE) && opts & PASSTHROUGH_TUPLE == 0 { ObType::Tuple - } else if is_type!(ob_type, UUID_TYPE) { + } else if py_is!(ob_type, UUID_TYPE) { ObType::Uuid - } else if is_type!(ob_type!(ob_type), ENUM_TYPE) { + } else if py_is!(ob_type!(ob_type), ENUM_TYPE) { ObType::Enum } else if opts & PASSTHROUGH_SUBCLASS == 0 && is_subclass!(ob_type, Py_TPFLAGS_UNICODE_SUBCLASS) { @@ -633,7 +633,7 @@ pub fn pyobject_to_obtype_unlikely(obj: *mut pyo3::ffi::PyObject, opts: Opt) -> || pydict_contains!(ob_type, PYDANTIC2_VALIDATOR_STR)) { ObType::Pydantic - } else if is_type!(ob_type, EXT_TYPE) { + } else if py_is!(ob_type, EXT_TYPE) { ObType::Ext } else { ObType::Unknown diff --git a/src/util.rs b/src/util.rs index 8ca01905..835014b5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,8 +1,8 @@ // SPDX-License-Identifier: (Apache-2.0 OR MIT) -macro_rules! is_type { - ($obj_ptr:expr, $type_ptr:expr) => { - unsafe { $obj_ptr == $type_ptr } +macro_rules! py_is { + ($x:expr, $y:expr) => { + unsafe { $x == $y } }; } From 717c31767da72fd0b34f213de0be80a52bfa5aeb Mon Sep 17 00:00:00 2001 From: Emanuele Giaquinta Date: Sun, 15 Dec 2024 17:59:53 +0200 Subject: [PATCH 2/2] Simplify lookup of object types Signed-off-by: Emanuele Giaquinta --- src/serialize/serializer.rs | 2 +- src/typeref.rs | 31 ++++++++++--------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/serialize/serializer.rs b/src/serialize/serializer.rs index 5d093342..8b3f0ccd 100644 --- a/src/serialize/serializer.rs +++ b/src/serialize/serializer.rs @@ -569,7 +569,7 @@ pub fn pyobject_to_obtype(obj: *mut pyo3::ffi::PyObject, opts: Opt) -> ObType { ObType::Int } else if py_is!(ob_type, BOOL_TYPE) { ObType::Bool - } else if py_is!(ob_type, NONE_TYPE) { + } else if py_is!(obj, NONE) { ObType::None } else if py_is!(ob_type, FLOAT_TYPE) { ObType::Float diff --git a/src/typeref.rs b/src/typeref.rs index 0caf8d1c..57c92413 100644 --- a/src/typeref.rs +++ b/src/typeref.rs @@ -41,7 +41,6 @@ pub static mut MEMORYVIEW_TYPE: *mut PyTypeObject = null_mut(); pub static mut STR_TYPE: *mut PyTypeObject = null_mut(); pub static mut INT_TYPE: *mut PyTypeObject = null_mut(); pub static mut BOOL_TYPE: *mut PyTypeObject = null_mut(); -pub static mut NONE_TYPE: *mut PyTypeObject = null_mut(); pub static mut FLOAT_TYPE: *mut PyTypeObject = null_mut(); pub static mut LIST_TYPE: *mut PyTypeObject = null_mut(); pub static mut DICT_TYPE: *mut PyTypeObject = null_mut(); @@ -107,26 +106,16 @@ pub fn init_typerefs() { TRUE = Py_True(); FALSE = Py_False(); EMPTY_UNICODE = PyUnicode_New(0, 255); - STR_TYPE = (*EMPTY_UNICODE).ob_type; - BYTES_TYPE = (*PyBytes_FromStringAndSize("".as_ptr() as *const c_char, 0)).ob_type; - - { - let bytearray = PyByteArray_FromStringAndSize("".as_ptr() as *const c_char, 0); - BYTEARRAY_TYPE = (*bytearray).ob_type; - - let memoryview = PyMemoryView_FromObject(bytearray); - MEMORYVIEW_TYPE = (*memoryview).ob_type; - Py_DECREF(memoryview); - Py_DECREF(bytearray); - } - - DICT_TYPE = (*PyDict_New()).ob_type; - LIST_TYPE = (*PyList_New(0)).ob_type; - TUPLE_TYPE = (*PyTuple_New(0)).ob_type; - NONE_TYPE = (*NONE).ob_type; - BOOL_TYPE = (*TRUE).ob_type; - INT_TYPE = (*PyLong_FromLongLong(0)).ob_type; - FLOAT_TYPE = (*PyFloat_FromDouble(0.0)).ob_type; + STR_TYPE = &mut PyUnicode_Type; + BYTES_TYPE = &mut PyBytes_Type; + BYTEARRAY_TYPE = &mut PyByteArray_Type; + MEMORYVIEW_TYPE = &mut PyMemoryView_Type; + DICT_TYPE = &mut PyDict_Type; + LIST_TYPE = &mut PyList_Type; + TUPLE_TYPE = &mut PyTuple_Type; + BOOL_TYPE = &mut PyBool_Type; + INT_TYPE = &mut PyLong_Type; + FLOAT_TYPE = &mut PyFloat_Type; DATETIME_TYPE = look_up_datetime_type(); DATE_TYPE = look_up_date_type(); TIME_TYPE = look_up_time_type();