Skip to content

Commit

Permalink
Fix EmPy interpreter shutdown on construction error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cottsay committed Sep 6, 2024
1 parent 4d7ab92 commit dba58b2
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions colcon_core/shell/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'")
Expand All @@ -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):
Expand Down

0 comments on commit dba58b2

Please sign in to comment.