From 157d99af34e87d80f0d2d025201188f300572f65 2012-07-27 02:03:31 From: Bradley M. Froehle Date: 2012-07-27 02:03:31 Subject: [PATCH] Merge pull request #2194 from minrk/clean_nan Clean nan/inf in json_clean. The floating point values NaN and Infinity are not part of the JSON specification and causes some parsers to throw errors. Since our usage is only for things like the display of function defaults, we can use a basic string representation ('NaN', 'inf', etc) instead. --- diff --git a/IPython/utils/jsonutil.py b/IPython/utils/jsonutil.py index f689460..ed4d959 100644 --- a/IPython/utils/jsonutil.py +++ b/IPython/utils/jsonutil.py @@ -11,6 +11,7 @@ # Imports #----------------------------------------------------------------------------- # stdlib +import math import re import sys import types @@ -161,11 +162,17 @@ def json_clean(obj): """ # types that are 'atomic' and ok in json as-is. bool doesn't need to be # 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 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) + return obj + if isinstance(obj, atomic_ok): return obj