Skip to content

Commit 8c145ad

Browse files
authored
Merge pull request #6178 from martinRenou/fix_jupyter_client_warning
Fix jupyter_client warning
2 parents 65e9701 + 257e6e5 commit 8c145ad

File tree

4 files changed

+77
-57
lines changed

4 files changed

+77
-57
lines changed

notebook/base/zmqhandlers.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
from tornado.websocket import WebSocketHandler, WebSocketClosedError
1515

1616
from jupyter_client.session import Session
17-
from jupyter_client.jsonutil import date_default, extract_dates
17+
try:
18+
from jupyter_client.jsonutil import json_default, extract_dates
19+
except ImportError:
20+
from jupyter_client.jsonutil import (
21+
date_default as json_default, extract_dates
22+
)
1823
from ipython_genutils.py3compat import cast_unicode
1924

2025
from notebook.utils import maybe_future
@@ -40,7 +45,7 @@ def serialize_binary_message(msg):
4045
# don't modify msg or buffer list in-place
4146
msg = msg.copy()
4247
buffers = list(msg.pop('buffers'))
43-
bmsg = json.dumps(msg, default=date_default).encode('utf8')
48+
bmsg = json.dumps(msg, default=json_default).encode('utf8')
4449
buffers.insert(0, bmsg)
4550
nbufs = len(buffers)
4651
offsets = [4 * (nbufs + 1)]
@@ -88,15 +93,15 @@ class WebSocketMixin(object):
8893
last_ping = 0
8994
last_pong = 0
9095
stream = None
91-
96+
9297
@property
9398
def ping_interval(self):
9499
"""The interval for websocket keep-alive pings.
95-
100+
96101
Set ws_ping_interval = 0 to disable pings.
97102
"""
98103
return self.settings.get('ws_ping_interval', WS_PING_INTERVAL)
99-
104+
100105
@property
101106
def ping_timeout(self):
102107
"""If no ping is received in this many milliseconds,
@@ -109,7 +114,7 @@ def ping_timeout(self):
109114

110115
def check_origin(self, origin=None):
111116
"""Check Origin == Host or Access-Control-Allow-Origin.
112-
117+
113118
Tornado >= 4 calls this method automatically, raising 403 if it returns False.
114119
"""
115120

@@ -120,18 +125,18 @@ def check_origin(self, origin=None):
120125
host = self.request.headers.get("Host")
121126
if origin is None:
122127
origin = self.get_origin()
123-
128+
124129
# If no origin or host header is provided, assume from script
125130
if origin is None or host is None:
126131
return True
127-
132+
128133
origin = origin.lower()
129134
origin_host = urlparse(origin).netloc
130-
135+
131136
# OK if origin matches host
132137
if origin_host == host:
133138
return True
134-
139+
135140
# Check CORS headers
136141
if self.allow_origin:
137142
allow = self.allow_origin == origin
@@ -193,7 +198,7 @@ def on_pong(self, data):
193198

194199

195200
class ZMQStreamHandler(WebSocketMixin, WebSocketHandler):
196-
201+
197202
if tornado.version_info < (4,1):
198203
"""Backport send_error from tornado 4.1 to 4.0"""
199204
def send_error(self, *args, **kwargs):
@@ -206,17 +211,17 @@ def send_error(self, *args, **kwargs):
206211
# we can close the connection more gracefully.
207212
self.stream.close()
208213

209-
214+
210215
def _reserialize_reply(self, msg_or_list, channel=None):
211216
"""Reserialize a reply message using JSON.
212217
213218
msg_or_list can be an already-deserialized msg dict or the zmq buffer list.
214219
If it is the zmq list, it will be deserialized with self.session.
215-
220+
216221
This takes the msg list from the ZMQ socket and serializes the result for the websocket.
217222
This method should be used by self._on_zmq_reply to build messages that can
218223
be sent back to the browser.
219-
224+
220225
"""
221226
if isinstance(msg_or_list, dict):
222227
# already unpacked
@@ -230,7 +235,7 @@ def _reserialize_reply(self, msg_or_list, channel=None):
230235
buf = serialize_binary_message(msg)
231236
return buf
232237
else:
233-
smsg = json.dumps(msg, default=date_default)
238+
smsg = json.dumps(msg, default=json_default)
234239
return cast_unicode(smsg)
235240

236241
def _on_zmq_reply(self, stream, msg_list):

notebook/services/contents/handlers.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
from tornado import gen, web
1212

1313
from notebook.utils import maybe_future, url_path_join, url_escape
14-
from jupyter_client.jsonutil import date_default
14+
try:
15+
from jupyter_client.jsonutil import json_default
16+
except ImportError:
17+
from jupyter_client.jsonutil import (
18+
date_default as json_default
19+
)
1520

1621
from notebook.base.handlers import (
1722
IPythonHandler, APIHandler, path_regex,
@@ -85,7 +90,7 @@ def _finish_model(self, model, location=True):
8590
self.set_header('Location', location)
8691
self.set_header('Last-Modified', model['last_modified'])
8792
self.set_header('Content-Type', 'application/json')
88-
self.finish(json.dumps(model, default=date_default))
93+
self.finish(json.dumps(model, default=json_default))
8994

9095
@web.authenticated
9196
@gen.coroutine
@@ -107,7 +112,7 @@ def get(self, path=''):
107112
if content not in {'0', '1'}:
108113
raise web.HTTPError(400, u'Content %r is invalid' % content)
109114
content = int(content)
110-
115+
111116
model = yield maybe_future(self.contents_manager.get(
112117
path=path, type=type, format=format, content=content,
113118
))
@@ -125,7 +130,7 @@ def patch(self, path=''):
125130
model = yield maybe_future(cm.update(model, path))
126131
validate_model(model, expect_content=False)
127132
self._finish_model(model)
128-
133+
129134
@gen.coroutine
130135
def _copy(self, copy_from, copy_to=None):
131136
"""Copy a file, optionally specifying a target directory."""
@@ -146,7 +151,7 @@ def _upload(self, model, path):
146151
self.set_status(201)
147152
validate_model(model, expect_content=False)
148153
self._finish_model(model)
149-
154+
150155
@gen.coroutine
151156
def _new_untitled(self, path, type='', ext=''):
152157
"""Create a new, empty untitled entity"""
@@ -155,13 +160,13 @@ def _new_untitled(self, path, type='', ext=''):
155160
self.set_status(201)
156161
validate_model(model, expect_content=False)
157162
self._finish_model(model)
158-
163+
159164
@gen.coroutine
160165
def _save(self, model, path):
161166
"""Save an existing file."""
162-
chunk = model.get("chunk", None)
167+
chunk = model.get("chunk", None)
163168
if not chunk or chunk == -1: # Avoid tedious log information
164-
self.log.info(u"Saving file at %s", path)
169+
self.log.info(u"Saving file at %s", path)
165170
model = yield maybe_future(self.contents_manager.save(model, path))
166171
validate_model(model, expect_content=False)
167172
self._finish_model(model)
@@ -247,7 +252,7 @@ def get(self, path=''):
247252
"""get lists checkpoints for a file"""
248253
cm = self.contents_manager
249254
checkpoints = yield maybe_future(cm.list_checkpoints(path))
250-
data = json.dumps(checkpoints, default=date_default)
255+
data = json.dumps(checkpoints, default=json_default)
251256
self.finish(data)
252257

253258
@web.authenticated
@@ -256,7 +261,7 @@ def post(self, path=''):
256261
"""post creates a new checkpoint"""
257262
cm = self.contents_manager
258263
checkpoint = yield maybe_future(cm.create_checkpoint(path))
259-
data = json.dumps(checkpoint, default=date_default)
264+
data = json.dumps(checkpoint, default=json_default)
260265
location = url_path_join(self.base_url, 'api/contents',
261266
url_escape(path), 'checkpoints', url_escape(checkpoint['id']))
262267
self.set_header('Location', location)

0 commit comments

Comments
 (0)