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

clean nan/inf in json_clean #2194

Merged
merged 2 commits into from Jul 27, 2012
Merged
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
9 changes: 8 additions & 1 deletion IPython/utils/jsonutil.py
Expand Up @@ -11,6 +11,7 @@
# Imports # Imports
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# stdlib # stdlib
import math
import re import re
import sys import sys
import types import types
Expand Down Expand Up @@ -161,11 +162,17 @@ def json_clean(obj):
""" """
# types that are 'atomic' and ok in json as-is. bool doesn't need to be # types that are 'atomic' and ok in json as-is. bool doesn't need to be
# listed explicitly because bools pass as int instances # listed explicitly because bools pass as int instances
atomic_ok = (unicode, int, float, types.NoneType) atomic_ok = (unicode, int, types.NoneType)


# containers that we need to convert into lists # containers that we need to convert into lists
container_to_list = (tuple, set, types.GeneratorType) container_to_list = (tuple, set, types.GeneratorType)


if isinstance(obj, float):
# cast out-of-range floats to their reprs
if math.isnan(obj) or math.isinf(obj):
return repr(obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You and I both know obj != obj is the way that you test for a a float being NaN, but I wonder if it wouldn't be a lot clearer to do:

if math.isnan(obj) or math.isinf(obj):
    return repr(obj)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the exact same tests in stlib json, but those seem clearer, so I will do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return obj

if isinstance(obj, atomic_ok): if isinstance(obj, atomic_ok):
return obj return obj


Expand Down