MCPcopy
hub / github.com/hylang/hy / compile_try_expression

Function compile_try_expression

hy/core/result_macros.py:1464–1567  ·  view source on GitHub ↗
(compiler, expr, root, body, catchers, orelse, finalbody)

Source from the content-addressed store, hash-verified

1462 ],
1463)
1464def 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.

Callers

nothing calls this directly

Calls 12

mangleFunction · 0.90
ResultClass · 0.90
SymbolClass · 0.90
_compile_branchMethod · 0.80
get_anon_varMethod · 0.80
_syntax_errorMethod · 0.80
addMethod · 0.80
_nonconstMethod · 0.80
_compile_collectMethod · 0.80
compileMethod · 0.80
createMethod · 0.80
expr_as_stmtMethod · 0.80

Tested by

no test coverage detected