From cee81d47a2992756d9bbe8760bf3d42a4f3c789a Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 9 Sep 2021 10:53:49 +0200 Subject: [PATCH] Fix jupyter_client warning --- jupyter_server/base/zmqhandlers.py | 11 ++++++++--- jupyter_server/services/contents/handlers.py | 13 +++++++++---- jupyter_server/services/kernels/handlers.py | 19 ++++++++++++------- jupyter_server/services/sessions/handlers.py | 15 ++++++++++----- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/jupyter_server/base/zmqhandlers.py b/jupyter_server/base/zmqhandlers.py index b7571f97d0..7ba5061778 100644 --- a/jupyter_server/base/zmqhandlers.py +++ b/jupyter_server/base/zmqhandlers.py @@ -9,7 +9,12 @@ import tornado from ipython_genutils.py3compat import cast_unicode -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 jupyter_client.jsonutil import extract_dates from jupyter_client.session import Session from tornado import ioloop @@ -39,7 +44,7 @@ def serialize_binary_message(msg): buffers = list(msg.pop("buffers")) if sys.version_info < (3, 4): buffers = [x.tobytes() for x in 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)] @@ -227,7 +232,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): diff --git a/jupyter_server/services/contents/handlers.py b/jupyter_server/services/contents/handlers.py index b79ba1eb8f..bf8666008f 100644 --- a/jupyter_server/services/contents/handlers.py +++ b/jupyter_server/services/contents/handlers.py @@ -6,7 +6,12 @@ # Distributed under the terms of the Modified BSD License. import json -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 tornado import web from jupyter_server.base.handlers import APIHandler @@ -77,7 +82,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 async def get(self, path=""): @@ -237,7 +242,7 @@ async def get(self, path=""): """get lists checkpoints for a file""" cm = self.contents_manager checkpoints = await ensure_async(cm.list_checkpoints(path)) - data = json.dumps(checkpoints, default=date_default) + data = json.dumps(checkpoints, default=json_default) self.finish(data) @web.authenticated @@ -245,7 +250,7 @@ async def post(self, path=""): """post creates a new checkpoint""" cm = self.contents_manager checkpoint = await ensure_async(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", diff --git a/jupyter_server/services/kernels/handlers.py b/jupyter_server/services/kernels/handlers.py index efc6bbd6de..5fd5b1e126 100644 --- a/jupyter_server/services/kernels/handlers.py +++ b/jupyter_server/services/kernels/handlers.py @@ -10,7 +10,12 @@ from ipython_genutils.py3compat import cast_unicode from jupyter_client import protocol_version as client_protocol_version -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 tornado import gen from tornado import web from tornado.concurrent import Future @@ -29,7 +34,7 @@ class MainKernelHandler(APIHandler): async def get(self): km = self.kernel_manager kernels = await ensure_async(km.list_kernels()) - self.finish(json.dumps(kernels, default=date_default)) + self.finish(json.dumps(kernels, default=json_default)) @web.authenticated async def post(self): @@ -45,7 +50,7 @@ async def post(self): location = url_path_join(self.base_url, "api", "kernels", url_escape(kernel_id)) self.set_header("Location", location) self.set_status(201) - self.finish(json.dumps(model, default=date_default)) + self.finish(json.dumps(model, default=json_default)) class KernelHandler(APIHandler): @@ -53,7 +58,7 @@ class KernelHandler(APIHandler): async def get(self, kernel_id): km = self.kernel_manager model = await ensure_async(km.kernel_model(kernel_id)) - self.finish(json.dumps(model, default=date_default)) + self.finish(json.dumps(model, default=json_default)) @web.authenticated async def delete(self, kernel_id): @@ -79,7 +84,7 @@ async def post(self, kernel_id, action): self.set_status(500) else: model = await ensure_async(km.kernel_model(kernel_id)) - self.write(json.dumps(model, default=date_default)) + self.write(json.dumps(model, default=json_default)) self.finish() @@ -443,7 +448,7 @@ def write_stderr(error_message): "stream", content={"text": error_message + "\n", "name": "stderr"}, parent=parent ) msg["channel"] = "iopub" - self.write_message(json.dumps(msg, default=date_default)) + self.write_message(json.dumps(msg, default=json_default)) channel = getattr(stream, "channel", None) msg_type = msg["header"]["msg_type"] @@ -610,7 +615,7 @@ def _send_status_message(self, status): iopub.flush() msg = self.session.msg("status", {"execution_state": status}) msg["channel"] = "iopub" - self.write_message(json.dumps(msg, default=date_default)) + self.write_message(json.dumps(msg, default=json_default)) def on_kernel_restarted(self): logging.warn("kernel %s restarted", self.kernel_id) diff --git a/jupyter_server/services/sessions/handlers.py b/jupyter_server/services/sessions/handlers.py index 07230cfb8d..c2f8efd783 100644 --- a/jupyter_server/services/sessions/handlers.py +++ b/jupyter_server/services/sessions/handlers.py @@ -6,7 +6,12 @@ # Distributed under the terms of the Modified BSD License. import json -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 jupyter_client.kernelspec import NoSuchKernel from tornado import web @@ -21,7 +26,7 @@ async def get(self): # Return a list of running sessions sm = self.session_manager sessions = await ensure_async(sm.list_sessions()) - self.finish(json.dumps(sessions, default=date_default)) + self.finish(json.dumps(sessions, default=json_default)) @web.authenticated async def post(self): @@ -79,7 +84,7 @@ async def post(self): location = url_path_join(self.base_url, "api", "sessions", model["id"]) self.set_header("Location", location) self.set_status(201) - self.finish(json.dumps(model, default=date_default)) + self.finish(json.dumps(model, default=json_default)) class SessionHandler(APIHandler): @@ -88,7 +93,7 @@ async def get(self, session_id): # Returns the JSON model for a single session sm = self.session_manager model = await sm.get_session(session_id=session_id) - self.finish(json.dumps(model, default=date_default)) + self.finish(json.dumps(model, default=json_default)) @web.authenticated async def patch(self, session_id): @@ -142,7 +147,7 @@ async def patch(self, session_id): # kernel_id changed because we got a new kernel # shutdown the old one await ensure_async(km.shutdown_kernel(before["kernel"]["id"])) - self.finish(json.dumps(model, default=date_default)) + self.finish(json.dumps(model, default=json_default)) @web.authenticated async def delete(self, session_id):