classification
Title: Inconsistent errors for JSON-encoding NaNs with allow_nan=False
Type: behavior Stage:
Components: Extension Modules, Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: earwig, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-09-24 16:44 by earwig, last changed 2020-09-24 17:05 by serhiy.storchaka.

Messages (1)
msg377450 - (view) Author: Ben Kurtovic (earwig) Date: 2020-09-24 16:44
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.

json.dumps uses this C implementation: https://github.com/python/cpython/blob/d67de0a30d76c6a28056bae22fd7d13f2e111b77/Modules/_json.c#L1340
json.dump uses this Python implementation: https://github.com/python/cpython/blob/d67de0a30d76c6a28056bae22fd7d13f2e111b77/Lib/json/encoder.py#L223

(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.)
History
Date User Action Args
2020-09-24 17:05:07serhiy.storchakasetnosy: + serhiy.storchaka
2020-09-24 16:44:20earwigcreate