Skip to content

Commit c96b08b

Browse files
committed
err!()
1 parent f4a9c70 commit c96b08b

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

src/encode.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ macro_rules! obj_name {
2626
};
2727
}
2828

29+
macro_rules! err {
30+
($msg:expr) => {
31+
return Err(ser::Error::custom($msg));
32+
};
33+
}
34+
2935
pub fn serialize(
3036
ptr: *mut pyo3::ffi::PyObject,
3137
default: Option<NonNull<pyo3::ffi::PyObject>>,
@@ -70,17 +76,17 @@ impl<'p> Serialize for SerializePyObject {
7076
let mut str_size: pyo3::ffi::Py_ssize_t = 0;
7177
let uni = ffi!(PyUnicode_AsUTF8AndSize(self.ptr, &mut str_size)) as *const u8;
7278
if unlikely!(uni.is_null()) {
73-
return Err(ser::Error::custom(INVALID_STR));
79+
err!(INVALID_STR)
7480
}
7581
serializer.serialize_str(str_from_slice!(uni, str_size))
7682
} else if is_type!(obj_ptr, INT_PTR) {
7783
let val = ffi!(PyLong_AsLongLong(self.ptr));
7884
if unlikely!(val == -1 && !pyo3::ffi::PyErr_Occurred().is_null()) {
79-
return Err(ser::Error::custom("Integer exceeds 64-bit range"));
85+
err!("Integer exceeds 64-bit range")
8086
} else if self.opts & STRICT_INTEGER == STRICT_INTEGER
8187
&& (val > STRICT_INT_MAX || val < STRICT_INT_MIN)
8288
{
83-
return Err(ser::Error::custom("Integer exceeds 53-bit range"));
89+
err!("Integer exceeds 53-bit range")
8490
}
8591
serializer.serialize_i64(val)
8692
} else if is_type!(obj_ptr, LIST_PTR) {
@@ -90,7 +96,7 @@ impl<'p> Serialize for SerializePyObject {
9096
let mut i = 0;
9197
while i < len {
9298
if unlikely!(self.recursion == 255) {
93-
return Err(ser::Error::custom("Recursion limit reached"));
99+
err!("Recursion limit reached")
94100
}
95101
let elem = ffi!(PyList_GET_ITEM(self.ptr, i as pyo3::ffi::Py_ssize_t));
96102
i += 1;
@@ -114,14 +120,14 @@ impl<'p> Serialize for SerializePyObject {
114120
let mut value: *mut pyo3::ffi::PyObject = std::ptr::null_mut();
115121
while unsafe { pyo3::ffi::PyDict_Next(self.ptr, &mut pos, &mut key, &mut value) != 0 } {
116122
if unlikely!(self.recursion == 255) {
117-
return Err(ser::Error::custom("Recursion limit reached"));
123+
err!("Recursion limit reached")
118124
}
119125
if unlikely!((*key).ob_type != STR_PTR) {
120-
return Err(ser::Error::custom("Dict key must be str"));
126+
err!("Dict key must be str")
121127
}
122128
let data = ffi!(PyUnicode_AsUTF8AndSize(key, &mut str_size)) as *const u8;
123129
if unlikely!(data.is_null()) {
124-
return Err(ser::Error::custom(INVALID_STR));
130+
err!(INVALID_STR)
125131
}
126132
map.serialize_entry(
127133
str_from_slice!(data, str_size),
@@ -167,10 +173,10 @@ impl<'p> Serialize for SerializePyObject {
167173
match write_datetime(self.ptr, self.opts, &mut dt) {
168174
Ok(_) => serializer.serialize_str(str_from_slice!(dt.as_ptr(), dt.len())),
169175
Err(DatetimeError::Offset) => {
170-
return Err(ser::Error::custom("datetime does not support timezones with offsets that are not even minutes",));
176+
err!("datetime does not support timezones with offsets that are not even minutes")
171177
}
172178
Err(DatetimeError::Library) => {
173-
return Err(ser::Error::custom("datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil"));
179+
err!("datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil")
174180
}
175181
}
176182
} else if is_type!(obj_ptr, DATE_PTR) {
@@ -179,16 +185,14 @@ impl<'p> Serialize for SerializePyObject {
179185
serializer.serialize_str(str_from_slice!(dt.as_ptr(), dt.len()))
180186
} else if is_type!(obj_ptr, TIME_PTR) {
181187
if unsafe { (*(self.ptr as *mut pyo3::ffi::PyDateTime_Time)).hastzinfo == 1 } {
182-
return Err(ser::Error::custom("datetime.time must not have tzinfo set"));
188+
err!("datetime.time must not have tzinfo set")
183189
}
184190
let mut dt: SmallVec<[u8; 32]> = SmallVec::with_capacity(32);
185191
write_time(self.ptr, &mut dt);
186192
serializer.serialize_str(str_from_slice!(dt.as_ptr(), dt.len()))
187193
} else if self.default.is_some() {
188194
if self.default_calls > 5 {
189-
Err(ser::Error::custom(
190-
"default serializer exceeds recursion limit",
191-
))
195+
err!("default serializer exceeds recursion limit")
192196
} else {
193197
let default_obj = unsafe {
194198
pyo3::ffi::PyObject_CallFunctionObjArgs(
@@ -209,22 +213,22 @@ impl<'p> Serialize for SerializePyObject {
209213
ffi!(Py_DECREF(default_obj));
210214
res
211215
} else if !ffi!(PyErr_Occurred()).is_null() {
212-
Err(ser::Error::custom(format_args!(
216+
err!(format_args!(
213217
"Type raised exception in default function: {}",
214218
obj_name!(obj_ptr)
215-
)))
219+
))
216220
} else {
217-
Err(ser::Error::custom(format_args!(
221+
err!(format_args!(
218222
"Type is not JSON serializable: {}",
219223
obj_name!(obj_ptr)
220-
)))
224+
))
221225
}
222226
}
223227
} else {
224-
Err(ser::Error::custom(format_args!(
228+
err!(format_args!(
225229
"Type is not JSON serializable: {}",
226230
obj_name!(obj_ptr)
227-
)))
231+
))
228232
}
229233
}
230234
}

0 commit comments

Comments
 (0)