Skip to content

Commit e6ff9ff

Browse files
committed
Handle synchronous message handlers
1 parent 976b762 commit e6ff9ff

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

ipykernel/kernelbase.py

Lines changed: 17 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,14 @@ 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
244+
else:
245+
warnings.warn(
246+
"Message handlers should be awaitable",
247+
DeprecationWarning,
248+
stacklevel=2)
241249
except Exception:
242250
self.log.error("Exception in control handler:", exc_info=True)
243251

@@ -304,7 +312,14 @@ async def dispatch_shell(self, msg):
304312
except Exception:
305313
self.log.debug("Unable to signal in pre_handler_hook:", exc_info=True)
306314
try:
307-
await handler(self.shell_stream, idents, msg)
315+
result = handler(self.shell_stream, idents, msg)
316+
if inspect.isawaitable(result):
317+
await result
318+
else:
319+
warnings.warn(
320+
"Message handlers should be awaitable",
321+
DeprecationWarning,
322+
stacklevel=2)
308323
except Exception:
309324
self.log.error("Exception in message handler:", exc_info=True)
310325
finally:

0 commit comments

Comments
 (0)