Commit c0937f1a by David Trowbridge

Fix an UnboundLocalError if a compiler fails.

Depending on where an exception is raised inside a compiler's
`execute_command`, the `stdout` variable may not be assigned. In this case, the
finally handler would fail with an `UnboundLocalError`, hiding the true cause
of the exception. This change adds a new variable, `stdout_name`, which is
populated once we've actually created the file. After this, the `CompilerError`
is successfully raised with the contents of the true error.
parent 0db06ec1
...@@ -112,9 +112,12 @@ class SubProcessCompiler(CompilerBase): ...@@ -112,9 +112,12 @@ class SubProcessCompiler(CompilerBase):
argument_list.extend(flattening_arg) argument_list.extend(flattening_arg)
try: try:
stdout_name = None
# We always catch stdout in a file, but we may not have a use for it. # We always catch stdout in a file, but we may not have a use for it.
temp_file_container = cwd or os.path.dirname(stdout_captured or "") or os.getcwd() temp_file_container = cwd or os.path.dirname(stdout_captured or "") or os.getcwd()
with NamedTemporaryFile(delete=False, dir=temp_file_container) as stdout: with NamedTemporaryFile(delete=False, dir=temp_file_container) as stdout:
stdout_name = stdout.name
compiling = subprocess.Popen(argument_list, cwd=cwd, compiling = subprocess.Popen(argument_list, cwd=cwd,
stdout=stdout, stdout=stdout,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
...@@ -135,7 +138,8 @@ class SubProcessCompiler(CompilerBase): ...@@ -135,7 +138,8 @@ class SubProcessCompiler(CompilerBase):
raise CompilerError(e) raise CompilerError(e)
finally: finally:
# Decide what to do with captured stdout. # Decide what to do with captured stdout.
if stdout_captured: if stdout_name:
os.rename(stdout.name, os.path.join(cwd or os.curdir, stdout_captured)) if stdout_captured:
else: os.rename(stdout_name, os.path.join(cwd or os.curdir, stdout_captured))
os.remove(stdout.name) else:
os.remove(stdout_name)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment