New submission from Hrvoje Nikšić:

Our application compiles snippets of user-specified code using the compile 
built-in with ast.PyCF_ONLY_AST flag. At this stage we catch syntax errors and 
perform some sanity checks on the AST. The AST is then compiled into actual 
code using compile() and run using further guards.

We found that using a bare "return" in the code works with ast.PyCF_ONLY_AST, 
but raises SyntaxError when compiled without the flag:

>>> import ast
>>> compile('return', '', 'exec', ast.PyCF_ONLY_AST, True)
<_ast.Module object at 0x7f35df872310>

>>> compile('return', '', 'exec', 0, True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 1
SyntaxError: 'return' outside function

Is this intended behavior? It doesn't seem to be documented anywhere.

----------
components: Interpreter Core
messages: 295771
nosy: hniksic
priority: normal
severity: normal
status: open
title: Syntax error reported on compile(...), but not on compile(..., 
ast.PyCF_ONLY_AST)
versions: Python 2.7, Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30637>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to