Skip to content

Commit 3604092

Browse files
committed
Use native coroutines
1 parent 280fdfd commit 3604092

File tree

4 files changed

+75
-106
lines changed

4 files changed

+75
-106
lines changed

ipykernel/inprocess/ipkernel.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6-
from contextlib import contextmanager
6+
from contextlib import asynccontextmanager
77
import logging
88
import sys
99

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

77-
def execute_request(self, stream, ident, parent):
77+
async def execute_request(self, stream, ident, parent):
7878
""" Override for temporary IO redirection. """
79-
with self._redirected_io():
80-
super(InProcessKernel, self).execute_request(stream, ident, parent)
79+
async with self._redirected_io():
80+
await super(InProcessKernel, self).execute_request(stream, ident, parent)
8181

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

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

@@ -113,7 +113,7 @@ def _input_request(self, prompt, ident, parent, password=False):
113113
# Protected interface
114114
#-------------------------------------------------------------------------
115115

116-
@contextmanager
116+
@asynccontextmanager
117117
def _redirected_io(self):
118118
""" Temporarily redirect IO to the kernel.
119119
"""

ipykernel/ipkernel.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from IPython.core import release
1212
from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
13-
from tornado import gen
1413
from traitlets import Instance, Type, Any, List, Bool, observe, observe_compat
1514
from zmq.eventloop.zmqstream import ZMQStream
1615

@@ -149,8 +148,7 @@ def __init__(self, **kwargs):
149148
'file_extension': '.py'
150149
}
151150

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

279-
@gen.coroutine
280-
def do_execute(self, code, silent, store_history=True,
281-
user_expressions=None, allow_stdin=False):
277+
async def do_execute(self, code, silent, store_history=True,
278+
user_expressions=None, allow_stdin=False):
282279
shell = self.shell # we'll need this a lot here
283280

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

@@ -324,7 +320,7 @@ def run_cell(*args, **kwargs):
324320
with self._cancel_on_sigint(coro_future):
325321
res = None
326322
try:
327-
res = yield coro_future
323+
res = await coro_future
328324
finally:
329325
shell.events.trigger('post_execute')
330326
if not silent:
@@ -385,7 +381,7 @@ def run_cell(*args, **kwargs):
385381

386382
return reply_content
387383

388-
def do_complete(self, code, cursor_pos):
384+
async def do_complete(self, code, cursor_pos):
389385
if _use_experimental_60_completion and self.use_experimental_completions:
390386
return self._experimental_do_complete(code, cursor_pos)
391387

@@ -404,9 +400,8 @@ def do_complete(self, code, cursor_pos):
404400
'metadata' : {},
405401
'status' : 'ok'}
406402

407-
@gen.coroutine
408-
def do_debug_request(self, msg):
409-
return (yield self.debugger.process_request(msg))
403+
async def do_debug_request(self, msg):
404+
return self.debugger.process_request(msg)
410405

411406
def _experimental_do_complete(self, code, cursor_pos):
412407
"""
@@ -442,9 +437,7 @@ def _experimental_do_complete(self, code, cursor_pos):
442437
'metadata': {_EXPERIMENTAL_KEY_NAME: comps},
443438
'status': 'ok'}
444439

445-
446-
447-
def do_inspect(self, code, cursor_pos, detail_level=0):
440+
async def do_inspect(self, code, cursor_pos, detail_level=0):
448441
name = token_at_cursor(code, cursor_pos)
449442

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

466459
return reply_content
467460

468-
def do_history(self, hist_access_type, output, raw, session=0, start=0,
461+
async def do_history(self, hist_access_type, output, raw, session=0, start=0,
469462
stop=None, n=None, pattern=None, unique=False):
470463
if hist_access_type == 'tail':
471464
hist = self.shell.history_manager.get_tail(n, raw=raw, output=output,
@@ -486,11 +479,11 @@ def do_history(self, hist_access_type, output, raw, session=0, start=0,
486479
'history' : list(hist),
487480
}
488481

489-
def do_shutdown(self, restart):
482+
async def do_shutdown(self, restart):
490483
self.shell.exit_now = True
491484
return dict(status='ok', restart=restart)
492485

493-
def do_is_complete(self, code):
486+
async def do_is_complete(self, code):
494487
transformer_manager = getattr(self.shell, 'input_transformer_manager', None)
495488
if transformer_manager is None:
496489
# input_splitter attribute is deprecated
@@ -501,7 +494,7 @@ def do_is_complete(self, code):
501494
r['indent'] = ' ' * indent_spaces
502495
return r
503496

504-
def do_apply(self, content, bufs, msg_id, reply_metadata):
497+
async def do_apply(self, content, bufs, msg_id, reply_metadata):
505498
from .serialize import serialize_object, unpack_apply_message
506499
shell = self.shell
507500
try:
@@ -556,7 +549,7 @@ def do_apply(self, content, bufs, msg_id, reply_metadata):
556549

557550
return reply_content, result_buf
558551

559-
def do_clear(self):
552+
async def do_clear(self):
560553
self.shell.reset(False)
561554
return dict(status='ok')
562555

0 commit comments

Comments
 (0)