Skip to content

Commit 6c4a711

Browse files
authored
Resolve names in catch blocks properly (#273)
1 parent 8c2cb41 commit 6c4a711

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/basilisp/lang/compiler.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,10 +1164,18 @@ def _catch_ast(ctx: CompilerContext, form: llist.List) -> ast.ExceptHandler:
11641164
assert isinstance(form[1], sym.Symbol)
11651165
assert isinstance(form[2], sym.Symbol)
11661166

1167-
body = seq(form[3:]).flat_map(lambda f: _to_ast(ctx, f)).map(_unwrap_node).to_list()
1167+
type_name = form[1].name
1168+
if form[1].ns is not None:
1169+
type_name = f"{form[1].ns}.{type_name}"
1170+
1171+
exc_name = munge(form[2].name)
1172+
with ctx.new_symbol_table(genname('catch_block')):
1173+
ctx.symbol_table.new_symbol(form[2], exc_name, _SYM_CTX_LOCAL)
1174+
body = seq(form[3:]).flat_map(lambda f: _to_ast(ctx, f)).map(_unwrap_node).to_list()
1175+
11681176
return ast.ExceptHandler(
1169-
type=ast.Name(id=form[1].name, ctx=ast.Load()),
1170-
name=munge(form[2].name),
1177+
type=_load_attr(type_name),
1178+
name=exc_name,
11711179
body=list(_catch_expr_body(body)))
11721180

11731181

tests/basilisp/compiler_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,13 @@ def test_try_catch(capsys, ns: runtime.Namespace):
648648
"""
649649
assert "lower" == lcompile(code)
650650

651+
code = """
652+
(try
653+
(.fake-lower "UPPER")
654+
(catch builtins/AttributeError e (.-args e)))
655+
"""
656+
assert ("'str' object has no attribute 'fake_lower'",) == lcompile(code)
657+
651658
code = """
652659
(try
653660
(.fake-lower "UPPER")

0 commit comments

Comments
 (0)