Skip to content

Commit a6085b9

Browse files
author
Alan
committed
Should return result of orjson.loads.
1 parent 4518a01 commit a6085b9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

jupyter_client/session.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import functools
1717
import hashlib
1818
import hmac
19+
import importlib.util
1920
import json
2021
import logging
2122
import os
@@ -127,12 +128,13 @@ def json_unpacker(s: str | bytes) -> t.Any:
127128
return json.loads(s)
128129

129130

130-
try:
131+
orjson = None
132+
orjson_packer, orjson_unpacker = json_packer, json_unpacker
133+
134+
if importlib.util.find_spec("orjson"):
131135
import orjson
132-
except ModuleNotFoundError:
133-
orjson = None
134-
orjson_packer, orjson_unpacker = json_packer, json_unpacker
135-
else:
136+
137+
assert orjson
136138

137139
def orjson_packer(
138140
obj: t.Any, *, option: int | None = orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z
@@ -147,7 +149,7 @@ def orjson_packer(
147149
def orjson_unpacker(s: str | bytes) -> t.Any:
148150
"""Convert a json bytes or string to an object using orjson with fallback to json_unpacker."""
149151
try:
150-
orjson.loads(s)
152+
return orjson.loads(s)
151153
except Exception:
152154
pass
153155
return json_unpacker(s)

0 commit comments

Comments
 (0)