New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compile() can compile a bare starred expression with PyCF_ONLY_AST
flag with the old parser, but not the new one
#85025
Comments
Not 100% sure this would be considered a bug, but it seems at least worth filing to check. This is a behaviour difference between the new parser and the old one. It's very easy to reproduce: <mock-chroot> sh-5.0# PYTHONOLDPARSER=1 python3
Python 3.9.0b1 (default, May 29 2020, 00:00:00)
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from _ast import *
>>> compile("(*starred)", "<unknown>", "exec", flags=PyCF_ONLY_AST)
<ast.Module object at 0x7fe1504532e0>
>>>
<mock-chroot> sh-5.0# python3
Python 3.9.0b1 (default, May 29 2020, 00:00:00)
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from _ast import *
>>> compile("(*starred)", "<unknown>", "exec", flags=PyCF_ONLY_AST)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<unknown>", line 1
(*starred)
^
SyntaxError: invalid syntax That is, you can compile() the expression "(*starred)" with PyCF_ONLY_AST flag set with the old parser, but not with the new one. Without PyCF_ONLY_AST you get a SyntaxError with both parsers, though a with the old parser, the error message is "can't use starred expression here", not "invalid syntax". |
There is no promise that the parser can parse invalid Python code. I think there is no a bug here. |
Realized I forgot to give it, so in case it's important, the context here is the black test suite: that test suite has a file full of expressions that it expects to be able to parse this way (it uses https://github.com/psf/black/blob/master/tests/data/expression.py#L149 and has been for as long as black has existed. Presumably if this isn't going to be fixed we'll need to adapt this black test file to test a starred expression in a 'valid' way, somehow. |
Yeah, this is not a bug as Serhiy points out. Regarding the error message, we are currently working on trying to unify the error messages in the the parser as much as we can but there is also no promise that they will match 100%. Many grammar checks currently are done outside the parser (we mention it on PEP-617) so I am not surprised that the old parser is ok with some invalid expressions and PyCF_ONLY_AST. This is one of the things we are trying to change with the new parser precisely. I will close for now as "not a bug". Feel free to reopen if you think we missed something or if something was left to discuss. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: