Skip to content

Commit e379bf9

Browse files
author
Steven Silvester
authored
Merge pull request #703 from minrk/stdlib-json
avoid use of deprecated zmq.utils.jsonapi
2 parents 0aaa6fa + 460d1a5 commit e379bf9

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

jupyter_client/session.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# Distributed under the terms of the Modified BSD License.
1313
import hashlib
1414
import hmac
15+
import json
1516
import logging
1617
import os
1718
import pickle
@@ -45,7 +46,6 @@
4546
from traitlets.utils.importstring import import_item # type: ignore
4647
from zmq.eventloop.ioloop import IOLoop
4748
from zmq.eventloop.zmqstream import ZMQStream
48-
from zmq.utils import jsonapi
4949

5050
from jupyter_client import protocol_version
5151
from jupyter_client.adapter import adapt
@@ -92,16 +92,18 @@ def squash_unicode(obj):
9292

9393

9494
def json_packer(obj):
95-
return jsonapi.dumps(
95+
return json.dumps(
9696
obj,
9797
default=json_default,
9898
ensure_ascii=False,
9999
allow_nan=False,
100-
)
100+
).encode("utf8")
101101

102102

103103
def json_unpacker(s):
104-
return jsonapi.loads(s)
104+
if isinstance(s, bytes):
105+
s = s.decode("utf8", "replace")
106+
return json.loads(s)
105107

106108

107109
def pickle_packer(o):
@@ -589,12 +591,9 @@ def _check_packers(self) -> None:
589591
try:
590592
packed = pack(msg_list)
591593
except Exception as e:
592-
error_msg = "packer '{packer}' could not serialize a simple message: {e}{jsonmsg}"
593-
if self.packer == "json":
594-
jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod
595-
else:
596-
jsonmsg = ""
597-
raise ValueError(error_msg.format(packer=self.packer, e=e, jsonmsg=jsonmsg)) from e
594+
raise ValueError(
595+
f"packer '{self.packer}' could not serialize a simple message: {e}"
596+
) from e
598597

599598
# ensure packed message is bytes
600599
if not isinstance(packed, bytes):
@@ -605,15 +604,9 @@ def _check_packers(self) -> None:
605604
unpacked = unpack(packed)
606605
assert unpacked == msg_list
607606
except Exception as e:
608-
error_msg = (
609-
"unpacker '{unpacker}' could not handle output from packer '{packer}': {e}{jsonmsg}"
610-
)
611-
if self.packer == "json":
612-
jsonmsg = "\nzmq.utils.jsonapi.jsonmod = %s" % jsonapi.jsonmod
613-
else:
614-
jsonmsg = ""
615607
raise ValueError(
616-
error_msg.format(packer=self.packer, unpacker=self.unpacker, e=e, jsonmsg=jsonmsg)
608+
f"unpacker '{self.unpacker}' could not handle output from packer"
609+
f" '{self.packer}': {e}"
617610
) from e
618611

619612
# check datetime support

0 commit comments

Comments
 (0)