Skip to content

Commit 11a1302

Browse files
committed
Simplify default fallthrough
1 parent d3fedd3 commit 11a1302

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/serialize/default.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,11 @@ impl<'p> Serialize for DefaultSerializer {
5656
self.ptr,
5757
std::ptr::null_mut() as *mut pyo3::ffi::PyObject
5858
));
59-
if default_obj.is_null() {
59+
if unlikely!(default_obj.is_null()) {
6060
err!(format_args!(
6161
"Type is not JSON serializable: {}",
6262
obj_name!(ob_type!(self.ptr))
6363
))
64-
} else if !ffi!(PyErr_Occurred()).is_null() {
65-
err!(format_args!(
66-
"Type raised exception in default function: {}",
67-
obj_name!(ob_type!(self.ptr))
68-
))
6964
} else {
7065
let res = SerializePyObject::new(
7166
default_obj,

src/serialize/uuid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl UUID {
2727
// test_uuid_overflow
2828
pyo3::ffi::_PyLong_AsByteArray(
2929
py_int as *mut pyo3::ffi::PyLongObject,
30-
buffer.as_ptr() as *const c_uchar,
30+
buffer.as_ptr() as *mut c_uchar,
3131
16,
3232
1, // little_endian
3333
0, // is_signed

test/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
arrow
22
dataclasses;python_version<"3.7"
33
numpy;platform_machine=="x86_64" and python_version<"3.9"
4-
pendulum;sys_platform=="linux" and python_version<"3.9" and platform_machine=="x86_64"
4+
pendulum;sys_platform=="linux" and platform_machine=="x86_64"
55
psutil
66
pytest
77
pytz

test/test_default.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def default(obj):
2626
return obj.cur
2727

2828

29+
def default_raises(obj):
30+
raise TypeError
31+
32+
2933
class TypeTests(unittest.TestCase):
3034
def test_default_not_callable(self):
3135
"""
@@ -95,6 +99,15 @@ def default(obj):
9599
ran = True
96100
self.assertTrue(ran)
97101

102+
def test_default_exception_type(self):
103+
"""
104+
dumps() TypeError in default() raises orjson.JSONEncodeError
105+
"""
106+
ref = Custom()
107+
108+
with self.assertRaises(orjson.JSONEncodeError):
109+
orjson.dumps(ref, default=default_raises)
110+
98111
def test_default_func_nested_str(self):
99112
"""
100113
dumps() default function nested str

0 commit comments

Comments
 (0)