Skip to content

Commit d98ca81

Browse files
authored
gh-99925: Fix inconsistency in json.dumps() error messages (GH-99926)
1 parent a6331b6 commit d98ca81

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

Lib/test/test_json/test_float.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def test_allow_nan(self):
2626
res = self.loads(out)
2727
self.assertEqual(len(res), 1)
2828
self.assertNotEqual(res[0], res[0])
29-
self.assertRaises(ValueError, self.dumps, [val], allow_nan=False)
29+
msg = f'Out of range float values are not JSON compliant: {val}'
30+
self.assertRaisesRegex(ValueError, msg, self.dumps, [val], allow_nan=False)
3031

3132

3233
class TestPyFloat(TestFloat, PyTest): pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Unify error messages in JSON serialization between
2+
``json.dumps(float('nan'), allow_nan=False)`` and ``json.dumps(float('nan'),
3+
allow_nan=False, indent=<SOMETHING>)``. Now both include the representation
4+
of the value that could not be serialized.

Modules/_json.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1319,9 +1319,10 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
13191319
double i = PyFloat_AS_DOUBLE(obj);
13201320
if (!Py_IS_FINITE(i)) {
13211321
if (!s->allow_nan) {
1322-
PyErr_SetString(
1322+
PyErr_Format(
13231323
PyExc_ValueError,
1324-
"Out of range float values are not JSON compliant"
1324+
"Out of range float values are not JSON compliant: %R",
1325+
obj
13251326
);
13261327
return NULL;
13271328
}

0 commit comments

Comments
 (0)