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

Terry J. Reedy report at bugs.python.org
Mon Sep 6 14:18:38 EDT 2021


Terry J. Reedy <tjreedy at udel.edu> added the comment:

The doc section in question is
https://docs.python.org/3/library/ast.html#ast.parse

I confirmed that 'break', 'continue', 'yield', and 'return' still parse, with the results how having "type_ignores=[]" added.
  'Module(body=[Expr(value=Yield())], type_ignores=[])'
I do not understand Nick's comment about 'await' as 'await' is not a legal statement'

The current initial paragraph says:
  "Parse the source into an AST node. Equivalent to compile(source, filename, mode, ast.PyCF_ONLY_AST)."

I suggest following this with:
  "If the AST node is compiled to a code object, there are additional syntax checks that can raise errors.  "For example, 'return' parses to a node, but is not legal outside of a def statement."

Hrvoje's suggested adding something like
  "If source contains a null character ('\0'), ValueError is raised."

I don't think that this is needed as ast.parse is explicitly noted as equivalent to a compile call, and the compile doc says "This function raises SyntaxError if the compiled source is invalid, and ValueError if the source contains null bytes."  (Should not that be 'null characters' given that *source* is now unicode?) This statement need not be repeated here.

----------
versions: +Python 3.11 -Python 2.7, Python 3.7

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


More information about the Python-bugs-list mailing list