(compiler, expr, root, body, catchers, orelse, finalbody)
| 1462 | ], |
| 1463 | ) |
| 1464 | def compile_try_expression(compiler, expr, root, body, catchers, orelse, finalbody): |
| 1465 | if orelse is not None and not catchers: |
| 1466 | # Python forbids `else` when there are no `except` clauses. |
| 1467 | # But we can get the same effect by appending the `else` forms |
| 1468 | # to the body. |
| 1469 | body += list(orelse) |
| 1470 | orelse = None |
| 1471 | body = compiler._compile_branch(body) |
| 1472 | if not (catchers or finalbody): |
| 1473 | # Python forbids this, so just return the body, per `do`. |
| 1474 | return body |
| 1475 | |
| 1476 | return_var = asty.Name(expr, id=mangle(compiler.get_anon_var()), ctx=ast.Store()) |
| 1477 | |
| 1478 | handler_results = Result() |
| 1479 | handlers = [] |
| 1480 | except_syms_seen = set() |
| 1481 | for catcher in catchers: |
| 1482 | # exceptions catch should be either: |
| 1483 | # [[list of exceptions]] |
| 1484 | # or |
| 1485 | # [variable [list of exceptions]] |
| 1486 | # or |
| 1487 | # [variable exception] |
| 1488 | # or |
| 1489 | # [exception] |
| 1490 | # or |
| 1491 | # [] |
| 1492 | except_sym, exceptions, ebody = catcher |
| 1493 | if not PY3_11 and except_sym == Symbol("except*"): |
| 1494 | compiler._syntax_error(except_sym, "`{}` requires Python 3.11 or later") |
| 1495 | except_syms_seen.add(str(except_sym)) |
| 1496 | if len(except_syms_seen) > 1: |
| 1497 | compiler._syntax_error( |
| 1498 | except_sym, "cannot have both `except` and `except*` on the same `try`" |
| 1499 | ) |
| 1500 | |
| 1501 | name = None |
| 1502 | if len(exceptions) == 0: |
| 1503 | exceptions = "ALL" |
| 1504 | elif len(exceptions) == 1: |
| 1505 | [exceptions] = exceptions |
| 1506 | else: |
| 1507 | [name, exceptions] = exceptions |
| 1508 | name = mangle(compiler._nonconst(name)) |
| 1509 | |
| 1510 | if exceptions == "ALL": |
| 1511 | # Catch all exceptions. |
| 1512 | types = Result() |
| 1513 | elif isinstance(exceptions, List): |
| 1514 | # [FooBar BarFoo] → Catch Foobar and BarFoo exceptions. |
| 1515 | elts, types, _ = compiler._compile_collect(exceptions) |
| 1516 | types += asty.Tuple(exceptions, elts=elts, ctx=ast.Load()) |
| 1517 | else: |
| 1518 | # A single exception type. |
| 1519 | types = compiler.compile(exceptions) |
| 1520 | |
| 1521 | # Create a "fake" scope for the exception variable. |
nothing calls this directly
no test coverage detected