8000 Fix NaN and Inf serialization · jupyter/jupyter_client@6daba70 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 6daba70

Browse files
committed
Fix NaN and Inf serialization
1 parent 5e3ca02 commit 6daba70

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

jupyter_client/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# Distributed under the terms of the Modified BSD License.
1313
import hashlib
1414
import hmac
15-
import json
1615
import logging
1716
import os
1817
import pickle
@@ -27,6 +26,7 @@
2726
compare_digest,
2827
) # We are using compare_digest to limit the surface of timing attacks
2928

29+
import simplejson as json
3030
import zmq
3131
from traitlets import Any # type: ignore
3232
from traitlets import Bool
@@ -96,7 +96,7 @@ def json_packer(obj):
9696
obj,
9797
default=json_default,
9898
ensure_ascii=False,
99-
allow_nan=False,
99+
ignore_nan=True,
100100
).encode("utf8")
101101

102102

jupyter_client/tests/test_jsonutil.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the terms of the Modified BSD License.
44
import datetime
55
import json
6+
import math
67
import numbers
78
from datetime import timedelta
89
from unittest import mock
@@ -12,6 +13,7 @@
1213
from dateutil.tz import tzoffset
1314

1415
from jupyter_client import jsonutil
16+
from jupyter_client.session import json_packer
1517
from jupyter_client.session import utcnow
1618

1719
REFERENCE_DATETIME = datetime.datetime(2013, 7, 3, 16, 34, 52, 249482, tzlocal())
@@ -128,3 +130,9 @@ def test_json_default():
128130
out = json.loads(json.dumps(val, default=jsonutil.json_default))
129131
# validate our cleanup
130132
assert out == jval
133+
134+
135+
def test_json_packer():
136+
assert json_packer(math.nan) == b'null'
137+
assert json_packer(math.inf) == b'null'
138+
assert json_packer(-math.inf) == b'null'

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ jupyter_core>=4.6.0
33
nest-asyncio>=1.5
44
python-dateutil>=2.1
55
pyzmq>=13
6+
simplejson
67
tornado>=4.1
78
traitlets

0 commit comments

Comments
 (0)
0