Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-86018 Inconsistent errors for JSON-encoding NaNs with allow_nan=False #99172

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

Closed
wants to merge 3 commits into from
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 9 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.