diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 93fbe10..50b201a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/src/zlib_ng/gzip_ng_threaded.py b/src/zlib_ng/gzip_ng_threaded.py index f8c0820..5b8a9ff 100644 --- a/src/zlib_ng/gzip_ng_threaded.py +++ b/src/zlib_ng/gzip_ng_threaded.py @@ -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 @@ -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 @@ -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() @@ -334,7 +337,8 @@ def close(self) -> None: trailer = struct.pack("