You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent errors for JSON-encoding NaNs with allow_nan=False #86018
json.dump vs. json.dumps have inconsistent error messages when encoding NaN with allow_nan=False:
>>> json.dumps(float('nan'), allow_nan=False)
ValueError: Out of range float values are not JSON compliant
>>> json.dump(float('nan'), sys.stdout, allow_nan=False)
ValueError: Out of range float values are not JSON compliant: nan
json.dump's error includes the value (which is useful); json.dumps's does not.
(Separately, is it expected that only one uses the C implementation? This seems it could have unexpected performance implications - without testing, I would've expected json.dump to take the "faster" path since it doesn't need to hold the entire encoded string in memory.)
>>> json.dumps(float('nan'), allow_nan=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\wanne\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 238, in dumps**kw).encode(obj)
^^^^^^^^^^^
File "C:\Users\wanne\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", line 200, in encode
chunks =self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\wanne\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", line 258, in iterencodereturn _iterencode(o, 0)
^^^^^^^^^^^^^^^^^ValueError: Out of range float values are not JSON compliant: nan
It was reported in a separate issue #99925 and resolved by #99926. I think this one was just forgotten. I'll close it as completed, even though it's a duplicate (well technically, the other is a duplicate...)
Activity
earwig commentedon Sep 24, 2020
json.dump vs. json.dumps have inconsistent error messages when encoding NaN with allow_nan=False:
json.dump's error includes the value (which is useful); json.dumps's does not.
json.dumps uses this C implementation:
cpython/Modules/_json.c
Line 1340 in d67de0a
json.dump uses this Python implementation:
cpython/Lib/json/encoder.py
Line 223 in d67de0a
(Separately, is it expected that only one uses the C implementation? This seems it could have unexpected performance implications - without testing, I would've expected json.dump to take the "faster" path since it doesn't need to hold the entire encoded string in memory.)
pythongh-86018 Inconsistent errors for JSON-encoding NaNs with allow_…
nineteendo commentedon Jun 30, 2024
This seems to be fixed:
>>> json.dumps(float('nan'), allow_nan=False) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\wanne\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 238, in dumps **kw).encode(obj) ^^^^^^^^^^^ File "C:\Users\wanne\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\wanne\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", line 258, in iterencode return _iterencode(o, 0) ^^^^^^^^^^^^^^^^^ ValueError: Out of range float values are not JSON compliant: nanpicnixz commentedon Sep 7, 2024
It was reported in a separate issue #99925 and resolved by #99926. I think this one was just forgotten. I'll close it as completed, even though it's a duplicate (well technically, the other is a duplicate...)