Skip to content

gh-110941: Fix json.dump encoding dict subclasses as empty (continued) - #150631

Open
eendebakpt wants to merge 7 commits into
python:mainfrom
eendebakpt:json_dict_subclass_gh110941
Open

gh-110941: Fix json.dump encoding dict subclasses as empty (continued)#150631
eendebakpt wants to merge 7 commits into
python:mainfrom
eendebakpt:json_dict_subclass_gh110941

Conversation

@eendebakpt

@eendebakpt eendebakpt commented May 30, 2026

Copy link
Copy Markdown
Contributor

This is a continuation of #111036 which has stalled. The PR is the same, exact that we only handle the fast path for exact dicts. This keeps the code a bit cleaner.

aljungberg and others added 6 commits October 18, 2023 13:17
See python#110941 for full details but the tldr is that the C optimised JSON encoder did not handle `dict` subclasses correctly and encoded them as empty if they did not put something into `super()`'s storage.
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 30, 2026
Comment thread Modules/_json.c
// Only take the fast path for exact dicts: a subclass may keep its
// contents outside the dict storage, so PyDict_GET_SIZE could be 0 while
// the mapping is non-empty (gh-110941).
if (PyAnyDict_CheckExact(dct) && PyDict_GET_SIZE(dct) == 0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A variation would be to use PyMapping_Size. That is slightly slower for an exact dict, but will cover dict subclasses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants