Skip to content

Commit 865cb98

Browse files
committed
Throw OSError when unsupported mode is used
1 parent a1dbaf5 commit 865cb98

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

src/xopen/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ def __init__( # noqa: C901
192192
if isinstance(self._infile, io.IOBase):
193193
# not supported (yet) for reading.
194194
if "r" in mode:
195-
raise ValueError(
196-
"File object not supported for reading through Piped Program."
195+
raise OSError(
196+
f"File object not supported for reading through "
197+
f"{self.__class__.__name__}."
197198
)
198199
path = path.name
199200
if (

tests/test_piped.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,12 @@ def test_piped_tool_fails_on_close(tmp_path):
378378
) as f:
379379
f.write(b"Hello")
380380
assert "exit code 5" in e.value.args[0]
381+
382+
383+
def test_piped_tool_fails_read_mode_with_filehandle(tmp_path):
384+
test_file = tmp_path / "test.txt.gz"
385+
test_file.write_bytes(gzip.compress(b"test"))
386+
with open(test_file, "rb") as filehandle:
387+
with pytest.raises(OSError) as error:
388+
_PipedCompressionProgram(filehandle, "rb")
389+
error.match("File object not supported for reading")

tests/test_xopen.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -578,24 +578,6 @@ def test_pass_file_object_for_reading_no_threads(ext):
578578
assert f.readline() == CONTENT_LINES[0].encode("utf-8")
579579

580580

581-
@pytest.mark.parametrize("ext", extensions)
582-
def test_pass_file_object_for_reading(ext):
583-
if ext == ".zst" and zstandard is None:
584-
return
585-
# non-compressed file do not raise error
586-
if not ext:
587-
return
588-
# todo : disable the gz modules to force piped compression.
589-
with open(TEST_DIR / f"file.txt{ext}", "rb") as fh:
590-
with pytest.raises(ValueError) as e:
591-
with xopen(fh, mode="rb", threads=1) as f:
592-
f.readline() # pragma: no cover
593-
assert (
594-
"File object not supported for reading through Piped Program"
595-
in e.value.args[0]
596-
)
597-
598-
599581
@pytest.mark.parametrize("ext", extensions)
600582
def test_pass_file_object_for_writing(tmp_path, ext):
601583
if ext == ".zst" and zstandard is None:

0 commit comments

Comments
 (0)