]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99925: Fix inconsistency in `json.dumps()` error messages (GH-99926)
authorFrantišek Nesveda <fnesveda@users.noreply.github.com>
Tue, 20 Dec 2022 10:54:56 +0000 (10:54 +0000)
committerGitHub <noreply@github.com>
Tue, 20 Dec 2022 10:54:56 +0000 (12:54 +0200)
Lib/test/test_json/test_float.py
Misc/NEWS.d/next/Library/2022-12-01-15-44-58.gh-issue-99925.x4y6pF.rst [new file with mode: 0644]
Modules/_json.c

index d0c7214334d6e5c9ffff99abfa890a59735fdb49..61540a3a02c2c6f1bd5de70f372537d91b90114a 100644 (file)
@@ -26,7 +26,8 @@ class TestFloat:
                 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
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 (file)
index 0000000..660635a
--- /dev/null
@@ -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.
index 6879ad3d0722b6cb08c4d2c78169d235fe1ba6fa..fa8e2a936d2c33af391344ba01a7279b5657675b 100644 (file)
@@ -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;
         }