codeop.Compile() difference with compile builtin and trailing newline.
Found as a failing test in the IPython test suite, and sorry this is only using the latest packaged version on pypy38 in cf-stating on conda.
# error.py data = """async with a: pass""" import ast from codeop import Compile c = Compile() c.flags |= ast.PyCF_ALLOW_TOP_LEVEL_AWAIT for extra in ("", "\n"): try: c( data + extra, "example.py", symbol="exec", ) except SyntaxError: print("Fail with syntax error") # will fail if extra == '\n' else: print("ok")
I don't expect the result of compiling a string to vary depending on whether there is trailing newline,
you can replace
c(...) with the equivalent
compile() builtin code with the flags=ast.... and that works fine.