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