diff --git a/Lib/test/test_json/test_float.py b/Lib/test/test_json/test_float.py index d0c7214334d6e5..8abcf317253083 100644 --- a/Lib/test/test_json/test_float.py +++ b/Lib/test/test_json/test_float.py @@ -26,8 +26,9 @@ 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) - + self.assertRaisesRegex( + ValueError, str(val), self.dumps, [val], allow_nan=False + ) class TestPyFloat(TestFloat, PyTest): pass class TestCFloat(TestFloat, CTest): pass diff --git a/Misc/NEWS.d/next/Library/2022-11-06-19-59-56.gh-issue-86018.xAt6g2.rst b/Misc/NEWS.d/next/Library/2022-11-06-19-59-56.gh-issue-86018.xAt6g2.rst new file mode 100644 index 00000000000000..dad1fc22f47b2a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-11-06-19-59-56.gh-issue-86018.xAt6g2.rst @@ -0,0 +1 @@ ++ Fix issue where the error messages for :meth:`json.dump` and :meth:`json.dumps` were inconsistent when serializing non-finite values with ``allow_nan=False`` diff --git a/Modules/_json.c b/Modules/_json.c index fe8695110f017d..f496b9401e1baa 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1325,9 +1325,9 @@ 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: %S", obj ); return NULL; }