Skip to content

Commit 7b01b09

Browse files
committed
do_[*] functions do not have to be async
1 parent 583dc40 commit 7b01b09

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

ipykernel/ipkernel.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ async def run_cell(*args, **kwargs):
384384

385385
return reply_content
386386

387-
async def do_complete(self, code, cursor_pos):
387+
def do_complete(self, code, cursor_pos):
388388
if _use_experimental_60_completion and self.use_experimental_completions:
389389
return self._experimental_do_complete(code, cursor_pos)
390390

@@ -404,7 +404,7 @@ async def do_complete(self, code, cursor_pos):
404404
'status' : 'ok'}
405405

406406
async def do_debug_request(self, msg):
407-
return self.debugger.process_request(msg)
407+
return await self.debugger.process_request(msg)
408408

409409
def _experimental_do_complete(self, code, cursor_pos):
410410
"""
@@ -440,7 +440,7 @@ def _experimental_do_complete(self, code, cursor_pos):
440440
'metadata': {_EXPERIMENTAL_KEY_NAME: comps},
441441
'status': 'ok'}
442442

443-
async def do_inspect(self, code, cursor_pos, detail_level=0):
443+
def do_inspect(self, code, cursor_pos, detail_level=0):
444444
name = token_at_cursor(code, cursor_pos)
445445

446446
reply_content = {'status' : 'ok'}
@@ -461,7 +461,7 @@ async def do_inspect(self, code, cursor_pos, detail_level=0):
461461

462462
return reply_content
463463

464-
async def do_history(self, hist_access_type, output, raw, session=0, start=0,
464+
def do_history(self, hist_access_type, output, raw, session=0, start=0,
465465
stop=None, n=None, pattern=None, unique=False):
466466
if hist_access_type == 'tail':
467467
hist = self.shell.history_manager.get_tail(n, raw=raw, output=output,
@@ -482,11 +482,11 @@ async def do_history(self, hist_access_type, output, raw, session=0, start=0,
482482
'history' : list(hist),
483483
}
484484

485-
async def do_shutdown(self, restart):
485+
def do_shutdown(self, restart):
486486
self.shell.exit_now = True
487487
return dict(status='ok', restart=restart)
488488

489-
async def do_is_complete(self, code):
489+
def do_is_complete(self, code):
490490
transformer_manager = getattr(self.shell, 'input_transformer_manager', None)
491491
if transformer_manager is None:
492492
# input_splitter attribute is deprecated
@@ -497,7 +497,7 @@ async def do_is_complete(self, code):
497497
r['indent'] = ' ' * indent_spaces
498498
return r
499499

500-
async def do_apply(self, content, bufs, msg_id, reply_metadata):
500+
def do_apply(self, content, bufs, msg_id, reply_metadata):
501501
from .serialize import serialize_object, unpack_apply_message
502502
shell = self.shell
503503
try:
@@ -552,7 +552,7 @@ async def do_apply(self, content, bufs, msg_id, reply_metadata):
552552

553553
return reply_content, result_buf
554554

555-
async def do_clear(self):
555+
def do_clear(self):
556556
self.shell.reset(False)
557557
return dict(status='ok')
558558

ipykernel/kernelbase.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,12 @@ async def execute_request(self, stream, ident, parent):
566566
self.execution_count += 1
567567
self._publish_execute_input(code, parent, self.execution_count)
568568

569-
reply_content = await self.do_execute(
569+
reply_content = self.do_execute(
570570
code, silent, store_history,
571571
user_expressions, allow_stdin,
572572
)
573+
if inspect.isawaitable(reply_content):
574+
reply_content = await reply_content
573575

574576
# Flush output before sending the reply.
575577
sys.stdout.flush()
@@ -593,7 +595,7 @@ async def execute_request(self, stream, ident, parent):
593595
if not silent and reply_msg['content']['status'] == 'error' and stop_on_error:
594596
await self._abort_queues()
595597

596-
async def do_execute(self, code, silent, store_history=True,
598+
def do_execute(self, code, silent, store_history=True,
597599
user_expressions=None, allow_stdin=False):
598600
"""Execute user code. Must be overridden by subclasses.
599601
"""
@@ -604,11 +606,14 @@ async def complete_request(self, stream, ident, parent):
604606
code = content['code']
605607
cursor_pos = content['cursor_pos']
606608

607-
matches = await self.do_complete(code, cursor_pos)
609+
matches = self.do_complete(code, cursor_pos)
610+
if inspect.isawaitable(matches):
611+
matches = await matches
612+
608613
matches = json_clean(matches)
609614
self.session.send(stream, "complete_reply", matches, parent, ident)
610615

611-
async def do_complete(self, code, cursor_pos):
616+
def do_complete(self, code, cursor_pos):
612617
"""Override in subclasses to find completions.
613618
"""
614619
return {'matches' : [],
@@ -620,32 +625,37 @@ async def do_complete(self, code, cursor_pos):
620625
async def inspect_request(self, stream, ident, parent):
621626
content = parent['content']
622627

623-
reply_content = await self.do_inspect(
628+
reply_content = self.do_inspect(
624629
content['code'], content['cursor_pos'],
625630
content.get('detail_level', 0),
626631
)
632+
if inspect.isawaitable(reply_content):
633+
reply_content = await reply_content
634+
627635
# Before we send this object over, we scrub it for JSON usage
628636
reply_content = json_clean(reply_content)
629637
msg = self.session.send(stream, 'inspect_reply',
630638
reply_content, parent, ident)
631639
self.log.debug("%s", msg)
632640

633-
async def do_inspect(self, code, cursor_pos, detail_level=0):
641+
def do_inspect(self, code, cursor_pos, detail_level=0):
634642
"""Override in subclasses to allow introspection.
635643
"""
636644
return {'status': 'ok', 'data': {}, 'metadata': {}, 'found': False}
637645

638646
async def history_request(self, stream, ident, parent):
639647
content = parent['content']
640648

641-
reply_content = await self.do_history(**content)
649+
reply_content = self.do_history(**content)
650+
if inspect.isawaitable(reply_content):
651+
reply_content = await reply_content
642652

643653
reply_content = json_clean(reply_content)
644654
msg = self.session.send(stream, 'history_reply',
645655
reply_content, parent, ident)
646656
self.log.debug("%s", msg)
647657

648-
async def do_history(self, hist_access_type, output, raw, session=None, start=None,
658+
def do_history(self, hist_access_type, output, raw, session=None, start=None,
649659
stop=None, n=None, pattern=None, unique=False):
650660
"""Override in subclasses to access history.
651661
"""
@@ -698,7 +708,9 @@ async def comm_info_request(self, stream, ident, parent):
698708
self.log.debug("%s", msg)
699709

700710
async def shutdown_request(self, stream, ident, parent):
701-
content = await self.do_shutdown(parent['content']['restart'])
711+
content = self.do_shutdown(parent['content']['restart'])
712+
if inspect.isawaitable(content):
713+
content = await content
702714
self.session.send(stream, 'shutdown_reply', content, parent, ident=ident)
703715
# same content, but different msg_id for broadcasting on IOPub
704716
self._shutdown_message = self.session.msg('shutdown_reply',
@@ -715,7 +727,7 @@ async def shutdown_request(self, stream, ident, parent):
715727
shell_io_loop = self.shell_stream.io_loop
716728
shell_io_loop.add_callback(shell_io_loop.stop)
717729

718-
async def do_shutdown(self, restart):
730+
def do_shutdown(self, restart):
719731
"""Override in subclasses to do things when the frontend shuts down the
720732
kernel.
721733
"""
@@ -725,21 +737,25 @@ async def is_complete_request(self, stream, ident, parent):
725737
content = parent['content']
726738
code = content['code']
727739

728-
reply_content = await self.do_is_complete(code)
740+
reply_content = self.do_is_complete(code)
741+
if inspect.isawaitable(reply_content):
742+
reply_content = await reply_content
729743
reply_content = json_clean(reply_content)
730744
reply_msg = self.session.send(stream, 'is_complete_reply',
731745
reply_content, parent, ident)
732746
self.log.debug("%s", reply_msg)
733747

734-
async def do_is_complete(self, code):
748+
def do_is_complete(self, code):
735749
"""Override in subclasses to find completions.
736750
"""
737751
return { 'status' : 'unknown'}
738752

739753
async def debug_request(self, stream, ident, parent):
740754
content = parent['content']
741755

742-
reply_content = await self.do_debug_request(content)
756+
reply_content = self.do_debug_request(content)
757+
if inspect.isawaitable(reply_content):
758+
reply_content = await reply_content
743759
reply_content = json_clean(reply_content)
744760
reply_msg = self.session.send(stream, 'debug_reply', reply_content,
745761
parent, ident)
@@ -775,7 +791,7 @@ async def apply_request(self, stream, ident, parent):
775791
self.session.send(stream, 'apply_reply', reply_content,
776792
parent=parent, ident=ident,buffers=result_buf, metadata=md)
777793

778-
async def do_apply(self, content, bufs, msg_id, reply_metadata):
794+
def do_apply(self, content, bufs, msg_id, reply_metadata):
779795
"""DEPRECATED"""
780796
raise NotImplementedError
781797

@@ -806,7 +822,7 @@ async def clear_request(self, stream, idents, parent):
806822
self.session.send(stream, 'clear_reply', ident=idents, parent=parent,
807823
content = content)
808824

809-
async def do_clear(self):
825+
def do_clear(self):
810826
"""DEPRECATED since 4.0.3"""
811827
raise NotImplementedError
812828

0 commit comments

Comments
 (0)