Commit 6ba65b0
committed
Python: Add support for PEP-758 exception syntax
See https://peps.python.org/pep-0758/ for more details.
We implement this by extending the syntax for exceptions and exception
groups so that the `type` field can now contain either an expression
(which matches the old behaviour), or a comma-separated list of at least
two elements (representing the new behaviour).
We model the latter case using a new node type `exception_list`, which
in `tsg-python` is simply mapped to a tuple. This means it matches the
existing behaviour (when the tuple is surrounded by parentheses)
exactly, hence we don't need to change any other code.
As a consequence of this, however, we cannot directly parse the Python
2.7 syntax `except Foo, e: ...` as `except Foo as e: ...`, as this would
introduce an ambiguity in the grammar. Thus, we have removed support for
the (deprecated) 2.7-style syntax, and only allow `as` to indicate
binding of the exception. The syntax `except Foo, e: ...` continues to
be parsed (in particular, it's not suddenly a syntax error), but it will
be parsed as if it were `except (Foo, e): ...`, which may not give the
correct results.
In principle we could extend the QL libraries to account for this case
(specifically when analysing Python 2 code). In practice, however, I
expect this to have a minor impact on results, and not worth the
additional investment at this time.1 parent a35fba1 commit 6ba65b0
2 files changed
+17
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
3445 | 3445 | | |
3446 | 3446 | | |
3447 | 3447 | | |
| 3448 | + | |
| 3449 | + | |
| 3450 | + | |
3448 | 3451 | | |
3449 | 3452 | | |
3450 | 3453 | | |
| |||
3480 | 3483 | | |
3481 | 3484 | | |
3482 | 3485 | | |
| 3486 | + | |
3483 | 3487 | | |
3484 | 3488 | | |
3485 | 3489 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
300 | 309 | | |
301 | 310 | | |
302 | 311 | | |
303 | | - | |
| 312 | + | |
304 | 313 | | |
305 | | - | |
| 314 | + | |
306 | 315 | | |
307 | 316 | | |
308 | 317 | | |
| |||
314 | 323 | | |
315 | 324 | | |
316 | 325 | | |
317 | | - | |
| 326 | + | |
318 | 327 | | |
319 | 328 | | |
320 | 329 | | |
| |||
0 commit comments