Skip to content

Commit

Permalink
Merge pull request #193 from pycompression/release_1.6.1
Browse files Browse the repository at this point in the history
Release 1.6.1
  • Loading branch information
rhpvorderman authored Mar 11, 2024
2 parents ffe04cb + a16ad97 commit aa4a483
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Changelog
.. This document is user facing. Please word the changes in such a way
.. that users understand how the changes affect the new version.
version 1.6.1
-----------------
+ Fix a bug where streams that were passed to igzip_threaded.open where closed.

version 1.6.0
-----------------
+ Fix a bug where compression levels for IGzipFile where checked in read mode.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def build_isa_l():

setup(
name="isal",
version="1.6.0",
version="1.6.1",
description="Faster zlib and gzip compatible compression and "
"decompression by providing python bindings for the ISA-L "
"library.",
Expand Down
2 changes: 1 addition & 1 deletion src/isal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"__version__"
]

__version__ = "1.6.0"
__version__ = "1.6.1"
14 changes: 9 additions & 5 deletions src/isal/igzip_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,18 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
def open_as_binary_stream(filename, open_mode):
if isinstance(filename, (str, bytes)) or hasattr(filename, "__fspath__"):
binary_file = builtins.open(filename, open_mode)
closefd = True
elif hasattr(filename, "read") or hasattr(filename, "write"):
binary_file = filename
closefd = False
else:
raise TypeError("filename must be a str or bytes object, or a file")
return binary_file
return binary_file, closefd


class _ThreadedGzipReader(io.RawIOBase):
def __init__(self, filename, queue_size=2, block_size=1024 * 1024):
self.raw = open_as_binary_stream(filename, "rb")
self.raw, self.closefd = open_as_binary_stream(filename, "rb")
self.fileobj = igzip._IGzipReader(self.raw, buffersize=8 * block_size)
self.pos = 0
self.read_file = False
Expand Down Expand Up @@ -155,7 +157,8 @@ def close(self) -> None:
self.running = False
self.worker.join()
self.fileobj.close()
self.raw.close()
if self.closefd:
self.raw.close()
self._closed = True

@property
Expand Down Expand Up @@ -246,7 +249,7 @@ def __init__(self,
self._crc = 0
self.running = False
self._size = 0
self.raw = open_as_binary_stream(filename, mode)
self.raw, self.closefd = open_as_binary_stream(filename, mode)
self._closed = False
self._write_gzip_header()
self.start()
Expand Down Expand Up @@ -329,7 +332,8 @@ def close(self) -> None:
trailer = struct.pack("<II", self._crc, self._size & 0xFFFFFFFF)
self.raw.write(trailer)
self.raw.flush()
self.raw.close()
if self.closefd:
self.raw.close()
self._closed = True

@property
Expand Down
19 changes: 19 additions & 0 deletions tests/test_igzip_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,22 @@ def test_igzip_threaded_open_compresslevel_and_reading(tmp_path):
with igzip_threaded.open(test_file, compresslevel=5) as f:
text = f.read()
assert text == b"thisisatest"


def test_threaded_reader_does_not_close_stream():
test_stream = io.BytesIO()
test_stream.write(gzip.compress(b"thisisatest"))
test_stream.seek(0)
with igzip_threaded.open(test_stream, "rb") as f:
text = f.read()
assert not test_stream.closed
assert text == b"thisisatest"


def test_threaded_writer_does_not_close_stream():
test_stream = io.BytesIO()
with igzip_threaded.open(test_stream, "wb") as f:
f.write(b"thisisatest")
assert not test_stream.closed
test_stream.seek(0)
assert gzip.decompress(test_stream.read()) == b"thisisatest"

0 comments on commit aa4a483

Please sign in to comment.