The Wayback Machine - https://web.archive.org/web/20221223182052/https://github.com/python/cpython/pull/99926/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-99925: Fix inconsistency in json.dumps() error messages #99926

Merged
merged 4 commits into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
@@ -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.
@@ -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;
}