Show More
@@ -15,9 +15,15 b' import math' | |||||
15 | import re |
|
15 | import re | |
16 | import sys |
|
16 | import sys | |
17 | import types |
|
17 | import types | |
18 | from base64 import encodestring |
|
|||
19 | from datetime import datetime |
|
18 | from datetime import datetime | |
20 |
|
19 | |||
|
20 | try: | |||
|
21 | # base64.encodestring is deprecated in Python 3.x | |||
|
22 | from base64 import encodebytes | |||
|
23 | except ImportError: | |||
|
24 | # Python 2.x | |||
|
25 | from base64 import encodestring as encodebytes | |||
|
26 | ||||
21 | from IPython.utils import py3compat |
|
27 | from IPython.utils import py3compat | |
22 | from IPython.utils.encoding import DEFAULT_ENCODING |
|
28 | from IPython.utils.encoding import DEFAULT_ENCODING | |
23 | from IPython.utils import text |
|
29 | from IPython.utils import text | |
@@ -82,7 +88,7 b' def squash_dates(obj):' | |||||
82 | elif isinstance(obj, datetime): |
|
88 | elif isinstance(obj, datetime): | |
83 | obj = obj.strftime(ISO8601) |
|
89 | obj = obj.strftime(ISO8601) | |
84 | return obj |
|
90 | return obj | |
85 |
|
91 | |||
86 | def date_default(obj): |
|
92 | def date_default(obj): | |
87 | """default function for packing datetime objects in JSON.""" |
|
93 | """default function for packing datetime objects in JSON.""" | |
88 | if isinstance(obj, datetime): |
|
94 | if isinstance(obj, datetime): | |
@@ -97,37 +103,37 b" JPEG = b'\\xff\\xd8'" | |||||
97 |
|
103 | |||
98 | def encode_images(format_dict): |
|
104 | def encode_images(format_dict): | |
99 | """b64-encodes images in a displaypub format dict |
|
105 | """b64-encodes images in a displaypub format dict | |
100 |
|
106 | |||
101 | Perhaps this should be handled in json_clean itself? |
|
107 | Perhaps this should be handled in json_clean itself? | |
102 |
|
108 | |||
103 | Parameters |
|
109 | Parameters | |
104 | ---------- |
|
110 | ---------- | |
105 |
|
111 | |||
106 | format_dict : dict |
|
112 | format_dict : dict | |
107 | A dictionary of display data keyed by mime-type |
|
113 | A dictionary of display data keyed by mime-type | |
108 |
|
114 | |||
109 | Returns |
|
115 | Returns | |
110 | ------- |
|
116 | ------- | |
111 |
|
117 | |||
112 | format_dict : dict |
|
118 | format_dict : dict | |
113 | A copy of the same dictionary, |
|
119 | A copy of the same dictionary, | |
114 | but binary image data ('image/png' or 'image/jpeg') |
|
120 | but binary image data ('image/png' or 'image/jpeg') | |
115 | is base64-encoded. |
|
121 | is base64-encoded. | |
116 |
|
122 | |||
117 | """ |
|
123 | """ | |
118 | encoded = format_dict.copy() |
|
124 | encoded = format_dict.copy() | |
119 | pngdata = format_dict.get('image/png') |
|
125 | pngdata = format_dict.get('image/png') | |
120 | if isinstance(pngdata, bytes) and pngdata[:8] == PNG: |
|
126 | if isinstance(pngdata, bytes) and pngdata[:8] == PNG: | |
121 |
encoded['image/png'] = encodes |
|
127 | encoded['image/png'] = encodebytes(pngdata).decode('ascii') | |
122 | jpegdata = format_dict.get('image/jpeg') |
|
128 | jpegdata = format_dict.get('image/jpeg') | |
123 | if isinstance(jpegdata, bytes) and jpegdata[:2] == JPEG: |
|
129 | if isinstance(jpegdata, bytes) and jpegdata[:2] == JPEG: | |
124 |
encoded['image/jpeg'] = encodes |
|
130 | encoded['image/jpeg'] = encodebytes(jpegdata).decode('ascii') | |
125 | return encoded |
|
131 | return encoded | |
126 |
|
132 | |||
127 |
|
133 | |||
128 | def json_clean(obj): |
|
134 | def json_clean(obj): | |
129 | """Clean an object to ensure it's safe to encode in JSON. |
|
135 | """Clean an object to ensure it's safe to encode in JSON. | |
130 |
|
136 | |||
131 | Atomic, immutable objects are returned unmodified. Sets and tuples are |
|
137 | Atomic, immutable objects are returned unmodified. Sets and tuples are | |
132 | converted to lists, lists are copied and dicts are also copied. |
|
138 | converted to lists, lists are copied and dicts are also copied. | |
133 |
|
139 | |||
@@ -142,7 +148,7 b' def json_clean(obj):' | |||||
142 | Returns |
|
148 | Returns | |
143 | ------- |
|
149 | ------- | |
144 | out : object |
|
150 | out : object | |
145 |
|
151 | |||
146 | A version of the input which will not cause an encoding error when |
|
152 | A version of the input which will not cause an encoding error when | |
147 | encoded as JSON. Note that this function does not *encode* its inputs, |
|
153 | encoded as JSON. Note that this function does not *encode* its inputs, | |
148 | it simply sanitizes it so that there will be no encoding errors later. |
|
154 | it simply sanitizes it so that there will be no encoding errors later. | |
@@ -163,26 +169,26 b' def json_clean(obj):' | |||||
163 | # types that are 'atomic' and ok in json as-is. bool doesn't need to be |
|
169 | # types that are 'atomic' and ok in json as-is. bool doesn't need to be | |
164 | # listed explicitly because bools pass as int instances |
|
170 | # listed explicitly because bools pass as int instances | |
165 | atomic_ok = (unicode, int, types.NoneType) |
|
171 | atomic_ok = (unicode, int, types.NoneType) | |
166 |
|
172 | |||
167 | # containers that we need to convert into lists |
|
173 | # containers that we need to convert into lists | |
168 | container_to_list = (tuple, set, types.GeneratorType) |
|
174 | container_to_list = (tuple, set, types.GeneratorType) | |
169 |
|
175 | |||
170 | if isinstance(obj, float): |
|
176 | if isinstance(obj, float): | |
171 | # cast out-of-range floats to their reprs |
|
177 | # cast out-of-range floats to their reprs | |
172 | if math.isnan(obj) or math.isinf(obj): |
|
178 | if math.isnan(obj) or math.isinf(obj): | |
173 | return repr(obj) |
|
179 | return repr(obj) | |
174 | return obj |
|
180 | return obj | |
175 |
|
181 | |||
176 | if isinstance(obj, atomic_ok): |
|
182 | if isinstance(obj, atomic_ok): | |
177 | return obj |
|
183 | return obj | |
178 |
|
184 | |||
179 | if isinstance(obj, bytes): |
|
185 | if isinstance(obj, bytes): | |
180 | return obj.decode(DEFAULT_ENCODING, 'replace') |
|
186 | return obj.decode(DEFAULT_ENCODING, 'replace') | |
181 |
|
187 | |||
182 | if isinstance(obj, container_to_list) or ( |
|
188 | if isinstance(obj, container_to_list) or ( | |
183 | hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)): |
|
189 | hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)): | |
184 | obj = list(obj) |
|
190 | obj = list(obj) | |
185 |
|
191 | |||
186 | if isinstance(obj, list): |
|
192 | if isinstance(obj, list): | |
187 | return [json_clean(x) for x in obj] |
|
193 | return [json_clean(x) for x in obj] | |
188 |
|
194 |
General Comments 0
You need to be logged in to leave comments.
Login now