Skip to content

Commit aa4a483

Browse files
authored
Merge pull request #193 from pycompression/release_1.6.1
Release 1.6.1
2 parents ffe04cb + a16ad97 commit aa4a483

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Changelog
77
.. This document is user facing. Please word the changes in such a way
88
.. that users understand how the changes affect the new version.
99
10+
version 1.6.1
11+
-----------------
12+
+ Fix a bug where streams that were passed to igzip_threaded.open where closed.
13+
1014
version 1.6.0
1115
-----------------
1216
+ Fix a bug where compression levels for IGzipFile where checked in read mode.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def build_isa_l():
130130

131131
setup(
132132
name="isal",
133-
version="1.6.0",
133+
version="1.6.1",
134134
description="Faster zlib and gzip compatible compression and "
135135
"decompression by providing python bindings for the ISA-L "
136136
"library.",

src/isal/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
"__version__"
1717
]
1818

19-
__version__ = "1.6.0"
19+
__version__ = "1.6.1"

src/isal/igzip_threaded.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,18 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
7878
def open_as_binary_stream(filename, open_mode):
7979
if isinstance(filename, (str, bytes)) or hasattr(filename, "__fspath__"):
8080
binary_file = builtins.open(filename, open_mode)
81+
closefd = True
8182
elif hasattr(filename, "read") or hasattr(filename, "write"):
8283
binary_file = filename
84+
closefd = False
8385
else:
8486
raise TypeError("filename must be a str or bytes object, or a file")
85-
return binary_file
87+
return binary_file, closefd
8688

8789

8890
class _ThreadedGzipReader(io.RawIOBase):
8991
def __init__(self, filename, queue_size=2, block_size=1024 * 1024):
90-
self.raw = open_as_binary_stream(filename, "rb")
92+
self.raw, self.closefd = open_as_binary_stream(filename, "rb")
9193
self.fileobj = igzip._IGzipReader(self.raw, buffersize=8 * block_size)
9294
self.pos = 0
9395
self.read_file = False
@@ -155,7 +157,8 @@ def close(self) -> None:
155157
self.running = False
156158
self.worker.join()
157159
self.fileobj.close()
158-
self.raw.close()
160+
if self.closefd:
161+
self.raw.close()
159162
self._closed = True
160163

161164
@property
@@ -246,7 +249,7 @@ def __init__(self,
246249
self._crc = 0
247250
self.running = False
248251
self._size = 0
249-
self.raw = open_as_binary_stream(filename, mode)
252+
self.raw, self.closefd = open_as_binary_stream(filename, mode)
250253
self._closed = False
251254
self._write_gzip_header()
252255
self.start()
@@ -329,7 +332,8 @@ def close(self) -> None:
329332
trailer = struct.pack("<II", self._crc, self._size & 0xFFFFFFFF)
330333
self.raw.write(trailer)
331334
self.raw.flush()
332-
self.raw.close()
335+
if self.closefd:
336+
self.raw.close()
333337
self._closed = True
334338

335339
@property

tests/test_igzip_threaded.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,22 @@ def test_igzip_threaded_open_compresslevel_and_reading(tmp_path):
199199
with igzip_threaded.open(test_file, compresslevel=5) as f:
200200
text = f.read()
201201
assert text == b"thisisatest"
202+
203+
204+
def test_threaded_reader_does_not_close_stream():
205+
test_stream = io.BytesIO()
206+
test_stream.write(gzip.compress(b"thisisatest"))
207+
test_stream.seek(0)
208+
with igzip_threaded.open(test_stream, "rb") as f:
209+
text = f.read()
210+
assert not test_stream.closed
211+
assert text == b"thisisatest"
212+
213+
214+
def test_threaded_writer_does_not_close_stream():
215+
test_stream = io.BytesIO()
216+
with igzip_threaded.open(test_stream, "wb") as f:
217+
f.write(b"thisisatest")
218+
assert not test_stream.closed
219+
test_stream.seek(0)
220+
assert gzip.decompress(test_stream.read()) == b"thisisatest"

0 commit comments

Comments
 (0)