Skip to content

Commit 953f5c7

Browse files
committed
Preemprively raise an error on wrong level
1 parent 720f1d9 commit 953f5c7

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/isal/igzip_threaded.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ def __init__(self,
172172
level: int = isal_zlib.ISAL_DEFAULT_COMPRESSION,
173173
threads: int = 1,
174174
queue_size: int = 2):
175+
if level < 0 or level > 3:
176+
raise ValueError(
177+
f"Invalid compression level, "
178+
f"level should be between 0 and 3: {level}")
175179
self.lock = threading.Lock()
176180
self.exception: Optional[Exception] = None
177181
self.raw = fp

tests/test_igzip_threaded.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ def test_threaded_read_error():
7373

7474

7575
@pytest.mark.timeout(5)
76-
def test_threaded_write_error():
76+
def test_threaded_write_error(monkeypatch):
7777
tmp = tempfile.mktemp()
78-
with pytest.raises(ValueError) as error:
79-
with igzip_threaded.open(tmp, "wb", compresslevel=43) as writer:
78+
# Compressobj method is called in a worker thread.
79+
monkeypatch.delattr(igzip_threaded.isal_zlib, "compressobj")
80+
with pytest.raises(AttributeError) as error:
81+
with igzip_threaded.open(tmp, "wb", compresslevel=3) as writer:
8082
writer.write(b"x")
81-
error.match("compression level")
83+
error.match("no attribute 'compressobj'")

0 commit comments

Comments
 (0)