Skip to content

Commit e07fbee

Browse files
committed
Reunite read and write processes for PipedCompressionProgram
1 parent f61ecf2 commit e07fbee

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/xopen/__init__.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import tempfile
2222
import threading
2323
import time
24-
from subprocess import Popen, PIPE
2524
from typing import (
2625
Dict,
2726
Optional,
@@ -230,41 +229,32 @@ def __init__( # noqa: C901
230229
self._feeding = True
231230
if "r" in mode:
232231
self._program_args += ["-c", "-d"] # type: ignore
233-
try:
234-
self.process = subprocess.Popen(
235-
self._program_args,
236-
stderr=self._stderr,
237-
stdout=PIPE,
238-
stdin=PIPE,
239-
close_fds=close_fds,
240-
) # type: ignore
241-
except OSError:
242-
if self.closefd:
243-
self.fileobj.close()
244-
raise
245-
assert self.process.stdin is not None
232+
stdout = subprocess.PIPE
233+
else:
234+
if compresslevel is not None:
235+
self._program_args += ["-" + str(compresslevel)]
236+
stdout = self.fileobj # type: ignore
237+
try:
238+
self.process = subprocess.Popen(
239+
self._program_args,
240+
stderr=self._stderr,
241+
stdout=stdout,
242+
stdin=subprocess.PIPE,
243+
close_fds=close_fds,
244+
) # type: ignore
245+
except OSError:
246+
if self.closefd:
247+
self.fileobj.close()
248+
raise
249+
assert self.process.stdin is not None
250+
if "r" in mode:
246251
self.in_pipe = self.process.stdin
247252
self.in_thread = threading.Thread(target=self._feed_pipe)
248253
self.in_thread.start()
249254
self._file: BinaryIO = self.process.stdout # type: ignore
250255
self._wait_for_output_or_process_exit()
251256
self._raise_if_error()
252257
else:
253-
if compresslevel is not None:
254-
self._program_args += ["-" + str(compresslevel)]
255-
try:
256-
self.process = Popen(
257-
self._program_args,
258-
stderr=self._stderr,
259-
stdin=PIPE,
260-
stdout=self.fileobj,
261-
close_fds=close_fds,
262-
) # type: ignore
263-
except OSError:
264-
if self.closefd:
265-
self.fileobj.close()
266-
raise
267-
assert self.process.stdin is not None
268258
self._file = self.process.stdin # type: ignore
269259

270260
_set_pipe_size_to_max(self._file.fileno())

0 commit comments

Comments
 (0)