From 9c913902c5b7b8f6565e800e5c33da8eddbdc758 Mon Sep 17 00:00:00 2001 From: Taus Date: Tue, 22 Oct 2024 15:39:29 +0000 Subject: [PATCH] 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. --- .../parser/exception_groups_new.expected | 22 ++++++++++++++++++- .../tests/parser/exception_groups_new.py | 5 +++++ python/extractor/tsg-python/tsp/grammar.js | 3 ++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/python/extractor/tests/parser/exception_groups_new.expected b/python/extractor/tests/parser/exception_groups_new.expected index 1d2de92755f..a70bf1b2485 100644 --- a/python/extractor/tests/parser/exception_groups_new.expected +++ b/python/extractor/tests/parser/exception_groups_new.expected @@ -1,4 +1,4 @@ -Module: [1, 0] - [22, 0] +Module: [1, 0] - [27, 0] body: [ Try: [1, 0] - [1, 4] body: [ @@ -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: [] ] diff --git a/python/extractor/tests/parser/exception_groups_new.py b/python/extractor/tests/parser/exception_groups_new.py index f24e3b00f4e..01fe58b9a98 100644 --- a/python/extractor/tests/parser/exception_groups_new.py +++ b/python/extractor/tests/parser/exception_groups_new.py @@ -19,3 +19,8 @@ else: finally: u v + +try: + pass +except *foo as e: + pass diff --git a/python/extractor/tsg-python/tsp/grammar.js b/python/extractor/tsg-python/tsp/grammar.js index 256364f4364..7016eea5b18 100644 --- a/python/extractor/tsg-python/tsp/grammar.js +++ b/python/extractor/tsg-python/tsp/grammar.js @@ -309,7 +309,8 @@ module.exports = grammar({ ), except_group_clause: $ => seq( - 'except*', + 'except', + '*', seq( field('type', $.expression), optional(seq(