Skip to content

Commit 9f42c41

Browse files
authored
Add Decompressor.is_finished()
1 parent 4877f5b commit 9f42c41

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/brotlicffi/_api.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
class Error(Exception):
99
"""
1010
Raised whenever an error is encountered with compressing or decompressing
11-
data using brotlipy.
11+
data using brotlicffi.
1212
1313
.. versionadded:: 0.5.1
1414
"""
1515
pass
1616

1717

18-
#: An alias of :class:`Error <brotli.Error>` that exists for compatibility with
19-
#: the original C brotli module.
18+
#: An alias of :class:`Error <brotlicffi.Error>` that
19+
#: exists for compatibility with the original C brotli module.
2020
#:
2121
#: .. versionadded: 0.5.1
2222
error = Error
@@ -51,7 +51,7 @@ class BrotliEncoderMode(enum.IntEnum):
5151
#: .. note:: This name is defined for compatibility with the Brotli C
5252
#: extension. If you're not interested in that compatibility, it is
5353
#: recommended that you use :class:`BrotliEncoderMode
54-
#: <brotli.BrotliEncoderMode>` instead.
54+
#: <brotlicffi.BrotliEncoderMode>` instead.
5555
#:
5656
#: .. versionadded:: 0.5.0
5757
MODE_GENERIC = BrotliEncoderMode.GENERIC
@@ -62,7 +62,7 @@ class BrotliEncoderMode(enum.IntEnum):
6262
#: .. note:: This name is defined for compatibility with the Brotli C
6363
#: extension. If you're not interested in that compatibility, it is
6464
#: recommended that you use :class:`BrotliEncoderMode
65-
#: <brotli.BrotliEncoderMode>` instead.
65+
#: <brotlicffi.BrotliEncoderMode>` instead.
6666
#:
6767
#: .. versionadded:: 0.5.0
6868
MODE_TEXT = BrotliEncoderMode.TEXT
@@ -73,7 +73,7 @@ class BrotliEncoderMode(enum.IntEnum):
7373
#: .. note:: This name is defined for compatibility with the Brotli C
7474
#: extension. If you're not interested in that compatibility, it is
7575
#: recommended that you use :class:`BrotliEncoderMode
76-
#: <brotli.BrotliEncoderMode>` instead.
76+
#: <brotlicffi.BrotliEncoderMode>` instead.
7777
#:
7878
#: .. versionadded:: 0.5.0
7979
MODE_FONT = BrotliEncoderMode.FONT
@@ -194,7 +194,7 @@ def _validate_lgblock(val):
194194
def _set_parameter(encoder, parameter, parameter_name, val):
195195
"""
196196
This helper function sets a specific Brotli encoder parameter, checking
197-
the return code and raising :class:`Error <brotli.Error>` if it is
197+
the return code and raising :class:`Error <brotlicffi.Error>` if it is
198198
invalid.
199199
"""
200200
rc = lib.BrotliEncoderSetParameter(encoder, parameter, val)
@@ -333,8 +333,7 @@ def flush(self):
333333
will not destroy the compressor. It can be used, for example, to ensure
334334
that given chunks of content will decompress immediately.
335335
"""
336-
chunks = []
337-
chunks.append(self._compress(b'', lib.BROTLI_OPERATION_FLUSH))
336+
chunks = [self._compress(b'', lib.BROTLI_OPERATION_FLUSH)]
338337

339338
while lib.BrotliEncoderHasMoreOutput(self._encoder) == lib.BROTLI_TRUE:
340339
chunks.append(self._compress(b'', lib.BROTLI_OPERATION_FLUSH))
@@ -460,7 +459,14 @@ def finish(self):
460459
assert (
461460
lib.BrotliDecoderHasMoreOutput(self._decoder) == lib.BROTLI_FALSE
462461
)
463-
if lib.BrotliDecoderIsFinished(self._decoder) == lib.BROTLI_FALSE:
462+
if not self.is_finished():
464463
raise Error("Decompression error: incomplete compressed stream.")
465464

466465
return b''
466+
467+
def is_finished(self):
468+
"""
469+
Returns ``True`` if the decompression stream
470+
is complete, ``False`` otherwise
471+
"""
472+
return lib.BrotliDecoderIsFinished(self._decoder) == lib.BROTLI_TRUE

test/test_simple_decompression.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ def test_drip_feed(simple_compressed_file):
5151
outdata = []
5252
o = brotlicffi.Decompressor()
5353
for i in range(0, len(compressed_data)):
54+
assert not o.is_finished()
5455
outdata.append(o.decompress(compressed_data[i:i+1]))
5556

57+
assert o.is_finished()
5658
outdata.append(o.flush())
59+
assert o.is_finished()
5760
outdata.append(o.finish())
61+
assert o.is_finished()
5862

5963
assert b''.join(outdata) == uncompressed_data
6064

@@ -67,6 +71,7 @@ def test_streaming_decompression_fails_properly_on_garbage(exception_cls):
6771
o = brotlicffi.Decompressor()
6872
with pytest.raises(exception_cls):
6973
o.decompress(b'some random garbage')
74+
assert not o.is_finished()
7075

7176

7277
@pytest.mark.parametrize('exception_cls', [brotlicffi.Error, brotlicffi.error])

0 commit comments

Comments
 (0)