From 81ccbac103dbb972151ef3ca9d47001696cea2a1 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Tue, 19 Jul 2016 09:52:09 +1000 Subject: [PATCH] Close the temporary file before using it An instance of NamedTemporaryFile would keep the file open in the current process, however, on Windows, a opened filed is no accessible from any other process, and thus the following commands use this file would fail to execute. To fix this, this commit ensures that the temporary file has been closed before it is used anywhere else, and removes the temporary file after everything gets done. --- ports/geckolib/gecko_bindings/tools/regen.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ports/geckolib/gecko_bindings/tools/regen.py b/ports/geckolib/gecko_bindings/tools/regen.py index 82897ffdb8d..a4c2dde4d51 100755 --- a/ports/geckolib/gecko_bindings/tools/regen.py +++ b/ports/geckolib/gecko_bindings/tools/regen.py @@ -284,10 +284,11 @@ def build(objdir, target_name, kind_name=None, print("[RUSTC]... ", end='') sys.stdout.flush() - tests_file = tempfile.NamedTemporaryFile() + with tempfile.NamedTemporaryFile(delete=False) as f: + test_file = f.name output = None try: - rustc_command = ["rustc", output_filename, "--test", "-o", tests_file.name] + rustc_command = ["rustc", output_filename, "--test", "-o", test_file] output = subprocess.check_output(rustc_command, stderr=subprocess.STDOUT) output = output.decode('utf8') except subprocess.CalledProcessError as e: @@ -299,17 +300,17 @@ def build(objdir, target_name, kind_name=None, if verbose: print(output) - tests_file.file.close() print("[RUSTC_TEST]... ", end='') sys.stdout.flush() try: - output = subprocess.check_output([tests_file.name], stderr=subprocess.STDOUT) + output = subprocess.check_output([test_file], stderr=subprocess.STDOUT) output = output.decode('utf8') except subprocess.CalledProcessError as e: print("tests failed: ", e.output.decode('utf8')) return 1 + os.remove(test_file) print("OK") # TODO: this -3 is hacky as heck