File tree 3 files changed +19
-6
lines changed
3 files changed +19
-6
lines changed Original file line number Diff line number Diff line change 3
3
# Distributed under the terms of the Modified BSD License.
4
4
import re
5
5
import warnings
6
+ from binascii import b2a_base64
6
7
from datetime import datetime
7
8
from typing import Optional
8
9
from typing import Union
@@ -91,9 +92,21 @@ def squash_dates(obj):
91
92
92
93
93
94
def date_default (obj ):
94
- """default function for packing datetime objects in JSON."""
95
+ """DEPRECATED: Use jupyter_client.jsonutil.json_default"""
96
+ warnings .warn (
97
+ "date_default is deprecated since jupyter_client 7.0.0."
98
+ " Use jupyter_client.jsonutil.json_default." ,
99
+ stacklevel = 2 ,
100
+ )
101
+ return json_default (obj )
102
+
103
+
104
+ def json_default (obj ):
105
+ """default function for packing objects in JSON."""
95
106
if isinstance (obj , datetime ):
96
107
obj = _ensure_tzinfo (obj )
97
- return obj .isoformat ().replace ("+00:00" , "Z" )
108
+ return obj .isoformat ().replace ('+00:00' , 'Z' )
109
+ elif isinstance (obj , bytes ):
110
+ return b2a_base64 (obj ).decode ('ascii' )
98
111
else :
99
112
raise TypeError ("%r is not JSON serializable" % obj )
Original file line number Diff line number Diff line change 49
49
50
50
from jupyter_client import protocol_version
51
51
from jupyter_client .adapter import adapt
52
- from jupyter_client .jsonutil import date_default
53
52
from jupyter_client .jsonutil import extract_dates
53
+ from jupyter_client .jsonutil import json_default
54
54
from jupyter_client .jsonutil import squash_dates
55
55
56
56
@@ -94,7 +94,7 @@ def squash_unicode(obj):
94
94
def json_packer (obj ):
95
95
return jsonapi .dumps (
96
96
obj ,
97
- default = date_default ,
97
+ default = json_default ,
98
98
ensure_ascii = False ,
99
99
allow_nan = False ,
100
100
)
Original file line number Diff line number Diff line change @@ -65,14 +65,14 @@ def test_parse_ms_precision():
65
65
assert isinstance (parsed , str )
66
66
67
67
68
- def test_date_default ():
68
+ def test_json_default ():
69
69
naive = datetime .datetime .now ()
70
70
local = tzoffset ("Local" , - 8 * 3600 )
71
71
other = tzoffset ("Other" , 2 * 3600 )
72
72
data = dict (naive = naive , utc = utcnow (), withtz = naive .replace (tzinfo = other ))
73
73
with mock .patch .object (jsonutil , "tzlocal" , lambda : local ):
74
74
with pytest .deprecated_call (match = "Please add timezone info" ):
75
- jsondata = json .dumps (data , default = jsonutil .date_default )
75
+ jsondata = json .dumps (data , default = jsonutil .json_default )
76
76
assert "Z" in jsondata
77
77
assert jsondata .count ("Z" ) == 1
78
78
extracted = jsonutil .extract_dates (json .loads (jsondata ))
You can’t perform that action at this time.
0 commit comments