Skip to content

bpo-36841: JSONEncoder call self.default for unsupported floats #28648

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Lib/json/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ def default(self, o):
return JSONEncoder.default(self, o)

"""
if isinstance(o, float):
raise ValueError(
"Out of range float values are not JSON compliant: " +
repr(o)
)
raise TypeError(f'Object of type {o.__class__.__name__} '
f'is not JSON serializable')

Expand Down Expand Up @@ -236,9 +241,7 @@ def floatstr(o, allow_nan=self.allow_nan,
return _repr(o)

if not allow_nan:
raise ValueError(
"Out of range float values are not JSON compliant: " +
repr(o))
return self.default(o)

return text

Expand Down