Repository navigation
_json: skip items() for empty dict subclasses #9018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1160,6 +1160,10 @@ mod _json { | |
| out: &mut Wtf8Buf, | ||
| vm: &VirtualMachine, | ||
| ) -> PyResult<()> { | ||
| if dict.is_empty() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Validation section explicitly says the workspace and C-API targets were built with AGENTS.md reference: AGENTS.md:L35-L35 Useful? React with 👍 / 👎. |
||
| out.push_str("{}"); | ||
| return Ok(()); | ||
| } | ||
| let mut items = self.dict_items(obj, dict, vm)?; | ||
| if items.is_empty() { | ||
| out.push_str("{}"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,24 @@ def json_load(obj): | |
| 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) == "{}" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Adding this loop and its assertions changes test logic in AGENTS.md reference: AGENTS.md:L277-L279 Useful? React with 👍 / 👎. |
||
| 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"}')) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit message explicitly states that OpenAI Codex assisted, but it has no
Assisted-by: AGENT_NAME:MODEL_VERSIONtrailer. Because repository policy requires that trailer for every AI-assisted contribution, add it before this commit lands rather than relying on the prose AI-assistance section.AGENTS.md reference: AGENTS.md:L23-L23
Useful? React with 👍 / 👎.