Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jupyter_client warning #6178

Merged
merged 1 commit into from Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 20 additions & 15 deletions notebook/base/zmqhandlers.py
Expand Up @@ -14,7 +14,12 @@
from tornado.websocket import WebSocketHandler, WebSocketClosedError

from jupyter_client.session import Session
from jupyter_client.jsonutil import date_default, extract_dates
try:
from jupyter_client.jsonutil import json_default, extract_dates
except ImportError:
from jupyter_client.jsonutil import (
date_default as json_default, extract_dates
)
from ipython_genutils.py3compat import cast_unicode

from notebook.utils import maybe_future
Expand All @@ -40,7 +45,7 @@ def serialize_binary_message(msg):
# don't modify msg or buffer list in-place
msg = msg.copy()
buffers = list(msg.pop('buffers'))
bmsg = json.dumps(msg, default=date_default).encode('utf8')
bmsg = json.dumps(msg, default=json_default).encode('utf8')
buffers.insert(0, bmsg)
nbufs = len(buffers)
offsets = [4 * (nbufs + 1)]
Expand Down Expand Up @@ -88,15 +93,15 @@ class WebSocketMixin(object):
last_ping = 0
last_pong = 0
stream = None

@property
def ping_interval(self):
"""The interval for websocket keep-alive pings.

Set ws_ping_interval = 0 to disable pings.
"""
return self.settings.get('ws_ping_interval', WS_PING_INTERVAL)

@property
def ping_timeout(self):
"""If no ping is received in this many milliseconds,
Expand All @@ -109,7 +114,7 @@ def ping_timeout(self):

def check_origin(self, origin=None):
"""Check Origin == Host or Access-Control-Allow-Origin.

Tornado >= 4 calls this method automatically, raising 403 if it returns False.
"""

Expand All @@ -120,18 +125,18 @@ def check_origin(self, origin=None):
host = self.request.headers.get("Host")
if origin is None:
origin = self.get_origin()

# If no origin or host header is provided, assume from script
if origin is None or host is None:
return True

origin = origin.lower()
origin_host = urlparse(origin).netloc

# OK if origin matches host
if origin_host == host:
return True

# Check CORS headers
if self.allow_origin:
allow = self.allow_origin == origin
Expand Down Expand Up @@ -193,7 +198,7 @@ def on_pong(self, data):


class ZMQStreamHandler(WebSocketMixin, WebSocketHandler):

if tornado.version_info < (4,1):
"""Backport send_error from tornado 4.1 to 4.0"""
def send_error(self, *args, **kwargs):
Expand All @@ -206,17 +211,17 @@ def send_error(self, *args, **kwargs):
# we can close the connection more gracefully.
self.stream.close()


def _reserialize_reply(self, msg_or_list, channel=None):
"""Reserialize a reply message using JSON.

msg_or_list can be an already-deserialized msg dict or the zmq buffer list.
If it is the zmq list, it will be deserialized with self.session.

This takes the msg list from the ZMQ socket and serializes the result for the websocket.
This method should be used by self._on_zmq_reply to build messages that can
be sent back to the browser.

"""
if isinstance(msg_or_list, dict):
# already unpacked
Expand All @@ -230,7 +235,7 @@ def _reserialize_reply(self, msg_or_list, channel=None):
buf = serialize_binary_message(msg)
return buf
else:
smsg = json.dumps(msg, default=date_default)
smsg = json.dumps(msg, default=json_default)
return cast_unicode(smsg)

def _on_zmq_reply(self, stream, msg_list):
Expand Down
25 changes: 15 additions & 10 deletions notebook/services/contents/handlers.py
Expand Up @@ -11,7 +11,12 @@
from tornado import gen, web

from notebook.utils import maybe_future, url_path_join, url_escape
from jupyter_client.jsonutil import date_default
try:
from jupyter_client.jsonutil import json_default
except ImportError:
from jupyter_client.jsonutil import (
date_default as json_default
)

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

@web.authenticated
@gen.coroutine
Expand All @@ -107,7 +112,7 @@ def get(self, path=''):
if content not in {'0', '1'}:
raise web.HTTPError(400, u'Content %r is invalid' % content)
content = int(content)

model = yield maybe_future(self.contents_manager.get(
path=path, type=type, format=format, content=content,
))
Expand All @@ -125,7 +130,7 @@ def patch(self, path=''):
model = yield maybe_future(cm.update(model, path))
validate_model(model, expect_content=False)
self._finish_model(model)

@gen.coroutine
def _copy(self, copy_from, copy_to=None):
"""Copy a file, optionally specifying a target directory."""
Expand All @@ -146,7 +151,7 @@ def _upload(self, model, path):
self.set_status(201)
validate_model(model, expect_content=False)
self._finish_model(model)

@gen.coroutine
def _new_untitled(self, path, type='', ext=''):
"""Create a new, empty untitled entity"""
Expand All @@ -155,13 +160,13 @@ def _new_untitled(self, path, type='', ext=''):
self.set_status(201)
validate_model(model, expect_content=False)
self._finish_model(model)

@gen.coroutine
def _save(self, model, path):
"""Save an existing file."""
chunk = model.get("chunk", None)
chunk = model.get("chunk", None)
if not chunk or chunk == -1: # Avoid tedious log information
self.log.info(u"Saving file at %s", path)
self.log.info(u"Saving file at %s", path)
model = yield maybe_future(self.contents_manager.save(model, path))
validate_model(model, expect_content=False)
self._finish_model(model)
Expand Down Expand Up @@ -247,7 +252,7 @@ def get(self, path=''):
"""get lists checkpoints for a file"""
cm = self.contents_manager
checkpoints = yield maybe_future(cm.list_checkpoints(path))
data = json.dumps(checkpoints, default=date_default)
data = json.dumps(checkpoints, default=json_default)
self.finish(data)

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