From 8d38b3ce41953d8d9048092cc283eb7662a768ed 2018-08-21 17:18:18 From: Dale Jung Date: 2018-08-21 17:18:18 Subject: [PATCH] Only trigger async loop runner when needed. Allows running files that call async loops synchronously on their own. https://github.com/ipython/ipython/pull/11265 Handle early return conditions from coro --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index dbb4592..3f4950d 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2808,15 +2808,25 @@ class InteractiveShell(SingletonConfigurable): def _run_cell(self, raw_cell, store_history, silent, shell_futures): """Internal method to run a complete IPython cell.""" - return self.loop_runner( - self.run_cell_async( - raw_cell, - store_history=store_history, - silent=silent, - shell_futures=shell_futures, - ) + coro = self.run_cell_async( + raw_cell, + store_history=store_history, + silent=silent, + shell_futures=shell_futures, ) + try: + interactivity = coro.send(None) + except StopIteration as exc: + return exc.value + + if isinstance(interactivity, ExecutionResult): + return interactivity + + if interactivity == 'async': + return self.loop_runner(coro) + return _pseudo_sync_runner(coro) + @asyncio.coroutine def run_cell_async(self, raw_cell:str, store_history=False, silent=False, shell_futures=True) -> ExecutionResult: """Run a complete IPython cell asynchronously. @@ -2965,6 +2975,9 @@ class InteractiveShell(SingletonConfigurable): interactivity = "none" if silent else self.ast_node_interactivity if _run_async: interactivity = 'async' + # yield interactivity so let run_cell decide whether to use + # an async loop_runner + yield interactivity has_raised = yield from self.run_ast_nodes(code_ast.body, cell_name, interactivity=interactivity, compiler=compiler, result=result)