Skip to content

Commit

Permalink
Imports must appear first, and produced after processing defines
Browse files Browse the repository at this point in the history
  • Loading branch information
malthe committed Jan 29, 2024
1 parent 6b4e52f commit 75c3fdd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Changes

In next release ...

-
- Fix a regression where a static symbol would not get correctly
imported.
(`#414 <https://github.com/malthe/chameleon/issues/414>`_)

4.5.1 (2024-01-28)
------------------
Expand Down
8 changes: 4 additions & 4 deletions src/chameleon/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ def visit_Module(self, module) -> AST:

for name, node in self.defines.items():
assignment = Assign(targets=[store(name)], value=node, lineno=None)
preamble.append(assignment)
preamble.append(self.visit(assignment))

imports: list[AST] = []
for value, node in self.imports.items():
stmt: AST

Expand All @@ -183,10 +184,9 @@ def visit_Module(self, module) -> AST:
else:
raise TypeError(value)

preamble.append(stmt)
imports.append(stmt)

preamble = [self.visit(stmt) for stmt in preamble]
return Module(preamble + module.body, ())
return Module(imports + preamble + module.body, ())

def visit_Comment(self, node) -> AST:
self.comments.append(node.text)
Expand Down

0 comments on commit 75c3fdd

Please sign in to comment.