Visitar URL original
gh-99925: Fix inconsistency in `json.dumps()` error messages by fnesveda · Pull Request #99926 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20221207034327/https://github.com/python/cpython/pull/99926
Skip to content
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

gh-99925: Fix inconsistency in json.dumps() error messages #99926

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fnesveda
Copy link

@fnesveda fnesveda commented Dec 1, 2022 •

Fixes #99925.

When you try to serialize a NaN, inf or -inf with json.dumps(..., allow_nan=False), the error messages are inconsistent, depending on whether you use indent or not.

>>> json.dumps(float('nan'), allow_nan=False)
ValueError: Out of range float values are not JSON compliant

>>> json.dumps(float('nan'), allow_nan=False, indent=4)
ValueError: Out of range float values are not JSON compliant: nan

That is because if you don't use indent, the encoding is done in C code here,
but if you use indent, the encoding is done in pure Python code here, and the error messages are different between the two.

This PR unifies the error messages, so that both now include the representation of the invalid value.

@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Dec 1, 2022 •

All commit authors signed the Contributor License Agreement.
CLA signed

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

Successfully merging this pull request may close these issues.

Inconsistent JSON serialization error messages
2 participants