Skip to content

Commit 5a54d59

Browse files
committed
Fix to_builtins with frozendict
1 parent 8f7ac06 commit 5a54d59

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎src/msgspec/_core.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20225,6 +20225,17 @@ cleanup:;
2022520225
return out;
2022620226
}
2022720227

20228+
#if PY315_PLUS
20229+
static PyObject *
20230+
to_builtins_frozendict(ToBuiltinsState *self, PyObject *obj) {
20231+
PyObject *out = to_builtins_dict(self, obj);
20232+
if (self->builtin_types & MS_BUILTIN_FROZENDICT) {
20233+
return _PyFrozenDict_NewSteal(out);
20234+
}
20235+
return out;
20236+
}
20237+
#endif
20238+
2022820239
static PyObject *
2022920240
to_builtins_struct(ToBuiltinsState *self, PyObject *obj, bool is_key) {
2023020241
if (Py_EnterRecursiveCall(" while serializing an object")) return NULL;
@@ -20506,8 +20517,7 @@ to_builtins(ToBuiltinsState *self, PyObject *obj, bool is_key) {
2050620517
}
2050720518
#if PY315_PLUS
2050820519
else if (PyFrozenDict_Check(obj)) {
20509-
if (self->builtin_types & MS_BUILTIN_FROZENDICT) goto builtin;
20510-
return to_builtins_dict(self, obj);
20520+
return to_builtins_frozendict(self, obj);
2051120521
}
2051220522
#endif
2051320523
else if (ms_is_struct_type(type)) {

‎tests/unit/test_to_builtins.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,17 @@ class in_type(frozendict):
331331
else:
332332
in_type = frozendict
333333

334-
msg = in_type({FruitStr.BANANA: 1, "b": [FruitInt.APPLE], 3: "three"})
334+
raw = {FruitStr.BANANA: 1, "b": [FruitInt.APPLE], 3: "three"}
335+
msg = in_type(raw)
335336

336337
res = to_builtins(msg)
337338
self.check_non_frozen(res, {"banana": 1, "b": [-1], 3: "three"})
338339
assert res is not msg
339340

341+
self.check_non_frozen(
342+
to_builtins(raw, builtin_types=[frozendict]),
343+
{"banana": 1, "b": [-1], 3: "three"},
344+
)
340345
self.check_non_frozen(to_builtins(in_type()), {})
341346

342347
@py315_or_later_only
@@ -349,13 +354,22 @@ def test_frozendict_builtin_types(self):
349354
frozendict(
350355
{
351356
"test": frozendict({1: "a"}),
357+
FruitStr.BANANA: 1,
358+
"b": [FruitInt.APPLE],
352359
}
353360
),
354361
builtin_types=(frozendict,),
355362
)
356363
assert type(msg) is frozendict
357364
assert type(msg["test"]) is frozendict
358-
assert msg == frozendict({"test": frozendict({1: "a"})})
365+
assert type(list(msg.keys())[1]) is str
366+
assert msg == frozendict(
367+
{
368+
"test": frozendict({1: "a"}),
369+
"banana": 1,
370+
"b": [-1],
371+
}
372+
)
359373

360374
@py315_or_later_only
361375
def test_frozendict_str_subclass_key(self):
@@ -629,9 +643,9 @@ def test_order_invalid(self):
629643
to_builtins(1, order="bad")
630644

631645
@staticmethod
632-
def assert_eq(left, right):
646+
def assert_eq(left, right, typ=dict):
633647
assert left == right
634-
if isinstance(left, dict):
648+
if isinstance(left, typ):
635649
assert list(left) == list(right)
636650

637651
@pytest.mark.parametrize("msg", [{}, {"y": 1, "x": 2, "z": 3}])
@@ -649,6 +663,15 @@ def test_order_dict_unsortable(self):
649663
with pytest.raises(TypeError):
650664
to_builtins({"x": 1, 1: 2}, order="deterministic")
651665

666+
@py315_or_later_only
667+
@pytest.mark.parametrize("msg", [{}, {"y": 1, "x": 2, "z": 3}])
668+
@pytest.mark.parametrize("order", [None, "deterministic", "sorted"])
669+
def test_frozendict_builtin_types_sorted(self, msg, order):
670+
msg = frozendict(msg)
671+
res = to_builtins(msg, builtin_types=[frozendict], order=order)
672+
sol = frozendict(sorted(msg.items())) if order else msg
673+
self.assert_eq(res, sol, typ=frozendict)
674+
652675
@pytest.mark.parametrize("typ", [set, frozenset])
653676
@pytest.mark.parametrize("order", ["deterministic", "sorted"])
654677
def test_order_set(self, typ, rand, order):

0 commit comments

Comments
 (0)