Skip to content

Commit

Permalink
Merge pull request #188 from pycompression/issue187
Browse files Browse the repository at this point in the history
No compression level errors should be thrown in read mode.
  • Loading branch information
rhpvorderman authored Feb 19, 2024
2 parents 970c141 + be6bdc7 commit bb55fc9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Changelog
version 1.6.0-dev
-----------------
+ Fix a bug where compression levels for IGzipFile where checked in read mode.
+ Update statically linked ISA-L release to 2.31.0
+ Fix an error that occurred in the ``__close__`` function when a threaded
writer was initialized with incorrect parameters.
Expand Down
9 changes: 5 additions & 4 deletions src/isal/igzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ def __init__(self, filename=None, mode=None,
If omitted or None, the current time is used.
"""
if not (isal_zlib.ISAL_BEST_SPEED <= compresslevel
<= isal_zlib.ISAL_BEST_COMPRESSION):
<= isal_zlib.ISAL_BEST_COMPRESSION) and "r" not in mode:
raise ValueError(
"Compression level should be between {0} and {1}.".format(
isal_zlib.ISAL_BEST_SPEED, isal_zlib.ISAL_BEST_COMPRESSION
))
f"Compression level should be between "
f"{isal_zlib.ISAL_BEST_SPEED} and "
f"{isal_zlib.ISAL_BEST_COMPRESSION}, got {compresslevel}."
)
super().__init__(filename, mode, compresslevel, fileobj, mtime)
if self.mode == WRITE:
self.compress = isal_zlib.compressobj(compresslevel,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_gzip_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ def test_with_open(self):
else:
self.fail("1/0 didn't raise an exception")

def test_read_and_compresslevel(self):
with igzip.GzipFile(self.filename, "wb") as f:
f.write(b"xxx")
with igzip.GzipFile(self.filename, "rb", compresslevel=17) as f:
data = f.read()
assert data == b"xxx"

def test_zero_padded_file(self):
with igzip.GzipFile(self.filename, "wb") as f:
f.write(data1 * 50)
Expand Down Expand Up @@ -785,6 +792,13 @@ def test_newline(self):
with igzip.open(self.filename, "rt", newline="\r") as f:
self.assertEqual(f.readlines(), [uncompressed])

def test_reading_and_compresslevel(self):
with igzip.open(self.filename, "wb") as f:
f.write(data1)
with igzip.open(self.filename, "rb", compresslevel=17) as f:
text = f.read()
assert text == data1


def create_and_remove_directory(directory):
def decorator(function):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_igzip_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,11 @@ def test_igzip_threaded_append_text_mode(tmp_path):
with gzip.open(test_file, "rt") as f:
contents = f.read()
assert contents == "ABCD"


def test_igzip_threaded_open_compresslevel_and_reading(tmp_path):
test_file = tmp_path / "test.txt.gz"
test_file.write_bytes(gzip.compress(b"thisisatest"))
with igzip_threaded.open(test_file, compresslevel=5) as f:
text = f.read()
assert text == b"thisisatest"

0 comments on commit bb55fc9

Please sign in to comment.