From 75c3fdd0fd38bad734391a7fcae80ec7eb44497d Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Mon, 29 Jan 2024 11:55:22 +0100 Subject: [PATCH] Imports must appear first, and produced after processing defines --- CHANGES.rst | 4 +++- src/chameleon/codegen.py | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index ddb44ca..2d9d659 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,7 +3,9 @@ Changes In next release ... -- +- Fix a regression where a static symbol would not get correctly + imported. + (`#414 `_) 4.5.1 (2024-01-28) ------------------ diff --git a/src/chameleon/codegen.py b/src/chameleon/codegen.py index 28da13c..2006251 100644 --- a/src/chameleon/codegen.py +++ b/src/chameleon/codegen.py @@ -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 @@ -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)