mirror of
https://github.com/github/codeql.git
synced 2026-02-11 20:51:06 +01:00
Merge pull request #20990 from github/tausbn/python-support-relaxed-exception-groups
Python: Add support for PEP-758 exception syntax
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
Module: [1, 0] - [27, 0]
|
||||
Module: [1, 0] - [32, 0]
|
||||
body: [
|
||||
Try: [1, 0] - [1, 4]
|
||||
body: [
|
||||
@@ -153,4 +153,28 @@ Module: [1, 0] - [27, 0]
|
||||
]
|
||||
]
|
||||
finalbody: []
|
||||
Try: [28, 0] - [28, 4]
|
||||
body: [
|
||||
Pass: [29, 4] - [29, 8]
|
||||
]
|
||||
orelse: []
|
||||
handlers: [
|
||||
ExceptGroupStmt: [30, 0] - [31, 8]
|
||||
type:
|
||||
Tuple: [30, 8] - [30, 12]
|
||||
elts: [
|
||||
Name: [30, 8] - [30, 9]
|
||||
variable: Variable('x', None)
|
||||
ctx: Load
|
||||
Name: [30, 11] - [30, 12]
|
||||
variable: Variable('y', None)
|
||||
ctx: Load
|
||||
]
|
||||
ctx: Load
|
||||
name: None
|
||||
body: [
|
||||
Pass: [31, 4] - [31, 8]
|
||||
]
|
||||
]
|
||||
finalbody: []
|
||||
]
|
||||
|
||||
@@ -24,3 +24,8 @@ try:
|
||||
pass
|
||||
except *foo as e:
|
||||
pass
|
||||
|
||||
try:
|
||||
pass
|
||||
except* x, y:
|
||||
pass
|
||||
|
||||
64
python/extractor/tests/parser/exceptions_new.expected
Normal file
64
python/extractor/tests/parser/exceptions_new.expected
Normal file
@@ -0,0 +1,64 @@
|
||||
Module: [1, 0] - [9, 0]
|
||||
body: [
|
||||
Try: [1, 0] - [1, 4]
|
||||
body: [
|
||||
Pass: [2, 4] - [2, 8]
|
||||
]
|
||||
orelse: []
|
||||
handlers: [
|
||||
ExceptStmt: [3, 0] - [3, 12]
|
||||
type:
|
||||
Tuple: [3, 7] - [3, 11]
|
||||
elts: [
|
||||
Name: [3, 7] - [3, 8]
|
||||
variable: Variable('a', None)
|
||||
ctx: Load
|
||||
Name: [3, 10] - [3, 11]
|
||||
variable: Variable('b', None)
|
||||
ctx: Load
|
||||
]
|
||||
ctx: Load
|
||||
name: None
|
||||
body: [
|
||||
Pass: [4, 4] - [4, 8]
|
||||
]
|
||||
ExceptStmt: [5, 0] - [5, 14]
|
||||
type:
|
||||
Tuple: [5, 8] - [5, 12]
|
||||
elts: [
|
||||
Name: [5, 8] - [5, 9]
|
||||
variable: Variable('c', None)
|
||||
ctx: Load
|
||||
Name: [5, 11] - [5, 12]
|
||||
variable: Variable('d', None)
|
||||
ctx: Load
|
||||
]
|
||||
ctx: Load
|
||||
parenthesised: True
|
||||
name: None
|
||||
body: [
|
||||
Pass: [6, 4] - [6, 8]
|
||||
]
|
||||
ExceptStmt: [7, 0] - [7, 19]
|
||||
type:
|
||||
Tuple: [7, 8] - [7, 12]
|
||||
elts: [
|
||||
Name: [7, 8] - [7, 9]
|
||||
variable: Variable('e', None)
|
||||
ctx: Load
|
||||
Name: [7, 11] - [7, 12]
|
||||
variable: Variable('f', None)
|
||||
ctx: Load
|
||||
]
|
||||
ctx: Load
|
||||
parenthesised: True
|
||||
name:
|
||||
Name: [7, 17] - [7, 18]
|
||||
variable: Variable('g', None)
|
||||
ctx: Store
|
||||
body: [
|
||||
Pass: [8, 4] - [8, 8]
|
||||
]
|
||||
]
|
||||
finalbody: []
|
||||
]
|
||||
8
python/extractor/tests/parser/exceptions_new.py
Normal file
8
python/extractor/tests/parser/exceptions_new.py
Normal file
@@ -0,0 +1,8 @@
|
||||
try:
|
||||
pass
|
||||
except a, b: # new, relaxed syntax
|
||||
pass
|
||||
except (c, d): # old syntax
|
||||
pass
|
||||
except (e, f) as g: # old syntax
|
||||
pass
|
||||
@@ -12,7 +12,7 @@
|
||||
(assignment !type) @assign
|
||||
{ let @assign.node = (ast-node @assign "Assign") }
|
||||
|
||||
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) (index_expression_list) ] @tuple
|
||||
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) (index_expression_list) (exception_list)] @tuple
|
||||
{ let @tuple.node = (ast-node @tuple "Tuple") }
|
||||
|
||||
(list_pattern) @list
|
||||
@@ -3445,6 +3445,9 @@
|
||||
(tuple element: (_) @elt) @parent
|
||||
|
||||
(tuple_pattern element: (_) @elt) @parent
|
||||
|
||||
; An exception list, as in `except A, B, C: ...`
|
||||
(exception_list element: (_) @elt) @parent
|
||||
]
|
||||
{
|
||||
edge @parent.node -> @elt.node
|
||||
@@ -3480,6 +3483,7 @@
|
||||
(parenthesized_expression inner: (_) @elt)
|
||||
(set element: (_) @elt)
|
||||
(match_sequence_pattern (_) @elt)
|
||||
(exception_list element: (_) @elt)
|
||||
] @seq
|
||||
{
|
||||
attr (@elt.node) _inherited_ctx = @seq.node
|
||||
|
||||
4
python/extractor/tsg-python/tsp/.gitattributes
vendored
Normal file
4
python/extractor/tsg-python/tsp/.gitattributes
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# Mark tree-sitter generated files
|
||||
src/grammar.json linguist-generated
|
||||
src/node-types.json linguist-generated
|
||||
src/parser.c linguist-generated
|
||||
@@ -297,12 +297,21 @@ module.exports = grammar({
|
||||
)
|
||||
),
|
||||
|
||||
exception_list: $ => seq(
|
||||
field('element', $.expression),
|
||||
repeat1(
|
||||
seq(
|
||||
',',
|
||||
field('element', $.expression))
|
||||
)
|
||||
),
|
||||
|
||||
except_clause: $ => seq(
|
||||
'except',
|
||||
optional(seq(
|
||||
field('type', $.expression),
|
||||
field('type', choice($.expression, $.exception_list)),
|
||||
optional(seq(
|
||||
choice('as', ','),
|
||||
'as',
|
||||
field('alias', $.expression)
|
||||
))
|
||||
)),
|
||||
@@ -314,7 +323,7 @@ module.exports = grammar({
|
||||
'except',
|
||||
'*',
|
||||
seq(
|
||||
field('type', $.expression),
|
||||
field('type', choice($.expression, $.exception_list)),
|
||||
optional(seq(
|
||||
'as',
|
||||
field('alias', $.expression)
|
||||
|
||||
72
python/extractor/tsg-python/tsp/src/grammar.json
generated
72
python/extractor/tsg-python/tsp/src/grammar.json
generated
@@ -1087,6 +1087,39 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"exception_list": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "FIELD",
|
||||
"name": "element",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "expression"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": ","
|
||||
},
|
||||
{
|
||||
"type": "FIELD",
|
||||
"name": "element",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "expression"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"except_clause": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
@@ -1104,8 +1137,17 @@
|
||||
"type": "FIELD",
|
||||
"name": "type",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "expression"
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "expression"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "exception_list"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -1115,17 +1157,8 @@
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "as"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": ","
|
||||
}
|
||||
]
|
||||
"type": "STRING",
|
||||
"value": "as"
|
||||
},
|
||||
{
|
||||
"type": "FIELD",
|
||||
@@ -1181,8 +1214,17 @@
|
||||
"type": "FIELD",
|
||||
"name": "type",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "expression"
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "expression"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "exception_list"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
24
python/extractor/tsg-python/tsp/src/node-types.json
generated
24
python/extractor/tsg-python/tsp/src/node-types.json
generated
@@ -1309,6 +1309,10 @@
|
||||
"multiple": false,
|
||||
"required": false,
|
||||
"types": [
|
||||
{
|
||||
"type": "exception_list",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "expression",
|
||||
"named": true
|
||||
@@ -1344,6 +1348,26 @@
|
||||
"type": {
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "exception_list",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "expression",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "exception_list",
|
||||
"named": true,
|
||||
"fields": {
|
||||
"element": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "expression",
|
||||
|
||||
91724
python/extractor/tsg-python/tsp/src/parser.c
generated
91724
python/extractor/tsg-python/tsp/src/parser.c
generated
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,4 @@
|
||||
---
|
||||
category: feature
|
||||
---
|
||||
* The extractor now supports the new, relaxed syntax `except A, B, C: ...` (which would previously have to be written as `except (A, B, C): ...`) as defined in [PEP-758](https://peps.python.org/pep-0758/). This may cause changes in results for code that uses Python 2-style exception binding (`except Foo, e: ...`). The more modern format, `except Foo as e: ...` (available since Python 2.6) is unaffected.
|
||||
Reference in New Issue
Block a user