mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Skip overridden items for empty dictionaries in JSON encoding (#9018)
Split the Python 3.14-compatible empty native dictionary fast path from #8954. Extend the existing JSON snippet with root, nested, sorting, and nonempty subclass regression coverage. Assisted-by: OpenAI Codex:model identifier unavailable
This commit is contained in:
1 parent
e6cbf3628d
commit
48419bd114
2 files changed
+22
No files matched your search
@@ -1160,6 +1160,10 @@ mod _json {
|
||||
out: &mut Wtf8Buf,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
if dict.is_empty() {
|
||||
out.push_str("{}");
|
||||
return Ok(());
|
||||
}
|
||||
let mut items = self.dict_items(obj, dict, vm)?;
|
||||
if items.is_empty() {
|
||||
out.push_str("{}");
|
||||
|
||||
@@ -67,6 +67,24 @@ assert "{}" == json.dumps({})
|
||||
assert "{}" == json_dump({})
|
||||
assert round_trip_test({"a": "b"})
|
||||
|
||||
|
||||
class EmptyDictWithItems(dict):
|
||||
def items(self):
|
||||
raise AssertionError("items() must not be called for an empty dictionary")
|
||||
|
||||
|
||||
for sort_keys in (False, True):
|
||||
assert json.dumps(EmptyDictWithItems(), sort_keys=sort_keys) == "{}"
|
||||
assert json.dumps([EmptyDictWithItems()], sort_keys=sort_keys) == "[{}]"
|
||||
|
||||
|
||||
class NonemptyDictWithItems(dict):
|
||||
def items(self):
|
||||
return [("replacement", 2)]
|
||||
|
||||
|
||||
assert json.dumps(NonemptyDictWithItems(original=1)) == '{"replacement": 2}'
|
||||
|
||||
# should reject non-str keys in jsons
|
||||
assert_raises(json.JSONDecodeError, lambda: json.loads('{3: "abc"}'))
|
||||
assert_raises(json.JSONDecodeError, lambda: json_load('{3: "abc"}'))
|
||||
|
||||
Reference in new issue
Block a user