diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index fefc523..04875b6 100644 --- a/IPython/core/inputtransformer2.py +++ b/IPython/core/inputtransformer2.py @@ -10,7 +10,9 @@ deprecated in 7.0. # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -from codeop import compile_command +import ast +import sys +from codeop import CommandCompiler import re import tokenize from typing import List, Tuple, Optional, Any @@ -773,3 +775,20 @@ def find_last_indent(lines): if not m: return 0 return len(m.group(0).replace('\t', ' '*4)) + + +class MaybeAsyncCommandCompiler(CommandCompiler): + def __init__(self, extra_flags=0): + self.compiler = self._compiler + self.extra_flags = extra_flags + + def _compiler(self, source, filename, symbol, flags, feature): + flags |= self.extra_flags + return compile(source, filename, symbol, flags, feature) + +if (sys.version_info.major, sys.version_info.minor) >= (3, 8): + _extra_flags = ast.PyCF_ALLOW_TOP_LEVEL_AWAIT +else: + _extra_flags = ast.PyCF_ONLY_AST + +compile_command = MaybeAsyncCommandCompiler(extra_flags=_extra_flags)