Make sure we close the spooled temporary file

This commit is contained in:
Pedro Algarvio 2017-04-04 12:06:46 +01:00
parent 5dfe43fc2e
commit 008f0016e2
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF

View file

@ -294,7 +294,8 @@ class ShellTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
# As a last resort, kill the process group
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
except OSError as exc:
if exc.errno != 3:
if exc.errno != errno.ESRCH:
# If errno is not "no such process", raise
raise
out = [
@ -369,6 +370,14 @@ class ShellTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
return out, []
finally:
try:
if os.path.exists(tmp_file.name):
if isinstance(tmp_file.name, str):
# tmp_file.name is an int when using SpooledTemporaryFiles
# int types cannot be used with os.remove() in Python 3
os.remove(tmp_file.name)
else:
# Clean up file handles
tmp_file.close()
process.terminate()
except OSError as err:
# process already terminated