[issue41852] Inconsistent errors for JSON-encoding NaNs with allow_nan=False

Ben Kurtovic report at bugs.python.org
Thu Sep 24 12:44:20 EDT 2020


New submission from Ben Kurtovic <ben.kurtovic at gmail.com>:

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.)

----------
components: Extension Modules, Library (Lib)
messages: 377450
nosy: earwig
priority: normal
severity: normal
status: open
title: Inconsistent errors for JSON-encoding NaNs with allow_nan=False
type: behavior
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41852>
_______________________________________


More information about the Python-bugs-list mailing list