Skip to content

Commit b6ed080

Browse files
committed
Fix several test fails
1 parent ec85410 commit b6ed080

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/xopen/__init__.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,15 @@ def __repr__(self):
271271
)
272272

273273
def _feed_pipe(self):
274-
while self._feeding:
275-
chunk = self.fileobj.read(BUFFER_SIZE)
276-
if chunk == b"":
277-
self.in_pipe.close()
278-
return
279-
self.in_pipe.write(chunk)
274+
try:
275+
while self._feeding:
276+
chunk = self.fileobj.read(BUFFER_SIZE)
277+
if chunk == b"":
278+
self.in_pipe.close()
279+
return
280+
self.in_pipe.write(chunk)
281+
finally:
282+
self.in_pipe.close()
280283

281284
def write(self, arg: bytes) -> int:
282285
return self._file.write(arg)
@@ -312,18 +315,21 @@ def close(self) -> None:
312315
self._stderr.close()
313316
return
314317
check_allowed_code_and_message = False
315-
if self.fileobj:
316-
self._file.close()
317-
self.process.wait()
318-
self.fileobj.close()
319-
else:
318+
if "r" in self._mode:
319+
self._feeding = False
320+
self._file.read()
320321
retcode = self.process.poll()
321322
if retcode is None:
322323
# still running
323324
self.process.terminate()
324325
check_allowed_code_and_message = True
325326
self.process.wait()
327+
if self.in_thread:
328+
self.in_thread.join()
326329
self._file.close()
330+
else:
331+
self._file.close()
332+
self.process.wait()
327333
stderr_message = self._read_error_message()
328334
self._stderr.close()
329335
if not self._error_raised:
@@ -591,6 +597,8 @@ def _detect_format_from_content(fileobj: BinaryIO) -> Optional[str]:
591597
Attempts to detect file format from the content by reading the first
592598
6 bytes. Returns None if no format could be detected.
593599
"""
600+
if not fileobj.readable():
601+
return None
594602
if hasattr(fileobj, "peek"):
595603
bs = fileobj.peek(6)
596604
elif hasattr(fileobj, "seekable") and fileobj.seekable():
@@ -651,7 +659,7 @@ def _file_or_path_to_name_and_binary_stream(
651659
return "", file_or_path
652660
else:
653661
raise TypeError(
654-
f"Unsupported type for {file_or_path:r}, "
662+
f"Unsupported type for {file_or_path}, "
655663
f"{file_or_path.__class__.__name__}."
656664
)
657665

tests/test_xopen.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ def test_readinto(fname):
167167

168168

169169
def test_detect_format_from_content(ext):
170-
detected = _detect_format_from_content(Path(__file__).parent / f"file.txt{ext}")
170+
with open(Path(__file__).parent / f"file.txt{ext}", "rb") as f:
171+
detected = _detect_format_from_content(f)
171172
if ext == "":
172173
assert detected is None
173174
else:
@@ -594,6 +595,8 @@ def test_pass_file_object_for_writing(tmp_path, ext):
594595
def test_pass_bytesio_for_writing(ext):
595596
filelike = io.BytesIO()
596597
format = ext[1:]
598+
if ext == "":
599+
format = None
597600
if ext == ".zst" and zstandard is None:
598601
return
599602
first_line = CONTENT_LINES[0].encode("utf-8")

0 commit comments

Comments
 (0)