From 3067a96cdf80b7e70d02ab8535d5714377a13569 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Mon, 22 Jan 2024 08:58:36 +0100 Subject: [PATCH] Small refactorings --- src/xopen/__init__.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/xopen/__init__.py b/src/xopen/__init__.py index 6a7ae93..868fabc 100644 --- a/src/xopen/__init__.py +++ b/src/xopen/__init__.py @@ -213,21 +213,24 @@ def __init__( self._threads = threads if threads != 0 and self._threads_flag is not None: - self._program_args += [f"{self._threads_flag}{threads}"] + self._program_args += [f"{self._threads_flag}{self._threads}"] # Setting close_fds to True in the Popen arguments is necessary due to # . # However, close_fds is not supported on Windows. See # . - kwargs = {} + close_fds = False if sys.platform != "win32": - kwargs["close_fds"] = True + close_fds = True if "r" in mode: self._program_args += ["-c", "-d", path] # type: ignore self.outfile = None self.process = subprocess.Popen( - self._program_args, stderr=self._stderr, stdout=PIPE, **kwargs + self._program_args, + stderr=self._stderr, + stdout=PIPE, + close_fds=close_fds, ) # type: ignore self._file: BinaryIO = self.process.stdout # type: ignore self._wait_for_output_or_process_exit() @@ -242,7 +245,7 @@ def __init__( stderr=self._stderr, stdin=PIPE, stdout=self.outfile, - **kwargs, + close_fds=close_fds, ) # type: ignore except OSError: self.outfile.close() @@ -293,14 +296,13 @@ def close(self) -> None: if hasattr(self, "_stderr"): self._stderr.close() return - if "r" not in self._mode: + check_allowed_code_and_message = False + if self.outfile: # Opened for writing. self._file.close() self.process.wait() - self.outfile.close() # type: ignore - check_allowed_code_and_message = True + self.outfile.close() else: retcode = self.process.poll() - check_allowed_code_and_message = False if retcode is None: # still running self.process.terminate()