[issue30637] Syntax error reported on compile(...), but not on compile(..., ast.PyCF_ONLY_AST)

Michał Radwański report at bugs.python.org
Tue Jun 13 07:53:12 EDT 2017


Michał Radwański added the comment:

Docs mention:


ast.parse(source, filename='<unknown>', mode='exec')
    Parse the source into an AST node. Equivalent to compile(source, filename, mode, ast.PyCF_ONLY_AST).

If you just parse code into AST, you first check whether it is possible to turn such source into a Python syntax tree. In that case, it obviously is, as you may imagine a function, that returns nothing:

def func():
    return

If however you try to make executable code of the source, it is checked whether the constructs make sense in provided context. And, as you may imagine, top-level code with return statement is not valid, hence the error.

----------
nosy: +enedil

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30637>
_______________________________________


More information about the Python-bugs-list mailing list