Skip to content

Commit 7cab211

Browse files
committed
Work on compress flush
1 parent 2697cea commit 7cab211

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/isal/isal_zlib.pyx

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ ISAL_NO_FLUSH=NO_FLUSH
6969
ISAL_SYNC_FLUSH=SYNC_FLUSH
7070
ISAL_FULL_FLUSH=FULL_FLUSH
7171

72-
Z_NO_FLUSH=ISAL_NO_FLUSH
73-
Z_SYNC_FLUSH=ISAL_SYNC_FLUSH
74-
Z_FINISH=ISAL_FULL_FLUSH
72+
Z_NO_FLUSH=zlib.Z_NO_FLUSH
73+
Z_SYNC_FLUSH=zlib.Z_SYNC_FLUSH
74+
Z_FULL_FLUSH=zlib.Z_FULL_FLUSH
75+
Z_FINISH=zlib.Z_FINISH
7576

7677
class IsalError(OSError):
7778
"""Exception raised on compression and decompression errors."""
@@ -452,27 +453,34 @@ cdef class Compress:
452453
finally:
453454
PyBuffer_Release(buffer)
454455

455-
def flush(self, int mode=FULL_FLUSH):
456+
def flush(self, mode=zlib.Z_FINISH):
456457
"""
457458
All pending input is processed, and a bytes object containing the
458459
remaining compressed output is returned.
459460
460-
:param mode: Defaults to ISAL_FULL_FLUSH (Z_FINISH equivalent) which
461+
:param mode: Defaults to Z_FINISH which
461462
finishes the compressed stream and prevents compressing
462-
any more data. The only other supported method is
463-
ISAL_SYNC_FLUSH (Z_SYNC_FLUSH) equivalent.
463+
any more data. The only other supported methods are
464+
Z_SYNC_FLUSH and Z_FULL_FLUSH.
464465
"""
465-
if mode == NO_FLUSH:
466+
467+
if mode == zlib.Z_NO_FLUSH:
466468
# Flushing with no_flush does nothing.
467469
return b""
468-
469-
self.stream.end_of_stream = 1
470-
self.stream.flush = mode
470+
elif mode == zlib.Z_FINISH:
471+
self.stream.flush = FULL_FLUSH
472+
self.stream.end_of_stream = 1
473+
elif mode == zlib.Z_FULL_FLUSH:
474+
self.stream.flush = FULL_FLUSH
475+
elif mode == zlib.Z_SYNC_FLUSH:
476+
self.stream.flush=SYNC_FLUSH
477+
else:
478+
raise IsalError("Unsupported flush mode")
471479

472480
# Initialise output buffer
473481
out = []
474482

475-
while self.stream.internal_state.state != ZSTATE_END:
483+
while True:
476484
self.stream.next_out = self.obuf # Reset output buffer.
477485
self.stream.avail_out = self.obuflen
478486
err = isal_deflate(&self.stream)
@@ -485,6 +493,10 @@ cdef class Compress:
485493
# the data is appended to a list.
486494
# TODO: Improve this with the buffer protocol.
487495
out.append(self.obuf[:self.obuflen - self.stream.avail_out])
496+
if self.stream.internal_state.state != ZSTATE_END and mode == zlib.Z_FINISH:
497+
continue
498+
elif self.stream.avail_in == 0:
499+
break
488500
return b"".join(out)
489501

490502
cdef class Decompress:

0 commit comments

Comments
 (0)