Skip to content

Commit 072a430

Browse files
nashifkartben
authored andcommitted
twister: handler: open files using with statement
Use with statement to open stdout/stderr and resolve current warning when running twister: ResourceWarning: unclosed file.... Signed-off-by: Anas Nashif <[email protected]>
1 parent ef6f804 commit 072a430

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

scripts/pylib/twister/twisterlib/handlers.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,9 @@ def handle(self, harness):
354354
return
355355

356356
stderr_log = f"{self.instance.build_dir}/handler_stderr.log"
357-
with (
358-
open(stderr_log, "w+") as stderr_log_fp,
359-
subprocess.Popen(
360-
command, stdout=subprocess.PIPE, stderr=stderr_log_fp, cwd=self.build_dir, env=env
361-
) as proc,
362-
):
357+
with open(stderr_log, "w+") as stderr_log_fp, subprocess.Popen(
358+
command, stdout=subprocess.PIPE, stderr=stderr_log_fp, cwd=self.build_dir, env=env
359+
) as proc:
363360
logger.debug(f"Spawning BinaryHandler Thread for {self.name}")
364361
t = threading.Thread(target=self._output_handler, args=(proc, harness,), daemon=True)
365362
t.start()
@@ -1102,10 +1099,11 @@ def handle(self, harness):
11021099
is_timeout = False
11031100
qemu_pid = None
11041101

1105-
with subprocess.Popen(
1102+
with open(self.stdout_fn, "w") as stdout_fp, \
1103+
open(self.stderr_fn, "w") as stderr_fp, subprocess.Popen(
11061104
command,
1107-
stdout=open(self.stdout_fn, "w"),
1108-
stderr=open(self.stderr_fn, "w"),
1105+
stdout=stdout_fp,
1106+
stderr=stderr_fp,
11091107
cwd=self.build_dir
11101108
) as proc:
11111109
logger.debug(f"Spawning QEMUHandler Thread for {self.name}")

0 commit comments

Comments
 (0)