Skip to content

Commit fb46301

Browse files
committed
Handle synchronous message handlers
1 parent ee78349 commit fb46301

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ipykernel/kernelbase.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from functools import partial
99
import itertools
1010
import logging
11+
import inspect
1112
from signal import signal, default_int_handler, SIGINT
1213
import sys
1314
import time
@@ -237,7 +238,9 @@ async def process_control(self, msg):
237238
self.log.error("UNKNOWN CONTROL MESSAGE TYPE: %r", msg_type)
238239
else:
239240
try:
240-
await handler(self.control_stream, idents, msg)
241+
result = handler(self.control_stream, idents, msg)
242+
if inspect.isawaitable(result):
243+
await result
241244
except Exception:
242245
self.log.error("Exception in control handler:", exc_info=True)
243246

@@ -303,7 +306,14 @@ async def dispatch_shell(self, msg):
303306
except Exception:
304307
self.log.debug("Unable to signal in pre_handler_hook:", exc_info=True)
305308
try:
306-
await handler(self.shell_stream, idents, msg)
309+
result = handler(self.shell_stream, idents, msg)
310+
if inspect.isawaitable(result):
311+
await result
312+
else:
313+
warnings.warn(
314+
"Message handlers should be awaitable",
315+
DeprecationWarning,
316+
stacklevel=2)
307317
except Exception:
308318
self.log.error("Exception in message handler:", exc_info=True)
309319
finally:

0 commit comments

Comments
 (0)