From 872a1e8bd87947902cc2cee947bbf181b09bb8d1 Mon Sep 17 00:00:00 2001 From: RK Date: Mon, 1 Feb 2021 11:15:41 +0000 Subject: [PATCH] Issue #12786: Implement hook for Comm messages --- ipykernel/ipkernel.py | 15 +++++++++++++-- ipykernel/kernelbase.py | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ipykernel/ipkernel.py b/ipykernel/ipkernel.py index 6943deacc..acbc64b82 100644 --- a/ipykernel/ipkernel.py +++ b/ipykernel/ipkernel.py @@ -68,6 +68,8 @@ def _user_ns_changed(self, change): _sys_raw_input = Any() _sys_eval_input = Any() + comm_msg_types = [ 'comm_open', 'comm_msg', 'comm_close' ] + def __init__(self, **kwargs): super(IPythonKernel, self).__init__(**kwargs) @@ -95,8 +97,7 @@ def __init__(self, **kwargs): self.comm_manager = CommManager(parent=self, kernel=self) self.shell.configurables.append(self.comm_manager) - comm_msg_types = [ 'comm_open', 'comm_msg', 'comm_close' ] - for msg_type in comm_msg_types: + for msg_type in self.comm_msg_types: self.shell_handlers[msg_type] = getattr(self.comm_manager, msg_type) if _use_appnope() and self._darwin_app_nap: @@ -566,6 +567,16 @@ def do_clear(self): self.shell.reset(False) return dict(status='ok') + def should_dispatch_immediately(self, msg): + try: + msg_type = msg['header']['msg_type'] + if msg_type in self.comm_msg_types: + return True + except ValueError: + pass + + return False + # This exists only for backwards compatibility - use IPythonKernel instead diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index 36f15f5a4..7287b4fd6 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -300,7 +300,7 @@ def should_handle(self, stream, msg, idents): return False return True - async def dispatch_shell(self, msg): + async def dispatch_shell(self, msg, idents=None): """dispatch shell requests""" # flush control queue before handling shell requests