src/basilisp/lang/compiler/generator.py
def _try_to_py_ast(ctx: GeneratorContext, node: Try) -> GeneratedPyAST:
    """Return a Python AST Node for a `try` expression."""
    assert node.op == NodeOp.TRY

    try_expr_name = genname("try_expr")

    body_ast = _synthetic_do_to_py_ast(ctx, node.body)
    catch_handlers = list(
        map(partial(__catch_to_py_ast, ctx, try_expr_name=try_expr_name), node.catches)
    )

    finallys: List[ast.AST] = []
    if node.finally_ is not None:
        finally_ast = _synthetic_do_to_py_ast(ctx, node.finally_)
        finallys.extend(map(statementize, finally_ast.dependencies))
        finallys.append(statementize(finally_ast.node))

    return GeneratedPyAST(
        node=ast.Name(id=try_expr_name, ctx=ast.Load()),
        dependencies=[
            ast.Try(
                body=list(
                    chain(
                        body_ast.dependencies,
                        [
                            ast.Assign(
                                targets=[ast.Name(id=try_expr_name, ctx=ast.Store())],
                                value=body_ast.node,
                            )
                        ],
                    )
                ),
                handlers=catch_handlers,
                orelse=[],
                finalbody=finallys,
            )
        ],
    )
def _try_to_py_ast(ctx: GeneratorContext, node: Try) -> GeneratedPyAST:
    assert node.op == NodeOp.TRY

    try_expr_name = genname("try_expr")

    body_ast = _synthetic_do_to_py_ast(ctx, node.body)
    catch_handlers = list(
        map(partial(__catch_to_py_ast, ctx, try_expr_name=try_expr_name), node.catches)
    )

    finallys: List[ast.AST] = []
    if node.finally_ is not None:
        finally_ast = _synthetic_do_to_py_ast(ctx, node.finally_)
        finallys.extend(map(statementize, finally_ast.dependencies))
        finallys.append(statementize(finally_ast.node))

    return GeneratedPyAST(
        node=ast.Name(id=try_expr_name, ctx=ast.Load()),
        dependencies=[
            ast.Try(
                body=list(
                    chain(
                        body_ast.dependencies,
                        [
                            ast.Assign(
                                targets=[ast.Name(id=try_expr_name, ctx=ast.Store())],
                                value=body_ast.node,
                            )
                        ],
                    )
                ),
                handlers=catch_handlers,
                orelse=[],
                finalbody=finallys,
            )
        ],
    )
def _try_to_py_ast ( ctx : GeneratorContext , node : Try ) -> GeneratedPyAST : assert node . op == NodeOp . TRY try_expr_name = genname ( "try_expr" ) body_ast = _synthetic_do_to_py_ast ( ctx , node . body ) catch_handlers = list ( map ( partial ( __catch_to_py_ast , ctx , try_expr_name = try_expr_name ) , node . catches ) ) finallys : List [ ast . AST ] = [ ] if node . finally_ is not None : finally_ast = _synthetic_do_to_py_ast ( ctx , node . finally_ ) finallys . extend ( map ( statementize , finally_ast . dependencies ) ) finallys . append ( statementize ( finally_ast . node ) ) return GeneratedPyAST ( node = ast . Name ( id = try_expr_name , ctx = ast . Load ( ) ) , dependencies = [ ast . Try ( body = list ( chain ( body_ast . dependencies , [ ast . Assign ( targets = [ ast . Name ( id = try_expr_name , ctx = ast . Store ( ) ) ] , value = body_ast . node , ) ] , ) ) , handlers = catch_handlers , orelse = [ ] , finalbody = finallys , ) ] , )
Return a Python AST Node for a `try` expression.
Return a Python AST Node for a try expression .