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

Merged
merged 4 commits into from Dec 20, 2022

Conversation

fnesveda
Copy link
Contributor

@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

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

PyOS_double_to_string() returns a pointer to an array allocated on the heap. You should use PyMem_Free() to deallocate it.

But it would be simpler to use format %R with the Python object obj.

Please add also a test for error message.

@bedevere-bot
Copy link

bedevere-bot commented Dec 17, 2022

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@fnesveda fnesveda requested a review from serhiy-storchaka Dec 19, 2022
@fnesveda
Copy link
Contributor Author

fnesveda commented Dec 19, 2022

@serhiy-storchaka Thanks for the review, makes sense 🙂

I have made the requested changes; please review again.

@bedevere-bot
Copy link

bedevere-bot commented Dec 19, 2022

Thanks for making the requested changes!

@serhiy-storchaka: please review the changes made to this pull request.

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Dec 20, 2022

LGTM.

@serhiy-storchaka serhiy-storchaka merged commit d98ca81 into python:main Dec 20, 2022
16 checks passed
jonburdo pushed a commit to jonburdo/cpython that referenced this pull request Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent JSON serialization error messages
3 participants