From 06e21e83c76b39c8b9ea7129fe20a6c1d9f864a5 2021-09-06 20:45:29 From: Matthias Bussonnier Date: 2021-09-06 20:45:29 Subject: [PATCH] Merge pull request #13120 from meeseeksmachine/auto-backport-of-pr-13090-on-7.x Backport PR #13090 on branch 7.x (Fix: `async with` doesn't allow newlines (ipython/ipython/issues/12975)) --- diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index 633b764..5b6f4a1 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, Compile import re import tokenize from typing import List, Tuple, Union @@ -724,3 +726,25 @@ def find_last_indent(lines): if not m: return 0 return len(m.group(0).replace('\t', ' '*4)) + + +class MaybeAsyncCompile(Compile): + def __init__(self, extra_flags=0): + super().__init__() + self.flags |= extra_flags + + def __call__(self, *args, **kwds): + return compile(*args, **kwds) + + +class MaybeAsyncCommandCompiler(CommandCompiler): + def __init__(self, extra_flags=0): + self.compiler = MaybeAsyncCompile(extra_flags=extra_flags) + + +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)