|
3 | 3 | import contextlib
|
4 | 4 | import functools
|
5 | 5 | import itertools
|
| 6 | +import sys |
6 | 7 | import types
|
7 | 8 | import uuid
|
8 | 9 | from collections import OrderedDict
|
@@ -470,7 +471,7 @@ def _def_ast(ctx: CompilerContext, form: llist.List) -> ASTStream:
|
470 | 471 |
|
471 | 472 | try:
|
472 | 473 | def_nodes, def_value = _nodes_and_expr(_to_ast(ctx, form[2]))
|
473 |
| - except KeyError: |
| 474 | + except IndexError: |
474 | 475 | def_nodes, def_value = [], None
|
475 | 476 |
|
476 | 477 | meta_nodes, meta = _nodes_and_exprl(_meta_kwargs_ast(ctx, form[1]))
|
@@ -1448,8 +1449,25 @@ def _resolve_sym(ctx: CompilerContext, form: sym.Symbol) -> Optional[str]:
|
1448 | 1449 |
|
1449 | 1450 | # Otherwise, try to direct-link it like a Python variable
|
1450 | 1451 | safe_ns = munge(form.ns)
|
| 1452 | + try: |
| 1453 | + ns_module = sys.modules[safe_ns] |
| 1454 | + except KeyError: |
| 1455 | + # This should never happen. A module listed in the namespace |
| 1456 | + # imports should always be imported already. |
| 1457 | + raise CompilerException(f"Module '{safe_ns}' is not imported") |
| 1458 | + |
| 1459 | + # Try without allowing builtins first |
1451 | 1460 | safe_name = munge(form.name)
|
1452 |
| - return f"{safe_ns}.{safe_name}" |
| 1461 | + if safe_name in ns_module.__dict__: |
| 1462 | + return f"{safe_ns}.{safe_name}" |
| 1463 | + |
| 1464 | + # Then allow builtins |
| 1465 | + safe_name = munge(form.name, allow_builtins=True) |
| 1466 | + if safe_name in ns_module.__dict__: |
| 1467 | + return f"{safe_ns}.{safe_name}" |
| 1468 | + |
| 1469 | + # If neither resolve, then defer to a Var.find |
| 1470 | + return None |
1453 | 1471 | elif ns_sym in ctx.current_ns.aliases:
|
1454 | 1472 | aliased_ns = ctx.current_ns.aliases[ns_sym]
|
1455 | 1473 | v = Var.find(sym.symbol(form.name, ns=aliased_ns))
|
|
0 commit comments