Skip to content

Commit

Permalink
Don't use hy.compile to bring hy into scope
Browse files Browse the repository at this point in the history
This change might yield a tiny speedup, but it's more for the sake of simplicity.
  • Loading branch information
Kodiologist committed May 31, 2022
1 parent c366b37 commit 049dcd7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from funcparserlib.parser import NoParseError, many

import hy
from hy._compat import PY3_8
from hy.errors import HyCompileError, HyLanguageError, HySyntaxError
from hy.lex import mangle
Expand Down Expand Up @@ -345,6 +346,8 @@ def __init__(self, module, filename=None, source=None):
self.module = importlib.import_module(module)
else:
self.module = module
self.module.hy = hy
# The `hy` module itself should always be in scope.

self.module_name = self.module.__name__

Expand Down Expand Up @@ -863,12 +866,6 @@ def hy_compile(

compiler = compiler or HyASTCompiler(module, filename=filename, source=source)

if import_stdlib:
# Import hy for compile time, but save the compiled AST.
stdlib_ast = compiler.compile(
mkexpr("eval-and-compile", mkexpr("import", "hy"))
)

with compiler.scope:
result = compiler.compile(tree)
expr = result.force_expr
Expand Down Expand Up @@ -899,7 +896,8 @@ def hy_compile(

# Import hy for runtime.
if import_stdlib:
body += stdlib_ast.stmts
body.append(ast.fix_missing_locations(
ast.Import([ast.alias("hy", "hy")])))

body += result.stmts
ret = root(body=body, type_ignores=[])
Expand Down
4 changes: 2 additions & 2 deletions tests/compilers/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,9 @@ def test_module_prelude():
hy_ast = can_compile("", import_stdlib=True)
assert len(hy_ast.body) == 1
assert isinstance(hy_ast.body[0], ast.Import)
assert hy_ast.body[0].module == "hy"
assert hy_ast.body[0].names[0].name == "hy"

hy_ast = can_compile("(setv flag (- hy.models.Symbol 1))", import_stdlib=True)
assert len(hy_ast.body) == 2
assert isinstance(hy_ast.body[0], ast.Import)
assert hy_ast.body[0].module == "hy"
assert hy_ast.body[0].names[0].name == "hy"

0 comments on commit 049dcd7

Please sign in to comment.