diff --git a/IPython/core/async_helpers.py b/IPython/core/async_helpers.py index ba07915..1a7d889 100644 --- a/IPython/core/async_helpers.py +++ b/IPython/core/async_helpers.py @@ -150,9 +150,11 @@ def _should_be_async(cell: str) -> bool: level, it will be seen as async. This is a know limitation. """ if sys.version_info > (3, 8): - code = compile(cell, "<>", "exec", flags=getattr(ast,'PyCF_ALLOW_TOP_LEVEL_AWAIT', 0x0)) - return inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE - + try: + code = compile(cell, "<>", "exec", flags=getattr(ast,'PyCF_ALLOW_TOP_LEVEL_AWAIT', 0x0)) + return inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE + except SyntaxError: + return False try: # we can't limit ourself to ast.parse, as it __accepts__ to parse on # 3.7+, but just does not _compile_ diff --git a/IPython/core/compilerop.py b/IPython/core/compilerop.py index a8037ea..c4771af 100644 --- a/IPython/core/compilerop.py +++ b/IPython/core/compilerop.py @@ -137,12 +137,17 @@ class CachingCompiler(codeop.Compile): @contextmanager def extra_flags(self, flags): - old_flags = self.flags + ## bits that we'll set to 1 + turn_on_bits = ~self.flags & flags + + self.flags = self.flags | flags try: yield finally: - self.flags = old_flags + # turn off only the bits we turned on so that something like + # __future__ that set flags stays. + self.flags &= ~turn_on_bits def check_linecache_ipython(*args):