From e55dddd92fa9c2dcd7b021181fb7a73552b88f79 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Fri, 24 Nov 2023 09:16:12 +0100 Subject: [PATCH] Fix append mode bug --- CHANGELOG.rst | 5 +++++ src/isal/igzip_threaded.py | 8 +++++++- tests/test_igzip_threaded.py | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3e936f62..de602380 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,11 @@ 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.5.3 +----------------- ++ Fix a bug where append mode would not work when using + ``igzip_threaded.open``. + version 1.5.2 ----------------- + Fix a bug where a filehandle remained opened when ``igzip_threaded.open`` diff --git a/src/isal/igzip_threaded.py b/src/isal/igzip_threaded.py index dd34e704..99eafc17 100644 --- a/src/isal/igzip_threaded.py +++ b/src/isal/igzip_threaded.py @@ -63,6 +63,7 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF, gzip_file = io.BufferedWriter( _ThreadedGzipWriter( filename, + mode.replace("t", "b"), block_size=block_size, level=compresslevel, threads=threads @@ -197,11 +198,16 @@ class _ThreadedGzipWriter(io.RawIOBase): """ def __init__(self, filename, + mode: str = "wb", level: int = isal_zlib.ISAL_DEFAULT_COMPRESSION, threads: int = 1, queue_size: int = 1, block_size: int = 1024 * 1024, ): + if "t" in mode or "r" in mode: + raise ValueError("Only binary writing is supported") + if "b" not in mode: + mode += "b" self.lock = threading.Lock() self.exception: Optional[Exception] = None self.level = level @@ -238,7 +244,7 @@ def __init__(self, self.running = False self._size = 0 self._closed = False - self.raw = open_as_binary_stream(filename, "wb") + self.raw = open_as_binary_stream(filename, mode) self._write_gzip_header() self.start() diff --git a/tests/test_igzip_threaded.py b/tests/test_igzip_threaded.py index be6c3ca4..dbee85f8 100644 --- a/tests/test_igzip_threaded.py +++ b/tests/test_igzip_threaded.py @@ -169,3 +169,25 @@ def test_writer_write_after_close(threads): with pytest.raises(ValueError) as error: f.write(b"abc") error.match("closed") + + +def test_igzip_threaded_append(tmp_path): + test_file = tmp_path / "test.txt.gz" + with igzip_threaded.open(test_file, "wb") as f: + f.write(b"AB") + with igzip_threaded.open(test_file, mode="ab") as f: + f.write(b"CD") + with gzip.open(test_file, "rb") as f: + contents = f.read() + assert contents == b"ABCD" + + +def test_igzip_threaded_append_text_mode(tmp_path): + test_file = tmp_path / "test.txt.gz" + with igzip_threaded.open(test_file, "wt") as f: + f.write("AB") + with igzip_threaded.open(test_file, mode="at") as f: + f.write("CD") + with gzip.open(test_file, "rt") as f: + contents = f.read() + assert contents == "ABCD"