From 878f297c0e89f5710be6060e7dab0856da1f87a3 Mon Sep 17 00:00:00 2001 From: Min RK Date: Sun, 10 Mar 2024 17:27:18 +0100 Subject: [PATCH] avoid jupyter_client.session monkeypatch at import time patch in App, Client classes this will hopefully shift to Session config in the future. --- ipyparallel/apps/baseapp.py | 3 +++ ipyparallel/client/client.py | 1 + ipyparallel/controller/heartmonitor.py | 2 ++ ipyparallel/controller/scheduler.py | 1 + ipyparallel/engine/app.py | 2 ++ ipyparallel/engine/nanny.py | 1 + ipyparallel/tests/test_db.py | 1 + ipyparallel/util.py | 21 +++++++-------------- pyproject.toml | 2 +- 9 files changed, 19 insertions(+), 15 deletions(-) diff --git a/ipyparallel/apps/baseapp.py b/ipyparallel/apps/baseapp.py index e6ebaac6f..2c58338db 100644 --- a/ipyparallel/apps/baseapp.py +++ b/ipyparallel/apps/baseapp.py @@ -17,6 +17,8 @@ from traitlets import Bool, Instance, Unicode, default, observe from traitlets.config.application import LevelFormatter, catch_config_error +from ipyparallel import util + from .._version import __version__ # FIXME: CUnicode is needed for cli parsing @@ -136,6 +138,7 @@ def _default_session(self): @catch_config_error def initialize(self, argv=None): """initialize the app""" + util._disable_session_extract_dates() self.init_config_from_env() super().initialize(argv) self.init_deprecated_config() diff --git a/ipyparallel/client/client.py b/ipyparallel/client/client.py index 73c821b08..d4f4285b9 100644 --- a/ipyparallel/client/client.py +++ b/ipyparallel/client/client.py @@ -570,6 +570,7 @@ def __init__( ) raise ValueError(msg.format(exc.message)) + util._disable_session_extract_dates() self.session = Session(**extra_args) self._query_socket = self._context.socket(zmq.DEALER) diff --git a/ipyparallel/controller/heartmonitor.py b/ipyparallel/controller/heartmonitor.py index 0862bda79..b6205c86a 100755 --- a/ipyparallel/controller/heartmonitor.py +++ b/ipyparallel/controller/heartmonitor.py @@ -18,6 +18,7 @@ from zmq.devices import ThreadDevice, ThreadMonitoredQueue from zmq.eventloop.zmqstream import ZMQStream +from ipyparallel import util from ipyparallel.util import bind, connect, log_errors, set_hwm @@ -120,6 +121,7 @@ class HeartMonitor(LoggingConfigurable): @default("session") def _default_session(self): + util._disable_session_extract_dates() return Session(parent=self) loop = Instance(ioloop.IOLoop) diff --git a/ipyparallel/controller/scheduler.py b/ipyparallel/controller/scheduler.py index c7e13b5ec..b279b7e2d 100644 --- a/ipyparallel/controller/scheduler.py +++ b/ipyparallel/controller/scheduler.py @@ -46,6 +46,7 @@ def _default_loop(self): @default("session") def _default_session(self): + util._disable_session_extract_dates() return jupyter_client.session.Session(parent=self) client_stream = Instance( diff --git a/ipyparallel/engine/app.py b/ipyparallel/engine/app.py index fdf2e3337..9a2f50376 100755 --- a/ipyparallel/engine/app.py +++ b/ipyparallel/engine/app.py @@ -37,6 +37,7 @@ from traitlets.config import Config from zmq.eventloop import zmqstream +from ipyparallel import util from ipyparallel.apps.baseapp import ( BaseParallelApplication, base_aliases, @@ -405,6 +406,7 @@ def load_connection_file(self): config.Session.packer = d['pack'] config.Session.unpacker = d['unpack'] + util._disable_session_extract_dates() self.session = Session(parent=self) self.log.debug("Config changed:") diff --git a/ipyparallel/engine/nanny.py b/ipyparallel/engine/nanny.py index 876efc02c..deb8a248f 100644 --- a/ipyparallel/engine/nanny.py +++ b/ipyparallel/engine/nanny.py @@ -68,6 +68,7 @@ def __init__( self.curve_secretkey = curve_secretkey self.config = config self.pipe = pipe + util._disable_session_extract_dates() self.session = Session(config=self.config) self.log = local_logger(f"{self.__class__.__name__}.{engine_id}", log_level) diff --git a/ipyparallel/tests/test_db.py b/ipyparallel/tests/test_db.py index dc9eeef99..f0115ae38 100644 --- a/ipyparallel/tests/test_db.py +++ b/ipyparallel/tests/test_db.py @@ -21,6 +21,7 @@ class TaskDBTest: def setUp(self): + util._disable_session_extract_dates() self.session = Session() self.db = self.create_db() self.load_records(16) diff --git a/ipyparallel/util.py b/ipyparallel/util.py index d94b59e7e..d5fa1ed80 100644 --- a/ipyparallel/util.py +++ b/ipyparallel/util.py @@ -24,6 +24,7 @@ from IPython import get_ipython from IPython.core.profiledir import ProfileDir, ProfileDirError from IPython.paths import get_ipython_dir +from jupyter_client import session from jupyter_client.localinterfaces import is_public_ip, localhost, public_ips from tornado.ioloop import IOLoop from traitlets.log import get_logger @@ -599,21 +600,13 @@ def _v(version_s): return tuple(int(s) for s in re.findall(r"\d+", version_s)) -def _patch_jupyter_client_dates(): - """Monkeypatch jupyter_client.extract_dates to be nondestructive wrt timezone info""" - import jupyter_client - - if _v(jupyter_client.__version__) < _v('5.0'): - from jupyter_client import session - - if hasattr(session, '_save_extract_dates'): - return - session._save_extract_dates = session.extract_dates - session.extract_dates = extract_dates - +@lru_cache() +def _disable_session_extract_dates(): + """Monkeypatch jupyter_client.extract_dates to be a no-op -# FIXME: remove patch when we require jupyter_client 5.0 -_patch_jupyter_client_dates() + avoids performance problem parsing unused timestamp strings + """ + session.extract_dates = lambda obj: obj def progress(*args, widget=None, **kwargs): diff --git a/pyproject.toml b/pyproject.toml index 2db37247f..6daf87481 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,7 @@ dependencies = [ "pyzmq>=18", "traitlets>=4.3", "ipython>=4", - "jupyter_client", + "jupyter_client>=5", "ipykernel>=4.4", "tornado>=5.1", "psutil",