Skip to content

bpo-40621: except clauses should use NAME, not target #20086

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ try_stmt[stmt_ty]:
| 'try' ':' b=block f=finally_block { _Py_Try(b, NULL, NULL, f, EXTRA) }
| 'try' ':' b=block ex=except_block+ el=[else_block] f=[finally_block] { _Py_Try(b, ex, el, f, EXTRA) }
except_block[excepthandler_ty]:
| 'except' e=expression t=['as' z=target { z }] ':' b=block {
| 'except' e=expression t=['as' z=NAME { z }] ':' b=block {
_Py_ExceptHandler(e, (t) ? ((expr_ty) t)->v.Name.id : NULL, b, EXTRA) }
| 'except' ':' b=block { _Py_ExceptHandler(NULL, NULL, b, EXTRA) }
finally_block[asdl_seq*]: 'finally' ':' a=block { a }
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,31 @@
...
SyntaxError: no binding for nonlocal '_A__x' found

Tests for try-except clauses:
>>> try:
... ...
... except Exception as a.b:
... pass
Traceback (most recent call last):
...
SyntaxError: invalid syntax

>>> try:
... ...
... except Exception as a[0]:
... pass
Traceback (most recent call last):
...
SyntaxError: invalid syntax

>>> try:
... ...
... except Exception as (a):
... pass
Traceback (most recent call last):
...
SyntaxError: invalid syntax


This tests assignment-context; there was a bug in Python 2.5 where compiling
a complex 'if' (one with 'elif') would fail to notice an invalid suite,
Expand Down
12 changes: 6 additions & 6 deletions Parser/pegen/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3350,7 +3350,7 @@ try_stmt_rule(Parser *p)
return _res;
}

// except_block: 'except' expression ['as' target] ':' block | 'except' ':' block
// except_block: 'except' expression ['as' NAME] ':' block | 'except' ':' block
static excepthandler_ty
except_block_rule(Parser *p)
{
Expand All @@ -3367,7 +3367,7 @@ except_block_rule(Parser *p)
UNUSED(_start_lineno); // Only used by EXTRA macro
int _start_col_offset = p->tokens[_mark]->col_offset;
UNUSED(_start_col_offset); // Only used by EXTRA macro
{ // 'except' expression ['as' target] ':' block
{ // 'except' expression ['as' NAME] ':' block
Token * _keyword;
Token * _literal;
asdl_seq* b;
Expand All @@ -3378,7 +3378,7 @@ except_block_rule(Parser *p)
&&
(e = expression_rule(p)) // expression
&&
(t = _tmp_48_rule(p), 1) // ['as' target]
(t = _tmp_48_rule(p), 1) // ['as' NAME]
&&
(_literal = _PyPegen_expect_token(p, 11)) // token=':'
&&
Expand Down Expand Up @@ -13073,7 +13073,7 @@ _loop1_47_rule(Parser *p)
return _seq;
}

// _tmp_48: 'as' target
// _tmp_48: 'as' NAME
static void *
_tmp_48_rule(Parser *p)
{
Expand All @@ -13082,13 +13082,13 @@ _tmp_48_rule(Parser *p)
}
void * _res = NULL;
int _mark = p->mark;
{ // 'as' target
{ // 'as' NAME
Token * _keyword;
expr_ty z;
if (
(_keyword = _PyPegen_expect_token(p, 531)) // token='as'
&&
(z = target_rule(p)) // target
(z = _PyPegen_name_token(p)) // NAME
)
{
_res = z;
Expand Down