Skip to content

Commit

Permalink
Python: Allow except* to be written as except *
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tausbn committed Oct 22, 2024
1 parent 7ceefb5 commit 9c91390
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
22 changes: 21 additions & 1 deletion python/extractor/tests/parser/exception_groups_new.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Module: [1, 0] - [22, 0]
Module: [1, 0] - [27, 0]
body: [
Try: [1, 0] - [1, 4]
body: [
Expand Down Expand Up @@ -133,4 +133,24 @@ Module: [1, 0] - [22, 0]
variable: Variable('v', None)
ctx: Load
]
Try: [23, 0] - [23, 4]
body: [
Pass: [24, 4] - [24, 8]
]
orelse: []
handlers: [
ExceptGroupStmt: [25, 0] - [26, 8]
type:
Name: [25, 8] - [25, 11]
variable: Variable('foo', None)
ctx: Load
name:
Name: [25, 15] - [25, 16]
variable: Variable('e', None)
ctx: Store
body: [
Pass: [26, 4] - [26, 8]
]
]
finalbody: []
]
5 changes: 5 additions & 0 deletions python/extractor/tests/parser/exception_groups_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
finally:
u
v

try:
pass
except *foo as e:
pass
3 changes: 2 additions & 1 deletion python/extractor/tsg-python/tsp/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ module.exports = grammar({
),

except_group_clause: $ => seq(
'except*',
'except',
'*',
seq(
field('type', $.expression),
optional(seq(
Expand Down

0 comments on commit 9c91390

Please sign in to comment.