Skip to content

Commit

Permalink
Use native coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed Apr 10, 2021
1 parent 280fdfd commit 4c9ae9c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 106 deletions.
12 changes: 6 additions & 6 deletions ipykernel/inprocess/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from contextlib import contextmanager
from contextlib import asynccontextmanager
import logging
import sys

Expand Down Expand Up @@ -74,16 +74,16 @@ def __init__(self, **traits):
self._underlying_iopub_socket.observe(self._io_dispatch, names=['message_sent'])
self.shell.kernel = self

def execute_request(self, stream, ident, parent):
async def execute_request(self, stream, ident, parent):
""" Override for temporary IO redirection. """
with self._redirected_io():
super(InProcessKernel, self).execute_request(stream, ident, parent)
async with self._redirected_io():
await super(InProcessKernel, self).execute_request(stream, ident, parent)

def start(self):
""" Override registration of dispatchers for streams. """
self.shell.exit_now = False

def _abort_queues(self):
async def _abort_queues(self):
""" The in-process kernel doesn't abort requests. """
pass

Expand Down Expand Up @@ -113,7 +113,7 @@ def _input_request(self, prompt, ident, parent, password=False):
# Protected interface
#-------------------------------------------------------------------------

@contextmanager
@asynccontextmanager
def _redirected_io(self):
""" Temporarily redirect IO to the kernel.
"""
Expand Down
35 changes: 14 additions & 21 deletions ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from IPython.core import release
from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
from tornado import gen
from traitlets import Instance, Type, Any, List, Bool, observe, observe_compat
from zmq.eventloop.zmqstream import ZMQStream

Expand Down Expand Up @@ -149,8 +148,7 @@ def __init__(self, **kwargs):
'file_extension': '.py'
}

@gen.coroutine
def dispatch_debugpy(self, msg):
async def dispatch_debugpy(self, msg):
# The first frame is the socket id, we can drop it
frame = msg[1].bytes.decode('utf-8')
self.log.debug("Debugpy received: %s", frame)
Expand Down Expand Up @@ -276,9 +274,8 @@ def set_sigint_result():
# restore the previous sigint handler
signal.signal(signal.SIGINT, save_sigint)

@gen.coroutine
def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):
async def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):
shell = self.shell # we'll need this a lot here

self._forward_input(allow_stdin)
Expand All @@ -291,8 +288,7 @@ def do_execute(self, code, silent, store_history=True,
should_run_async = lambda cell: False
# older IPython,
# use blocking run_cell and wrap it in coroutine
@gen.coroutine
def run_cell(*args, **kwargs):
async def run_cell(*args, **kwargs):
return shell.run_cell(*args, **kwargs)
try:

Expand Down Expand Up @@ -324,7 +320,7 @@ def run_cell(*args, **kwargs):
with self._cancel_on_sigint(coro_future):
res = None
try:
res = yield coro_future
res = await coro_future
finally:
shell.events.trigger('post_execute')
if not silent:
Expand Down Expand Up @@ -385,7 +381,7 @@ def run_cell(*args, **kwargs):

return reply_content

def do_complete(self, code, cursor_pos):
async 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,9 +400,8 @@ def do_complete(self, code, cursor_pos):
'metadata' : {},
'status' : 'ok'}

@gen.coroutine
def do_debug_request(self, msg):
return (yield self.debugger.process_request(msg))
async def do_debug_request(self, msg):
return self.debugger.process_request(msg)

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



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

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

return reply_content

def do_history(self, hist_access_type, output, raw, session=0, start=0,
async 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 @@ -486,11 +479,11 @@ def do_history(self, hist_access_type, output, raw, session=0, start=0,
'history' : list(hist),
}

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

def do_is_complete(self, code):
async 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 @@ -501,7 +494,7 @@ def do_is_complete(self, code):
r['indent'] = ' ' * indent_spaces
return r

def do_apply(self, content, bufs, msg_id, reply_metadata):
async 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 @@ -556,7 +549,7 @@ def do_apply(self, content, bufs, msg_id, reply_metadata):

return reply_content, result_buf

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

Expand Down
Loading

0 comments on commit 4c9ae9c

Please sign in to comment.