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

Drop ipython_genutils #209

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 7 additions & 61 deletions nbclient/jsonutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
from datetime import datetime
from typing import Dict

from ipython_genutils import py3compat
from ipython_genutils.py3compat import iteritems, unicode_type

next_attr_name = '__next__' if py3compat.PY3 else 'next'

# -----------------------------------------------------------------------------
# Globals and constants
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -71,44 +66,7 @@ def encode_images(format_dict: Dict) -> Dict[str, str]:
is base64-encoded.

"""

# no need for handling of ambiguous bytestrings on Python 3,
# where bytes objects always represent binary data and thus
# base64-encoded.
if py3compat.PY3:
return format_dict

encoded = format_dict.copy()

pngdata = format_dict.get('image/png')
if isinstance(pngdata, bytes):
# make sure we don't double-encode
if not pngdata.startswith(PNG64):
pngdata = b2a_base64(pngdata)
encoded['image/png'] = pngdata.decode('ascii')

jpegdata = format_dict.get('image/jpeg')
if isinstance(jpegdata, bytes):
# make sure we don't double-encode
if not jpegdata.startswith(JPEG64):
jpegdata = b2a_base64(jpegdata)
encoded['image/jpeg'] = jpegdata.decode('ascii')

gifdata = format_dict.get('image/gif')
if isinstance(gifdata, bytes):
# make sure we don't double-encode
if not gifdata.startswith((GIF_64, GIF89_64)):
gifdata = b2a_base64(gifdata)
encoded['image/gif'] = gifdata.decode('ascii')

pdfdata = format_dict.get('application/pdf')
if isinstance(pdfdata, bytes):
# make sure we don't double-encode
if not pdfdata.startswith(PDF64):
pdfdata = b2a_base64(pdfdata)
encoded['application/pdf'] = pdfdata.decode('ascii')

return encoded
return format_dict


def json_clean(obj):
Expand All @@ -135,7 +93,7 @@ def json_clean(obj):

"""
# types that are 'atomic' and ok in json as-is.
atomic_ok = (unicode_type, type(None))
atomic_ok = (str, type(None))

# containers that we need to convert into lists
container_to_list = (tuple, set, types.GeneratorType)
Expand All @@ -160,22 +118,10 @@ def json_clean(obj):
return obj

if isinstance(obj, bytes):
if py3compat.PY3:
# unanmbiguous binary data is base64-encoded
# (this probably should have happened upstream)
return b2a_base64(obj).decode('ascii')
else:
# Python 2 bytestr is ambiguous,
# needs special handling for possible binary bytestrings.
# imperfect workaround: if ascii, assume text.
# otherwise assume binary, base64-encode (py3 behavior).
try:
return obj.decode('ascii')
except UnicodeDecodeError:
return b2a_base64(obj).decode('ascii')
return b2a_base64(obj).decode('ascii')

if isinstance(obj, container_to_list) or (
hasattr(obj, '__iter__') and hasattr(obj, next_attr_name)
hasattr(obj, '__iter__') and hasattr(obj, '__next__')
):
obj = list(obj)

Expand All @@ -187,16 +133,16 @@ def json_clean(obj):
# key collisions after stringification. This can happen with keys like
# True and 'true' or 1 and '1', which collide in JSON.
nkeys = len(obj)
nkeys_collapsed = len(set(map(unicode_type, obj)))
nkeys_collapsed = len(set(map(str, obj)))
if nkeys != nkeys_collapsed:
raise ValueError(
'dict cannot be safely converted to JSON: '
'key collision would lead to dropped values'
)
# If all OK, proceed by making the new dict that will be json-safe
out = {}
for k, v in iteritems(obj):
out[unicode_type(k)] = json_clean(v)
for k, v in iter(obj.items()):
out[str(k)] = json_clean(v)
return out
if isinstance(obj, datetime):
return obj.strftime(ISO8601)
Expand Down
3 changes: 1 addition & 2 deletions nbclient/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import nbformat
import pytest
import xmltodict # type: ignore
from ipython_genutils.py3compat import string_types
from jupyter_client import KernelManager
from jupyter_client.kernelspec import KernelSpecManager
from nbconvert.filters import strip_ansi # type: ignore
Expand Down Expand Up @@ -215,7 +214,7 @@ def normalize_output(output):
if 'image/svg+xml' in output.get('data', {}):
output['data']['image/svg+xml'] = xmltodict.parse(output['data']['image/svg+xml'])
for key, value in output.get('data', {}).items():
if isinstance(value, string_types):
if isinstance(value, str):
output['data'][key] = normalize_base64(value)
if 'traceback' in output:
tb = []
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ python_version = 3.9

[[tool.mypy.overrides]]
module = [
"ipython_genutils.*",
"nbformat.*",
"nest_asyncio.*",
"async_generator.*",
Expand Down