From 85e2f78d0ef09426d70457467ba8bb4003f331ae Mon Sep 17 00:00:00 2001 From: Emanuele Giaquinta Date: Sun, 15 Dec 2024 17:59:53 +0200 Subject: [PATCH] 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 5d09334..8b3f0cc 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 0caf8d1..57c9241 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();