Skip to content

Commit

Permalink
Small refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpvorderman committed Jan 22, 2024
1 parent 1f402bc commit 3067a96
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/xopen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# <http://bugs.python.org/issue12786>.
# However, close_fds is not supported on Windows. See
# <https://github.com/marcelm/cutadapt/issues/315>.
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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3067a96

Please sign in to comment.