From dba58b27e187ca933bdd290ab96b0fa80974dd63 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Fri, 6 Sep 2024 14:19:42 -0500 Subject: [PATCH] Fix EmPy interpreter shutdown on construction error If the interpreter fails to construct, we don't want to reference it later. Instead, create a separate try/finally block to deal with interpreter shutdown. --- colcon_core/shell/template/__init__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/colcon_core/shell/template/__init__.py b/colcon_core/shell/template/__init__.py index 842220c89..c6ba34764 100644 --- a/colcon_core/shell/template/__init__.py +++ b/colcon_core/shell/template/__init__.py @@ -37,10 +37,13 @@ def expand_template(template_path, destination_path, data): # disable OVERRIDE_OPT to avoid saving / restoring stdout interpreter = CachingInterpreter( output=output, options={OVERRIDE_OPT: False}) - with template_path.open('r') as h: - content = h.read() - interpreter.string(content, str(template_path), locals=data) - output = output.getvalue() + try: + with template_path.open('r') as h: + content = h.read() + interpreter.string(content, str(template_path), locals=data) + output = output.getvalue() + finally: + interpreter.shutdown() except Exception as e: # noqa: F841 logger.error( f"{e.__class__.__name__} processing template '{template_path}'") @@ -53,8 +56,6 @@ def expand_template(template_path, destination_path, data): destination_path.unlink() with destination_path.open('w') as h: h.write(output) - finally: - interpreter.shutdown() class BypassStdoutInterpreter(Interpreter):