Skip to content

Commit

Permalink
pythongh-99925: Fix inconsistency in json.dumps() error messages (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
fnesveda committed Dec 20, 2022
1 parent a6331b6 commit d98ca81
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Lib/test/test_json/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def test_allow_nan(self):
res = self.loads(out)
self.assertEqual(len(res), 1)
self.assertNotEqual(res[0], res[0])
self.assertRaises(ValueError, self.dumps, [val], allow_nan=False)
msg = f'Out of range float values are not JSON compliant: {val}'
self.assertRaisesRegex(ValueError, msg, self.dumps, [val], allow_nan=False)


class TestPyFloat(TestFloat, PyTest): pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Unify error messages in JSON serialization between
``json.dumps(float('nan'), allow_nan=False)`` and ``json.dumps(float('nan'),
allow_nan=False, indent=<SOMETHING>)``. Now both include the representation
of the value that could not be serialized.
5 changes: 3 additions & 2 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1319,9 +1319,10 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
double i = PyFloat_AS_DOUBLE(obj);
if (!Py_IS_FINITE(i)) {
if (!s->allow_nan) {
PyErr_SetString(
PyErr_Format(
PyExc_ValueError,
"Out of range float values are not JSON compliant"
"Out of range float values are not JSON compliant: %R",
obj
);
return NULL;
}
Expand Down

0 comments on commit d98ca81

Please sign in to comment.