[Python-checkins] gh-99925: Fix inconsistency in `json.dumps()` error messages (GH-99926)
serhiy-storchaka
webhook-mailer at python.org
Tue Dec 20 05:55:10 EST 2022
https://github.com/python/cpython/commit/d98ca8172c39326bb200308a5191ceeb4a262d53
commit: d98ca8172c39326bb200308a5191ceeb4a262d53
branch: main
author: František Nesveda <fnesveda at users.noreply.github.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-12-20T12:54:56+02:00
summary:
gh-99925: Fix inconsistency in `json.dumps()` error messages (GH-99926)
files:
A Misc/NEWS.d/next/Library/2022-12-01-15-44-58.gh-issue-99925.x4y6pF.rst
M Lib/test/test_json/test_float.py
M Modules/_json.c
diff --git a/Lib/test/test_json/test_float.py b/Lib/test/test_json/test_float.py
index d0c7214334d6..61540a3a02c2 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
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 000000000000..660635a03963
--- /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.
diff --git a/Modules/_json.c b/Modules/_json.c
index 6879ad3d0722..fa8e2a936d2c 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: %R",
+ obj
);
return NULL;
}
More information about the Python-checkins
mailing list