New submission from Ben Kurtovic <ben.kurto...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue41852>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to