bpo-40618: Disallow invalid targets in augassign and except clauses by lysnikolaou · Pull Request #20083 · python/cpython · GitHub
Skip to content
Merged
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
17 changes: 8 additions & 9 deletions Grammar/python.gram
5 changes: 4 additions & 1 deletion Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ def __getitem__(self, i):
def test_try(self):
### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
### | 'try' ':' suite 'finally' ':' suite
### except_clause: 'except' [expr ['as' expr]]
### except_clause: 'except' [expr ['as' NAME]]
try:
1/0
except ZeroDivisionError:
Expand All @@ -1297,6 +1297,9 @@ def test_try(self):
except (EOFError, TypeError, ZeroDivisionError) as msg: pass
try: pass
finally: pass
with self.assertRaises(SyntaxError):
compile("try:\n pass\nexcept Exception as a.b:\n pass", "?", "exec")
compile("try:\n pass\nexcept Exception as a[b]:\n pass", "?", "exec")

def test_suite(self):
# simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_peg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
('attribute_simple', 'a.b'),
('attributes_subscript', 'a.b[0]'),
('augmented_assignment', 'x += 42'),
('augmented_assignment_attribute', 'a.b.c += 42'),
('augmented_assignment_paren', '(x) += 42'),
('augmented_assignment_paren_subscript', '(x[0]) -= 42'),
('binop_add', '1 + 1'),
('binop_add_multiple', '1 + 1 + 1 + 1'),
('binop_all', '1 + 2 * 5 + 3 ** 2 - -3'),
Expand Down Expand Up @@ -547,6 +550,11 @@ def f(*a, b):
with a as (x, y):
pass
'''),
('with_list_target',
'''
with a as [x, y]:
pass
'''),
('yield', 'yield'),
('yield_expr', 'yield a'),
('yield_from', 'yield from a'),
Expand All @@ -560,6 +568,9 @@ def f(*a, b):
("annotation_tuple", "(a,): int"),
("annotation_tuple_without_paren", "a,: int"),
("assignment_keyword", "a = if"),
("augmented_assignment_list", "[a, b] += 1"),
("augmented_assignment_tuple", "a, b += 1"),
("augmented_assignment_tuple_paren", "(a, b) += (1, 2)"),
("comprehension_lambda", "(a for a in lambda: b)"),
("comprehension_else", "(a for a in b if c else d"),
("del_call", "del a()"),
Expand Down Expand Up @@ -589,6 +600,20 @@ def f():
a
"""),
("not_terminated_string", "a = 'example"),
("try_except_attribute_target",
"""
try:
pass
except Exception as a.b:
pass
"""),
("try_except_subscript_target",
"""
try:
pass
except Exception as a[0]:
pass
"""),
]

FAIL_SPECIALIZED_MESSAGE_CASES = [
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@
Traceback (most recent call last):
SyntaxError: cannot assign to conditional expression

>>> a, b += 1, 2
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> (a, b) += 1, 2
Traceback (most recent call last):
SyntaxError: cannot assign to tuple

>>> [a, b] += 1, 2
Traceback (most recent call last):
SyntaxError: cannot assign to list

From compiler_complex_args():

>>> def f(None=1):
Expand Down
69 changes: 33 additions & 36 deletions Parser/pegen/parse.c