Skip to content

Commit bb55fc9

Browse files
authored
Merge pull request #188 from pycompression/issue187
No compression level errors should be thrown in read mode.
2 parents 970c141 + be6bdc7 commit bb55fc9

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Changelog
99
1010
version 1.6.0-dev
1111
-----------------
12+
+ Fix a bug where compression levels for IGzipFile where checked in read mode.
1213
+ Update statically linked ISA-L release to 2.31.0
1314
+ Fix an error that occurred in the ``__close__`` function when a threaded
1415
writer was initialized with incorrect parameters.

src/isal/igzip.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ def __init__(self, filename=None, mode=None,
151151
If omitted or None, the current time is used.
152152
"""
153153
if not (isal_zlib.ISAL_BEST_SPEED <= compresslevel
154-
<= isal_zlib.ISAL_BEST_COMPRESSION):
154+
<= isal_zlib.ISAL_BEST_COMPRESSION) and "r" not in mode:
155155
raise ValueError(
156-
"Compression level should be between {0} and {1}.".format(
157-
isal_zlib.ISAL_BEST_SPEED, isal_zlib.ISAL_BEST_COMPRESSION
158-
))
156+
f"Compression level should be between "
157+
f"{isal_zlib.ISAL_BEST_SPEED} and "
158+
f"{isal_zlib.ISAL_BEST_COMPRESSION}, got {compresslevel}."
159+
)
159160
super().__init__(filename, mode, compresslevel, fileobj, mtime)
160161
if self.mode == WRITE:
161162
self.compress = isal_zlib.compressobj(compresslevel,

tests/test_gzip_compliance.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,13 @@ def test_with_open(self):
456456
else:
457457
self.fail("1/0 didn't raise an exception")
458458

459+
def test_read_and_compresslevel(self):
460+
with igzip.GzipFile(self.filename, "wb") as f:
461+
f.write(b"xxx")
462+
with igzip.GzipFile(self.filename, "rb", compresslevel=17) as f:
463+
data = f.read()
464+
assert data == b"xxx"
465+
459466
def test_zero_padded_file(self):
460467
with igzip.GzipFile(self.filename, "wb") as f:
461468
f.write(data1 * 50)
@@ -785,6 +792,13 @@ def test_newline(self):
785792
with igzip.open(self.filename, "rt", newline="\r") as f:
786793
self.assertEqual(f.readlines(), [uncompressed])
787794

795+
def test_reading_and_compresslevel(self):
796+
with igzip.open(self.filename, "wb") as f:
797+
f.write(data1)
798+
with igzip.open(self.filename, "rb", compresslevel=17) as f:
799+
text = f.read()
800+
assert text == data1
801+
788802

789803
def create_and_remove_directory(directory):
790804
def decorator(function):

tests/test_igzip_threaded.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,11 @@ def test_igzip_threaded_append_text_mode(tmp_path):
191191
with gzip.open(test_file, "rt") as f:
192192
contents = f.read()
193193
assert contents == "ABCD"
194+
195+
196+
def test_igzip_threaded_open_compresslevel_and_reading(tmp_path):
197+
test_file = tmp_path / "test.txt.gz"
198+
test_file.write_bytes(gzip.compress(b"thisisatest"))
199+
with igzip_threaded.open(test_file, compresslevel=5) as f:
200+
text = f.read()
201+
assert text == b"thisisatest"

0 commit comments

Comments
 (0)