Skip to content

gzip_ng_threaded.open no longer closes streams passed to it #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Changelog

version 0.5.0-dev
-----------------
+ Fix a bug where streams that were passed to gzip_ng_threaded.open where
closed.
+ Fix compatibility with Python 3.13

version 0.4.0
Expand Down
14 changes: 9 additions & 5 deletions src/zlib_ng/gzip_ng_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,18 @@ def open(filename, mode="rb", compresslevel=gzip_ng._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 = zlib_ng._GzipReader(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 @@ -334,7 +337,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_gzip_ng_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,22 @@ def test_gzip_ng_threaded_append_text_mode(tmp_path):
with gzip.open(test_file, "rt") as f:
contents = f.read()
assert contents == "ABCD"


def test_threaded_reader_does_not_close_stream():
test_stream = io.BytesIO()
test_stream.write(gzip.compress(b"thisisatest"))
test_stream.seek(0)
with gzip_ng_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 gzip_ng_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"