diff --git a/IPython/core/async_helpers.py b/IPython/core/async_helpers.py index 13de7b0..187666f 100644 --- a/IPython/core/async_helpers.py +++ b/IPython/core/async_helpers.py @@ -153,7 +153,8 @@ def _should_be_async(cell: str) -> bool: try: # we can't limit ourself to ast.parse, as it __accepts__ to parse on # 3.7+, but just does not _compile_ - code = compile(cell, "<>", "exec", flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT) + # code = compile(cell, "<>", "exec", flags=getattr(ast,'PyCF_ALLOW_TOP_LEVEL_AWAIT', 0x0)) + code = compile(cell, "<>", "exec") return inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE except SyntaxError: #if sys.version_info > (3, 8): diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 4de0233..01310d2 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -3045,7 +3045,8 @@ class InteractiveShell(SingletonConfigurable): # Execute the user code interactivity = "none" if silent else self.ast_node_interactivity if _run_async: - interactivity = 'async' + print(interactivity) + interactivity = 'last_expr' has_raised = yield from self.run_ast_nodes(code_ast.body, cell_name, interactivity=interactivity, compiler=compiler, result=result) @@ -3167,6 +3168,9 @@ class InteractiveShell(SingletonConfigurable): """ if not nodelist: return + if interactivity == 'async': + interactivify = 'last' + if interactivity == 'last_expr_or_assign': if isinstance(nodelist[-1], _assign_nodes): asg = nodelist[-1] @@ -3200,6 +3204,8 @@ class InteractiveShell(SingletonConfigurable): _async = True else: raise ValueError("Interactivity was %r" % interactivity) + + print('interactivity:', interactivity) try: if _async and sys.version_info < (3,8): raise ValueError @@ -3217,17 +3223,20 @@ class InteractiveShell(SingletonConfigurable): is_async = (inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE) print('async=', _async, 'autodetect=', is_async) return is_async + + # refactor that to just change the mod constructor. for i, node in enumerate(to_run_exec): mod = Module([node], []) - code = compiler(mod, cell_name, "exec") - compare(code) - if (yield from self.run_code(code, result)): + with compiler.extra_flags(getattr(ast, 'PyCF_ALLOW_TOP_LEVEL_AWAIT', 0x0) if self.autoawait else 0x0): + code = compiler(mod, cell_name, "exec") + asy = compare(code) + if (yield from self.run_code(code, result, async_=asy)): return True for i, node in enumerate(to_run_interactive): print('B: interactive, async=', _async, nodelist) mod = ast.Interactive([node]) - with compiler.extra_flags(ast.PyCF_ALLOW_TOP_LEVEL_AWAIT if self.autoawait else 0x0): + with compiler.extra_flags(getattr(ast, 'PyCF_ALLOW_TOP_LEVEL_AWAIT', 0x0) if self.autoawait else 0x0): code = compiler(mod, cell_name, "single") asy = compare(code)