From af6042fd27f9528b9f40d0c6062ae3afd2c56792 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Fri, 11 Mar 2022 09:20:51 +0100 Subject: [PATCH] Drop ipython_genutils --- nbclient/jsonutil.py | 68 ++++------------------------------- nbclient/tests/test_client.py | 3 +- pyproject.toml | 1 - 3 files changed, 8 insertions(+), 64 deletions(-) diff --git a/nbclient/jsonutil.py b/nbclient/jsonutil.py index 9a6e0a74..e67ffd5f 100644 --- a/nbclient/jsonutil.py +++ b/nbclient/jsonutil.py @@ -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 # ----------------------------------------------------------------------------- @@ -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): @@ -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) @@ -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) @@ -187,7 +133,7 @@ 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: ' @@ -195,8 +141,8 @@ def json_clean(obj): ) # 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) diff --git a/nbclient/tests/test_client.py b/nbclient/tests/test_client.py index 8be9e1b9..50085963 100644 --- a/nbclient/tests/test_client.py +++ b/nbclient/tests/test_client.py @@ -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 @@ -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 = [] diff --git a/pyproject.toml b/pyproject.toml index 77b52db1..6bfca656 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,7 +37,6 @@ python_version = 3.9 [[tool.mypy.overrides]] module = [ - "ipython_genutils.*", "nbformat.*", "nest_asyncio.*", "async_generator.*",