From 94f9f5945f4756b8357d3fb2a41608385f9aa3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Nesveda?= <frantisek.nesveda@gmail.com> Date: Thu, 1 Dec 2022 15:27:46 +0100 Subject: [PATCH 1/4] gh-99925: Fix inconsistency in `json.dumps()` error messages --- .../Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst | 2 ++ Modules/_json.c | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst diff --git a/Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst b/Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst new file mode 100644 index 00000000000000..a31ae378be1a92 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst @@ -0,0 +1,2 @@ +Unify error messages in :meth:`json.dumps(float('nan'), allow_nan=False)` +between `indent=None` and `indent=<SOMETHING>` diff --git a/Modules/_json.c b/Modules/_json.c index 81431aa1041c55..81c3fc5a304ea8 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -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: %s", + PyOS_double_to_string(i, 'r', 0, 0, NULL) ); return NULL; } From 764a75e69c0d3108a98d9e1e68e8b4fcca8a6b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Nesveda?= <frantisek.nesveda@gmail.com> Date: Thu, 1 Dec 2022 15:46:01 +0100 Subject: [PATCH 2/4] Fix news blurb format --- .../Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst | 2 -- .../Library/2022-12-01-15-44-58.gh-issue-99925.x4y6pF.rst | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst create mode 100644 Misc/NEWS.d/next/Library/2022-12-01-15-44-58.gh-issue-99925.x4y6pF.rst diff --git a/Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst b/Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst deleted file mode 100644 index a31ae378be1a92..00000000000000 --- a/Misc/NEWS.d/next/Library/2022-12-01-15-26-42.gh-issue-99925.vXya8e.rst +++ /dev/null @@ -1,2 +0,0 @@ -Unify error messages in :meth:`json.dumps(float('nan'), allow_nan=False)` -between `indent=None` and `indent=<SOMETHING>` diff --git a/Misc/NEWS.d/next/Library/2022-12-01-15-44-58.gh-issue-99925.x4y6pF.rst b/Misc/NEWS.d/next/Library/2022-12-01-15-44-58.gh-issue-99925.x4y6pF.rst new file mode 100644 index 00000000000000..660635a039631c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-12-01-15-44-58.gh-issue-99925.x4y6pF.rst @@ -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. From ece854052060aa91bb0c4209bfcdda6de8dcc126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Nesveda?= <frantisek.nesveda@gmail.com> Date: Mon, 19 Dec 2022 11:26:21 +0100 Subject: [PATCH 3/4] Improve error string formatting --- Modules/_json.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_json.c b/Modules/_json.c index 81c3fc5a304ea8..a808bd42ea067b 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1321,8 +1321,8 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj) if (!s->allow_nan) { PyErr_Format( PyExc_ValueError, - "Out of range float values are not JSON compliant: %s", - PyOS_double_to_string(i, 'r', 0, 0, NULL) + "Out of range float values are not JSON compliant: %R", + obj ); return NULL; } From 3cba672b1f5bb94723c2dcd0cdba95e17be0933a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Nesveda?= <frantisek.nesveda@gmail.com> Date: Mon, 19 Dec 2022 11:26:36 +0100 Subject: [PATCH 4/4] Add unit test for error message --- Lib/test/test_json/test_float.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_json/test_float.py b/Lib/test/test_json/test_float.py index d0c7214334d6e5..61540a3a02c2c6 100644 --- a/Lib/test/test_json/test_float.py +++ b/Lib/test/test_json/test_float.py @@ -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