Skip to content

bpo-40631: Single-element starred elements in parenthesised expressions cannot be parsed#24014

Closed
pablogsal wants to merge 1 commit into
python:masterfrom
pablogsal:bpo-40631
Closed

bpo-40631: Single-element starred elements in parenthesised expressions cannot be parsed#24014
pablogsal wants to merge 1 commit into
python:masterfrom
pablogsal:bpo-40631

Conversation

@pablogsal

@pablogsal pablogsal commented Dec 30, 2020

Copy link
Copy Markdown
Member

Comment thread Grammar/python.gram
file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) }
eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) }
eval[mod_ty]: a=star_expressions NEWLINE* ENDMARKER { Expression(a, p->arena) }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lysnikolaou Do you remember why this was expressions and not star_expressions? I cannot see any edge case that makes this change incorrect but now I am curious.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this was for consistency with 3.8. While 3.8 and 3.9 currently fail at parser level for eval('*a'), this patch fails at bytecode compiler level.

@pablogsal pablogsal Dec 30, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make that fail at the parser level if you want but not sure if is worth it because we would need to factor that into some rule and I don't know if we can reuse the rest. What's your opinion?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't leaving it as is work?

The reason I'm pushing for the error to be in parser level is due to the quality of the error message. Here's an example:

Without this patch:

➜  cpython git:(bpo-40631) ✗ cat a.py       
a = [1, 2, 3]
eval('''


*a''')
➜  cpython git:(bpo-40631) ✗ python a.py
Traceback (most recent call last):
  File "/home/lysnikolaou/repos/cpython/a.py", line 2, in <module>
    eval('''
  File "<string>", line 4
    *a
    ^
SyntaxError: invalid syntax

With this patch:

➜  cpython git:(bpo-40631) ✗ cat a.py
a = [1, 2, 3]
eval('''


*a''')
➜  cpython git:(bpo-40631) ✗ ./python a.py
Traceback (most recent call last):
  File "/home/lysnikolaou/repos/cpython/a.py", line 2, in <module>
    eval('''
  File "<string>", line 4
SyntaxError: can't use starred expression here

The first one makes a much better job of making clear what the error is I feel. But if it's too much effort and leaving it as is doesn't work, I'm okay with it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much agree. I have closed the PR based on this discussion and Guido's request. Thanks for the examples and the comment.

Comment thread Grammar/python.gram

star_element[expr_ty]:
| '*' a=bitwise_or { _Py_Starred(a, Load, EXTRA) }
| '(' '*' a=bitwise_or ')' { _Py_Starred(a, Load, EXTRA) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch makes (*x), y valid, but with multiple layers of parentheses (((*x))), y is still considered invalid.

@pablogsal

Copy link
Copy Markdown
Member Author

I am closing this as per Guido's request in the issue tracker.

@pablogsal pablogsal closed this Dec 31, 2020
@pablogsal
pablogsal deleted the bpo-40631 branch December 31, 2020 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants