Skip to content

Commit c0937f1

Browse files
committed
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.
1 parent 0db06ec commit c0937f1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pipeline/compilers/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,12 @@ def execute_command(self, command, cwd=None, stdout_captured=None):
112112
argument_list.extend(flattening_arg)
113113

114114
try:
115+
stdout_name = None
116+
115117
# We always catch stdout in a file, but we may not have a use for it.
116118
temp_file_container = cwd or os.path.dirname(stdout_captured or "") or os.getcwd()
117119
with NamedTemporaryFile(delete=False, dir=temp_file_container) as stdout:
120+
stdout_name = stdout.name
118121
compiling = subprocess.Popen(argument_list, cwd=cwd,
119122
stdout=stdout,
120123
stderr=subprocess.PIPE)
@@ -135,7 +138,8 @@ def execute_command(self, command, cwd=None, stdout_captured=None):
135138
raise CompilerError(e)
136139
finally:
137140
# Decide what to do with captured stdout.
138-
if stdout_captured:
139-
os.rename(stdout.name, os.path.join(cwd or os.curdir, stdout_captured))
140-
else:
141-
os.remove(stdout.name)
141+
if stdout_name:
142+
if stdout_captured:
143+
os.rename(stdout_name, os.path.join(cwd or os.curdir, stdout_captured))
144+
else:
145+
os.remove(stdout_name)

0 commit comments

Comments
 (0)