Skip to content

Commit e55dddd

Browse files
committed
Fix append mode bug
1 parent 60470b9 commit e55dddd

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ 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.5.3
11+
-----------------
12+
+ Fix a bug where append mode would not work when using
13+
``igzip_threaded.open``.
14+
1015
version 1.5.2
1116
-----------------
1217
+ Fix a bug where a filehandle remained opened when ``igzip_threaded.open``

src/isal/igzip_threaded.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
6363
gzip_file = io.BufferedWriter(
6464
_ThreadedGzipWriter(
6565
filename,
66+
mode.replace("t", "b"),
6667
block_size=block_size,
6768
level=compresslevel,
6869
threads=threads
@@ -197,11 +198,16 @@ class _ThreadedGzipWriter(io.RawIOBase):
197198
"""
198199
def __init__(self,
199200
filename,
201+
mode: str = "wb",
200202
level: int = isal_zlib.ISAL_DEFAULT_COMPRESSION,
201203
threads: int = 1,
202204
queue_size: int = 1,
203205
block_size: int = 1024 * 1024,
204206
):
207+
if "t" in mode or "r" in mode:
208+
raise ValueError("Only binary writing is supported")
209+
if "b" not in mode:
210+
mode += "b"
205211
self.lock = threading.Lock()
206212
self.exception: Optional[Exception] = None
207213
self.level = level
@@ -238,7 +244,7 @@ def __init__(self,
238244
self.running = False
239245
self._size = 0
240246
self._closed = False
241-
self.raw = open_as_binary_stream(filename, "wb")
247+
self.raw = open_as_binary_stream(filename, mode)
242248
self._write_gzip_header()
243249
self.start()
244250

tests/test_igzip_threaded.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,25 @@ def test_writer_write_after_close(threads):
169169
with pytest.raises(ValueError) as error:
170170
f.write(b"abc")
171171
error.match("closed")
172+
173+
174+
def test_igzip_threaded_append(tmp_path):
175+
test_file = tmp_path / "test.txt.gz"
176+
with igzip_threaded.open(test_file, "wb") as f:
177+
f.write(b"AB")
178+
with igzip_threaded.open(test_file, mode="ab") as f:
179+
f.write(b"CD")
180+
with gzip.open(test_file, "rb") as f:
181+
contents = f.read()
182+
assert contents == b"ABCD"
183+
184+
185+
def test_igzip_threaded_append_text_mode(tmp_path):
186+
test_file = tmp_path / "test.txt.gz"
187+
with igzip_threaded.open(test_file, "wt") as f:
188+
f.write("AB")
189+
with igzip_threaded.open(test_file, mode="at") as f:
190+
f.write("CD")
191+
with gzip.open(test_file, "rt") as f:
192+
contents = f.read()
193+
assert contents == "ABCD"

0 commit comments

Comments
 (0)