[issue25457] json dump fails for mixed-type keys when sort_keys is specified

James Edwards report at bugs.python.org
Fri Jun 29 03:08:41 EDT 2018


James Edwards <jheiv at jheiv.com> added the comment:

This came up in a StackOverflow question[1] today, so I took a stab at addressing the error.  The changes don't restore the 2.x behavior, but just do as R. David Murray suggested and coerce the keys to strings prior to sorting to prevent the error.

The changes in _json.c and json.decoder are handled slightly differently in the case of skipkeys.

Both create a list of (coerced_key, value) pairs, sorts it (when specified), and uses that in place of the PyDict_Items / .items().

When skipkeys=True and invalid (uncoercible) keys are found, the c code will just not append that item to the coerced_items list while the python code uses None to signal that item should be filtered out.

(That being said, I'm not a huge fan of the approach I used in the Python code and may rewrite using .append instead of a generator.

The c code could definitely use a review when it comes to reference counts.

Fork commit: https://github.com/jheiv/cpython/commit/8d3612f56a137da0d26b83d00507ff2f11bca9bb

[1] https://stackoverflow.com/questions/51093268/why-am-i-getting-typeerror-unorderable-types-str-int-in-this-code

----------
nosy: +jedwards

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue25457>
_______________________________________


More information about the Python-bugs-list mailing list