From 64631c580aa78fcb15325ecd29df94614772f8dd 2021-09-06 17:14:54 From: Matthias Bussonnier Date: 2021-09-06 17:14:54 Subject: [PATCH] Merge pull request #13090 from madbird1304/madbird1304/issue#12975 Fix: `async with` doesn't allow newlines (ipython/ipython/issues/12975) --- diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index fefc523..53e114c 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, Optional, Any @@ -773,3 +775,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)