Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
suggestions
  • Loading branch information
WillAyd committed Nov 7, 2022
commit 0d2798e9d94f374499054a3b788e73ff26d22736
Original file line number Diff line number Diff line change
@@ -1 +1 @@
+ Fix issue where the error messages for :func:`json.dump` and :func:`json.dumps` were inconsistent when serializing non-finite values with ``allow_nan=False``
+ 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``
9 changes: 3 additions & 6 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1325,21 +1325,18 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
double i = PyFloat_AS_DOUBLE(obj);
if (!Py_IS_FINITE(i)) {
char* value;
char *py_value;
if (i > 0) {
value = "Infinity";
py_value = "inf";
} else if (i < 0) {
value = "-Infinity";
py_value = "-inf";
} else {
value = "NaN";
py_value = "nan";
}

if (!s->allow_nan) {
char message[55] = "Out of range float values are not JSON compliant: ";
PyErr_SetString(PyExc_ValueError, strcat(message, py_value));
PyErr_Format(PyExc_ValueError,
"Out of range float values are not JSON compliant: %S",
obj);
return NULL;
} else {
return PyUnicode_FromString(value);
Expand Down