Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9eb1365

Browse files
committed
Backport PR ipython#2778: P3K: fix DeprecationWarning under Python 3.x
The warning is: ``` .../site-packages/IPython/utils/jsonutil.py:121: DeprecationWarning: encodestring() is a deprecated alias, use encodebytes() encoded['image/png'] = encodestring(pngdata).decode('ascii') ```
1 parent 7ad908b commit 9eb1365

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

IPython/utils/jsonutil.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
import re
1616
import sys
1717
import types
18-
from base64 import encodestring
1918
from datetime import datetime
2019

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+
2127
from IPython.utils import py3compat
2228
from IPython.utils.encoding import DEFAULT_ENCODING
2329
from IPython.utils import text
@@ -82,7 +88,7 @@ def squash_dates(obj):
8288
elif isinstance(obj, datetime):
8389
obj = obj.strftime(ISO8601)
8490
return obj
85-
91+
8692
def date_default(obj):
8793
"""default function for packing datetime objects in JSON."""
8894
if isinstance(obj, datetime):
@@ -97,37 +103,37 @@ def date_default(obj):
97103

98104
def encode_images(format_dict):
99105
"""b64-encodes images in a displaypub format dict
100-
106+
101107
Perhaps this should be handled in json_clean itself?
102-
108+
103109
Parameters
104110
----------
105-
111+
106112
format_dict : dict
107113
A dictionary of display data keyed by mime-type
108-
114+
109115
Returns
110116
-------
111-
117+
112118
format_dict : dict
113119
A copy of the same dictionary,
114120
but binary image data ('image/png' or 'image/jpeg')
115121
is base64-encoded.
116-
122+
117123
"""
118124
encoded = format_dict.copy()
119125
pngdata = format_dict.get('image/png')
120126
if isinstance(pngdata, bytes) and pngdata[:8] == PNG:
121-
encoded['image/png'] = encodestring(pngdata).decode('ascii')
127+
encoded['image/png'] = encodebytes(pngdata).decode('ascii')
122128
jpegdata = format_dict.get('image/jpeg')
123129
if isinstance(jpegdata, bytes) and jpegdata[:2] == JPEG:
124-
encoded['image/jpeg'] = encodestring(jpegdata).decode('ascii')
130+
encoded['image/jpeg'] = encodebytes(jpegdata).decode('ascii')
125131
return encoded
126132

127133

128134
def json_clean(obj):
129135
"""Clean an object to ensure it's safe to encode in JSON.
130-
136+
131137
Atomic, immutable objects are returned unmodified. Sets and tuples are
132138
converted to lists, lists are copied and dicts are also copied.
133139
@@ -142,7 +148,7 @@ def json_clean(obj):
142148
Returns
143149
-------
144150
out : object
145-
151+
146152
A version of the input which will not cause an encoding error when
147153
encoded as JSON. Note that this function does not *encode* its inputs,
148154
it simply sanitizes it so that there will be no encoding errors later.
@@ -163,7 +169,7 @@ def json_clean(obj):
163169
# types that are 'atomic' and ok in json as-is. bool doesn't need to be
164170
# listed explicitly because bools pass as int instances
165171
atomic_ok = (unicode, int, types.NoneType)
166-
172+
167173
# containers that we need to convert into lists
168174
container_to_list = (tuple, set, types.GeneratorType)
169175

@@ -175,14 +181,14 @@ def json_clean(obj):
175181

176182
if isinstance(obj, atomic_ok):
177183
return obj
178-
184+
179185
if isinstance(obj, bytes):
180186
return obj.decode(DEFAULT_ENCODING, 'replace')
181-
187+
182188
if isinstance(obj, container_to_list) or (
183189
hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)):
184190
obj = list(obj)
185-
191+
186192
if isinstance(obj, list):
187193
return [json_clean(x) for x in obj]
188194

0 commit comments

Comments
 (0)