diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 97ade0d..e3f146a 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -150,14 +150,16 @@ def removed_co_newlocals(function:types.FunctionType) -> types.FunctionType: return FunctionType(new_code, globals(), function.__name__, function.__defaults__) +# we still need to run things using the asyncio eventloop, but there is no +# async integration +from .async_helpers import (_asyncio_runner, _asyncify) + if sys.version_info > (3,5): - from .async_helpers import (_asyncio_runner, _curio_runner, _trio_runner, - _should_be_async, _asyncify - ) + from .async_helpers import _curio_runner, _trio_runner, _should_be_async else : - _asyncio_runner = _curio_runner = _trio_runner = None + _curio_runner = _trio_runner = None - def _should_be_async(whatever:str)->bool: + def _should_be_async(cell:str)->bool: return False @@ -2200,6 +2202,8 @@ class InteractiveShell(SingletonConfigurable): m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, ) + if sys.version_info >(3,5): + self.register_magics(m.AsyncMagics) # Register Magic Aliases mman = self.magics_manager diff --git a/IPython/core/magics/__init__.py b/IPython/core/magics/__init__.py index d2fd5a6..841f4da 100644 --- a/IPython/core/magics/__init__.py +++ b/IPython/core/magics/__init__.py @@ -14,7 +14,7 @@ from ..magic import Magics, magics_class from .auto import AutoMagics -from .basic import BasicMagics +from .basic import BasicMagics, AsyncMagics from .code import CodeMagics, MacroToEdit from .config import ConfigMagics from .display import DisplayMagics diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py index 29434e9..c60a193 100644 --- a/IPython/core/magics/basic.py +++ b/IPython/core/magics/basic.py @@ -379,62 +379,6 @@ Currently the magic system has the following functions:""", except: xmode_switch_err('user') - @line_magic - def autoawait(self, parameter_s): - """ - Allow to change the status of the autoawait option. - - This allow you to set a specific asynchronous code runner. - - If no value is passed, print the currently used asynchronous integration - and whether it is activated. - - It can take a number of value evaluated in the following order: - - - False/false/off deactivate autoawait integration - - True/true/on activate autoawait integration using configured default - loop - - asyncio/curio/trio activate autoawait integration and use integration - with said library. - - If the passed parameter does not match any of the above and is a python - identifier, get said object from user namespace and set it as the - runner, and activate autoawait. - - If the object is a fully qualified object name, attempt to import it and - set it as the runner, and activate autoawait.""" - - param = parameter_s.strip() - d = {True: "on", False: "off"} - - if not param: - print("IPython autoawait is `{}`, and set to use `{}`".format( - d[self.shell.autoawait], - self.shell.loop_runner - )) - return None - - if param.lower() in ('false', 'off'): - self.shell.autoawait = False - return None - if param.lower() in ('true', 'on'): - self.shell.autoawait = True - return None - - if param in self.shell.loop_runner_map: - self.shell.loop_runner = param - self.shell.autoawait = True - return None - - if param in self.shell.user_ns : - self.shell.loop_runner = self.shell.user_ns[param] - self.shell.autoawait = True - return None - - runner = import_item(param) - - self.shell.loop_runner = runner - self.shell.autoawait = True @line_magic @@ -656,3 +600,63 @@ Currently the magic system has the following functions:""", nb = v4.new_notebook(cells=cells) with io.open(args.filename, 'w', encoding='utf-8') as f: write(nb, f, version=4) + +@magics_class +class AsyncMagics(BasicMagics): + + @line_magic + def autoawait(self, parameter_s): + """ + Allow to change the status of the autoawait option. + + This allow you to set a specific asynchronous code runner. + + If no value is passed, print the currently used asynchronous integration + and whether it is activated. + + It can take a number of value evaluated in the following order: + + - False/false/off deactivate autoawait integration + - True/true/on activate autoawait integration using configured default + loop + - asyncio/curio/trio activate autoawait integration and use integration + with said library. + + If the passed parameter does not match any of the above and is a python + identifier, get said object from user namespace and set it as the + runner, and activate autoawait. + + If the object is a fully qualified object name, attempt to import it and + set it as the runner, and activate autoawait.""" + + param = parameter_s.strip() + d = {True: "on", False: "off"} + + if not param: + print("IPython autoawait is `{}`, and set to use `{}`".format( + d[self.shell.autoawait], + self.shell.loop_runner + )) + return None + + if param.lower() in ('false', 'off'): + self.shell.autoawait = False + return None + if param.lower() in ('true', 'on'): + self.shell.autoawait = True + return None + + if param in self.shell.loop_runner_map: + self.shell.loop_runner = param + self.shell.autoawait = True + return None + + if param in self.shell.user_ns : + self.shell.loop_runner = self.shell.user_ns[param] + self.shell.autoawait = True + return None + + runner = import_item(param) + + self.shell.loop_runner = runner + self.shell.autoawait = True