Skip to content

Commit

Permalink
Merge pull request #178 from pycompression/appendbug
Browse files Browse the repository at this point in the history
Fix append mode bug
  • Loading branch information
rhpvorderman authored Nov 24, 2023
2 parents 60470b9 + e55dddd commit bcd11f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
8 changes: 7 additions & 1 deletion src/isal/igzip_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
22 changes: 22 additions & 0 deletions tests/test_igzip_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit bcd11f0

Please sign in to comment.