Skip to content

Commit

Permalink
do_[*] functions do not have to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed Apr 23, 2021
1 parent 583dc40 commit 7b01b09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
16 changes: 8 additions & 8 deletions ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ async def run_cell(*args, **kwargs):

return reply_content

async def do_complete(self, code, cursor_pos):
def do_complete(self, code, cursor_pos):
if _use_experimental_60_completion and self.use_experimental_completions:
return self._experimental_do_complete(code, cursor_pos)

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

async def do_debug_request(self, msg):
return self.debugger.process_request(msg)
return await self.debugger.process_request(msg)

def _experimental_do_complete(self, code, cursor_pos):
"""
Expand Down Expand Up @@ -440,7 +440,7 @@ def _experimental_do_complete(self, code, cursor_pos):
'metadata': {_EXPERIMENTAL_KEY_NAME: comps},
'status': 'ok'}

async def do_inspect(self, code, cursor_pos, detail_level=0):
def do_inspect(self, code, cursor_pos, detail_level=0):
name = token_at_cursor(code, cursor_pos)

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

return reply_content

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

async def do_shutdown(self, restart):
def do_shutdown(self, restart):
self.shell.exit_now = True
return dict(status='ok', restart=restart)

async def do_is_complete(self, code):
def do_is_complete(self, code):
transformer_manager = getattr(self.shell, 'input_transformer_manager', None)
if transformer_manager is None:
# input_splitter attribute is deprecated
Expand All @@ -497,7 +497,7 @@ async def do_is_complete(self, code):
r['indent'] = ' ' * indent_spaces
return r

async def do_apply(self, content, bufs, msg_id, reply_metadata):
def do_apply(self, content, bufs, msg_id, reply_metadata):
from .serialize import serialize_object, unpack_apply_message
shell = self.shell
try:
Expand Down Expand Up @@ -552,7 +552,7 @@ async def do_apply(self, content, bufs, msg_id, reply_metadata):

return reply_content, result_buf

async def do_clear(self):
def do_clear(self):
self.shell.reset(False)
return dict(status='ok')

Expand Down
46 changes: 31 additions & 15 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,12 @@ async def execute_request(self, stream, ident, parent):
self.execution_count += 1
self._publish_execute_input(code, parent, self.execution_count)

reply_content = await self.do_execute(
reply_content = self.do_execute(
code, silent, store_history,
user_expressions, allow_stdin,
)
if inspect.isawaitable(reply_content):
reply_content = await reply_content

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

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

matches = await self.do_complete(code, cursor_pos)
matches = self.do_complete(code, cursor_pos)
if inspect.isawaitable(matches):
matches = await matches

matches = json_clean(matches)
self.session.send(stream, "complete_reply", matches, parent, ident)

async def do_complete(self, code, cursor_pos):
def do_complete(self, code, cursor_pos):
"""Override in subclasses to find completions.
"""
return {'matches' : [],
Expand All @@ -620,32 +625,37 @@ async def do_complete(self, code, cursor_pos):
async def inspect_request(self, stream, ident, parent):
content = parent['content']

reply_content = await self.do_inspect(
reply_content = self.do_inspect(
content['code'], content['cursor_pos'],
content.get('detail_level', 0),
)
if inspect.isawaitable(reply_content):
reply_content = await reply_content

# Before we send this object over, we scrub it for JSON usage
reply_content = json_clean(reply_content)
msg = self.session.send(stream, 'inspect_reply',
reply_content, parent, ident)
self.log.debug("%s", msg)

async def do_inspect(self, code, cursor_pos, detail_level=0):
def do_inspect(self, code, cursor_pos, detail_level=0):
"""Override in subclasses to allow introspection.
"""
return {'status': 'ok', 'data': {}, 'metadata': {}, 'found': False}

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

reply_content = await self.do_history(**content)
reply_content = self.do_history(**content)
if inspect.isawaitable(reply_content):
reply_content = await reply_content

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

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

async def shutdown_request(self, stream, ident, parent):
content = await self.do_shutdown(parent['content']['restart'])
content = self.do_shutdown(parent['content']['restart'])
if inspect.isawaitable(content):
content = await content
self.session.send(stream, 'shutdown_reply', content, parent, ident=ident)
# same content, but different msg_id for broadcasting on IOPub
self._shutdown_message = self.session.msg('shutdown_reply',
Expand All @@ -715,7 +727,7 @@ async def shutdown_request(self, stream, ident, parent):
shell_io_loop = self.shell_stream.io_loop
shell_io_loop.add_callback(shell_io_loop.stop)

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

reply_content = await self.do_is_complete(code)
reply_content = self.do_is_complete(code)
if inspect.isawaitable(reply_content):
reply_content = await reply_content
reply_content = json_clean(reply_content)
reply_msg = self.session.send(stream, 'is_complete_reply',
reply_content, parent, ident)
self.log.debug("%s", reply_msg)

async def do_is_complete(self, code):
def do_is_complete(self, code):
"""Override in subclasses to find completions.
"""
return { 'status' : 'unknown'}

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

reply_content = await self.do_debug_request(content)
reply_content = self.do_debug_request(content)
if inspect.isawaitable(reply_content):
reply_content = await reply_content
reply_content = json_clean(reply_content)
reply_msg = self.session.send(stream, 'debug_reply', reply_content,
parent, ident)
Expand Down Expand Up @@ -775,7 +791,7 @@ async def apply_request(self, stream, ident, parent):
self.session.send(stream, 'apply_reply', reply_content,
parent=parent, ident=ident,buffers=result_buf, metadata=md)

async def do_apply(self, content, bufs, msg_id, reply_metadata):
def do_apply(self, content, bufs, msg_id, reply_metadata):
"""DEPRECATED"""
raise NotImplementedError

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

async def do_clear(self):
def do_clear(self):
"""DEPRECATED since 4.0.3"""
raise NotImplementedError

Expand Down

0 comments on commit 7b01b09

Please sign in to comment.