Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify lookup of object types #326

Merged
merged 2 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/deserialize/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/serialize/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/serialize/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions src/serialize/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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!(obj, NONE) {
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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
31 changes: 10 additions & 21 deletions src/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -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 }
};
}

Expand Down
Loading