From 460d1a5e9249613f9ef6bf1c1aa653c9d54fa1d5 Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 29 Sep 2021 09:03:32 +0200 Subject: [PATCH] avoid use of deprecated zmq.utils.jsonapi it doesn't offer anything useful in 2021 --- jupyter_client/session.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/jupyter_client/session.py b/jupyter_client/session.py index 8f4d4d7cd..f0786a517 100644 --- a/jupyter_client/session.py +++ b/jupyter_client/session.py @@ -12,6 +12,7 @@ # Distributed under the terms of the Modified BSD License. import hashlib import hmac +import json import logging import os import pickle @@ -45,7 +46,6 @@ from traitlets.utils.importstring import import_item # type: ignore from zmq.eventloop.ioloop import IOLoop from zmq.eventloop.zmqstream import ZMQStream -from zmq.utils import jsonapi from jupyter_client import protocol_version from jupyter_client.adapter import adapt @@ -92,16 +92,18 @@ def squash_unicode(obj): def json_packer(obj): - return jsonapi.dumps( + return json.dumps( obj, default=json_default, ensure_ascii=False, allow_nan=False, - ) + ).encode("utf8") def json_unpacker(s): - return jsonapi.loads(s) + if isinstance(s, bytes): + s = s.decode("utf8", "replace") + return json.loads(s) def pickle_packer(o): @@ -589,12 +591,9 @@ def _check_packers(self) -> None: try: packed = pack(msg_list) except Exception as e: - error_msg = "packer '{packer}' could not serialize a simple message: {e}{jsonmsg}" - if self.packer == "json": - jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod - else: - jsonmsg = "" - raise ValueError(error_msg.format(packer=self.packer, e=e, jsonmsg=jsonmsg)) from e + raise ValueError( + f"packer '{self.packer}' could not serialize a simple message: {e}" + ) from e # ensure packed message is bytes if not isinstance(packed, bytes): @@ -605,15 +604,9 @@ def _check_packers(self) -> None: unpacked = unpack(packed) assert unpacked == msg_list except Exception as e: - error_msg = ( - "unpacker '{unpacker}' could not handle output from packer '{packer}': {e}{jsonmsg}" - ) - if self.packer == "json": - jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod - else: - jsonmsg = "" raise ValueError( - error_msg.format(packer=self.packer, unpacker=self.unpacker, e=e, jsonmsg=jsonmsg) + f"unpacker '{self.unpacker}' could not handle output from packer" + f" '{self.packer}': {e}" ) from e # check datetime support