Skip to content

Commit

Permalink
Rename is_type macro to py_is
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuele Giaquinta <[email protected]>
  • Loading branch information
exg committed Dec 15, 2024
1 parent 8efe824 commit c5f02b5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
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!(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)
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
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

0 comments on commit c5f02b5

Please sign in to comment.