Skip to content

Commit 9c91390

Browse files
committed
Python: Allow except* to be written as except *
Turns out, `except*` is actually not a token on its own according to the Python grammar. This means it's legal to write `except *foo: ...`, which we previously would consider a syntax error. To fix it, we simply break up the `except*` into two separate tokens.
1 parent 7ceefb5 commit 9c91390

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

python/extractor/tests/parser/exception_groups_new.expected

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Module: [1, 0] - [22, 0]
1+
Module: [1, 0] - [27, 0]
22
body: [
33
Try: [1, 0] - [1, 4]
44
body: [
@@ -133,4 +133,24 @@ Module: [1, 0] - [22, 0]
133133
variable: Variable('v', None)
134134
ctx: Load
135135
]
136+
Try: [23, 0] - [23, 4]
137+
body: [
138+
Pass: [24, 4] - [24, 8]
139+
]
140+
orelse: []
141+
handlers: [
142+
ExceptGroupStmt: [25, 0] - [26, 8]
143+
type:
144+
Name: [25, 8] - [25, 11]
145+
variable: Variable('foo', None)
146+
ctx: Load
147+
name:
148+
Name: [25, 15] - [25, 16]
149+
variable: Variable('e', None)
150+
ctx: Store
151+
body: [
152+
Pass: [26, 4] - [26, 8]
153+
]
154+
]
155+
finalbody: []
136156
]

python/extractor/tests/parser/exception_groups_new.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@
1919
finally:
2020
u
2121
v
22+
23+
try:
24+
pass
25+
except *foo as e:
26+
pass

python/extractor/tsg-python/tsp/grammar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ module.exports = grammar({
309309
),
310310

311311
except_group_clause: $ => seq(
312-
'except*',
312+
'except',
313+
'*',
313314
seq(
314315
field('type', $.expression),
315316
optional(seq(

0 commit comments

Comments
 (0)