From fbc781d9a4524bb421dc22827a3303b47a014c57 2012-07-23 20:48:31
From: MinRK <benjaminrk@gmail.com>
Date: 2012-07-23 20:48:31
Subject: [PATCH] clean nan/inf in json_clean

these values are not json-compliant

---

diff --git a/IPython/utils/jsonutil.py b/IPython/utils/jsonutil.py
index f689460..422cf19 100644
--- a/IPython/utils/jsonutil.py
+++ b/IPython/utils/jsonutil.py
@@ -30,6 +30,12 @@ next_attr_name = '__next__' if py3compat.PY3 else 'next'
 ISO8601="%Y-%m-%dT%H:%M:%S.%f"
 ISO8601_PAT=re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+$")
 
+# float constants
+NAN  = float('nan')
+INF  = float('inf')
+NINF = float('-inf')
+INFS = (INF, NINF)
+
 #-----------------------------------------------------------------------------
 # Classes and functions
 #-----------------------------------------------------------------------------
@@ -161,11 +167,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 obj != obj or obj in INFS:
+            return repr(obj)
+        return obj
+    
     if isinstance(obj, atomic_ok):
         return obj