Skip to content

Commit 5c8f93d

Browse files
cdce8pijl
authored andcommitted
Use PyType_GetDict to safely access tp_dict [3.12]
1 parent e9b745e commit 5c8f93d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/util.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,20 @@ macro_rules! str_hash {
125125
};
126126
}
127127

128-
#[cfg(Py_3_10)]
128+
#[cfg(Py_3_12)]
129+
macro_rules! pydict_contains {
130+
($obj1:expr, $obj2:expr) => {
131+
unsafe {
132+
pyo3_ffi::_PyDict_Contains_KnownHash(
133+
pyo3_ffi::PyType_GetDict($obj1),
134+
$obj2,
135+
(*$obj2.cast::<pyo3_ffi::PyASCIIObject>()).hash,
136+
) == 1
137+
}
138+
};
139+
}
140+
141+
#[cfg(all(Py_3_10, not(Py_3_12)))]
129142
macro_rules! pydict_contains {
130143
($obj1:expr, $obj2:expr) => {
131144
unsafe {

test/test_default.py

+12
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,15 @@ def default(obj):
275275
orjson.dumps(ref, default=default)
276276

277277
assert sys.getrefcount(ref) == 2 # one for ref, one for default
278+
279+
def test_default_set(self):
280+
"""
281+
dumps() default function with set
282+
"""
283+
284+
def default(obj):
285+
if isinstance(obj, set):
286+
return list(obj)
287+
raise TypeError
288+
289+
assert orjson.dumps({1, 2}, default=default) == b"[1,2]"

0 commit comments

Comments
 (0)