t/fiotestlib: use 'with' for opening files
authorVincent Fu <vincent.fu@samsung.com>
Mon, 5 Jun 2023 17:59:55 +0000 (17:59 +0000)
committerVincent Fu <vincent.fu@samsung.com>
Thu, 8 Jun 2023 18:39:07 +0000 (14:39 -0400)
Use with when opening files to relieve us of the need to explicitly
close the files.

Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
t/fiotestlib.py

index e5ccc13f2b471bc022762e86f827ce9d0ec7f9e5..8565f63508dbf8ded837b314c6fa4d93b836976e 100755 (executable)
@@ -81,32 +81,31 @@ class FioExeTest(FioTest):
         """Execute the binary or script described by this instance."""
 
         command = [self.paths['exe']] + self.parameters
-        command_file = open(self.filenames['cmd'], "w+",
-                            encoding=locale.getpreferredencoding())
-        command_file.write(f"{command}\n")
-        command_file.close()
-
-        stdout_file = open(self.filenames['stdout'], "w+",
-                           encoding=locale.getpreferredencoding())
-        stderr_file = open(self.filenames['stderr'], "w+",
-                           encoding=locale.getpreferredencoding())
-        exitcode_file = open(self.filenames['exitcode'], "w+",
-                             encoding=locale.getpreferredencoding())
+        with open(self.filenames['cmd'], "w+",
+                  encoding=locale.getpreferredencoding()) as command_file:
+            command_file.write(f"{command}\n")
+
         try:
-            proc = None
-            # Avoid using subprocess.run() here because when a timeout occurs,
-            # fio will be stopped with SIGKILL. This does not give fio a
-            # chance to clean up and means that child processes may continue
-            # running and submitting IO.
-            proc = subprocess.Popen(command,
-                                    stdout=stdout_file,
-                                    stderr=stderr_file,
-                                    cwd=self.paths['test_dir'],
-                                    universal_newlines=True)
-            proc.communicate(timeout=self.success['timeout'])
-            exitcode_file.write(f'{proc.returncode}\n')
-            logging.debug("Test %d: return code: %d", self.testnum, proc.returncode)
-            self.output['proc'] = proc
+            with open(self.filenames['stdout'], "w+",
+                      encoding=locale.getpreferredencoding()) as stdout_file, \
+                open(self.filenames['stderr'], "w+",
+                     encoding=locale.getpreferredencoding()) as stderr_file, \
+                open(self.filenames['exitcode'], "w+",
+                     encoding=locale.getpreferredencoding()) as exitcode_file:
+                proc = None
+                # Avoid using subprocess.run() here because when a timeout occurs,
+                # fio will be stopped with SIGKILL. This does not give fio a
+                # chance to clean up and means that child processes may continue
+                # running and submitting IO.
+                proc = subprocess.Popen(command,
+                                        stdout=stdout_file,
+                                        stderr=stderr_file,
+                                        cwd=self.paths['test_dir'],
+                                        universal_newlines=True)
+                proc.communicate(timeout=self.success['timeout'])
+                exitcode_file.write(f'{proc.returncode}\n')
+                logging.debug("Test %d: return code: %d", self.testnum, proc.returncode)
+                self.output['proc'] = proc
         except subprocess.TimeoutExpired:
             proc.terminate()
             proc.communicate()
@@ -119,10 +118,6 @@ class FioExeTest(FioTest):
                     proc.communicate()
             self.output['failure'] = 'exception'
             self.output['exc_info'] = sys.exc_info()
-        finally:
-            stdout_file.close()
-            stderr_file.close()
-            exitcode_file.close()
 
     def check_result(self):
         """Check results of test run."""