Skip to content

Commit 31f57cf

Browse files
committed
Add [Compressor|Decompressor].process for API compatibility
1 parent 9f42c41 commit 31f57cf

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ dist/
77
.eggs/
88
.tox/
99
.coverage
10-
.hypothesis/
10+
.hypothesis/
11+
*.so

src/brotlicffi/_api.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ._brotlicffi import ffi, lib
66

77

8-
class Error(Exception):
8+
class error(Exception):
99
"""
1010
Raised whenever an error is encountered with compressing or decompressing
1111
data using brotlicffi.
@@ -15,11 +15,11 @@ class Error(Exception):
1515
pass
1616

1717

18-
#: An alias of :class:`Error <brotlicffi.Error>` that
19-
#: exists for compatibility with the original C brotli module.
18+
#: An alias of :class:`error <brotli.error>` that
19+
#: exists for compatibility with the original CFFI brotli module.
2020
#:
21-
#: .. versionadded: 0.5.1
22-
error = Error
21+
#: .. versionadded: 0.8.0
22+
Error = error
2323

2424

2525
class BrotliEncoderMode(enum.IntEnum):
@@ -159,15 +159,15 @@ def _validate_mode(val):
159159
try:
160160
val = BrotliEncoderMode(val)
161161
except ValueError:
162-
raise Error("%s is not a valid encoder mode" % val)
162+
raise error("%s is not a valid encoder mode" % val)
163163

164164

165165
def _validate_quality(val):
166166
"""
167167
Validate that the quality setting is valid.
168168
"""
169169
if not (0 <= val <= 11):
170-
raise Error(
170+
raise error(
171171
"%d is not a valid quality, must be between 0 and 11" % val
172172
)
173173

@@ -177,15 +177,15 @@ def _validate_lgwin(val):
177177
Validate that the lgwin setting is valid.
178178
"""
179179
if not (10 <= val <= 24):
180-
raise Error("%d is not a valid lgwin, must be between 10 and 24" % val)
180+
raise error("%d is not a valid lgwin, must be between 10 and 24" % val)
181181

182182

183183
def _validate_lgblock(val):
184184
"""
185185
Validate that the lgblock setting is valid.
186186
"""
187187
if (val != 0) and not (16 <= val <= 24):
188-
raise Error(
188+
raise error(
189189
"%d is not a valid lgblock, must be either 0 or between 16 and 24"
190190
% val
191191
)
@@ -214,7 +214,7 @@ def _set_parameter(encoder, parameter, parameter_name, val):
214214
# function returns a value we can live in hope that the brotli folks will
215215
# enforce their own constraints.
216216
if rc != lib.BROTLI_TRUE: # pragma: no cover
217-
raise Error(
217+
raise error(
218218
"Error setting parameter %s: %d" % (parameter_name, val)
219219
)
220220

@@ -309,7 +309,7 @@ def _compress(self, data, operation):
309309
ffi.NULL
310310
)
311311
if rc != lib.BROTLI_TRUE: # pragma: no cover
312-
raise Error("Error encountered compressing data.")
312+
raise error("Error encountered compressing data.")
313313

314314
assert not input_size[0]
315315

@@ -327,6 +327,8 @@ def compress(self, data):
327327
"""
328328
return self._compress(data, lib.BROTLI_OPERATION_PROCESS)
329329

330+
process = compress
331+
330332
def flush(self):
331333
"""
332334
Flush the compressor. This will emit the remaining output data, but
@@ -414,7 +416,7 @@ def decompress(self, data):
414416
if rc == lib.BROTLI_DECODER_RESULT_ERROR:
415417
error_code = lib.BrotliDecoderGetErrorCode(self._decoder)
416418
error_message = lib.BrotliDecoderErrorString(error_code)
417-
raise Error(
419+
raise error(
418420
"Decompression error: %s" % ffi.string(error_message)
419421
)
420422

@@ -433,6 +435,8 @@ def decompress(self, data):
433435

434436
return b''.join(chunks)
435437

438+
process = decompress
439+
436440
def flush(self):
437441
"""
438442
Complete the decompression, return whatever data is remaining to be
@@ -460,7 +464,7 @@ def finish(self):
460464
lib.BrotliDecoderHasMoreOutput(self._decoder) == lib.BROTLI_FALSE
461465
)
462466
if not self.is_finished():
463-
raise Error("Decompression error: incomplete compressed stream.")
467+
raise error("Decompression error: incomplete compressed stream.")
464468

465469
return b''
466470

test/test_simple_compression.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ def test_compressed_data_with_dictionaries(s, dictionary):
118118
assert uncompressed == s
119119

120120

121+
@given(binary())
122+
def test_process_alias(s):
123+
c1 = brotlicffi.Compressor()
124+
c2 = brotlicffi.Compressor()
125+
d1 = brotlicffi.Decompressor()
126+
d2 = brotlicffi.Decompressor()
127+
s1 = c1.compress(s) + c1.finish()
128+
s2 = c2.process(s) + c2.finish()
129+
assert (
130+
(d1.decompress(s1) + d1.finish())
131+
== (d2.process(s2) + d2.finish())
132+
== s
133+
)
134+
135+
121136
@pytest.mark.parametrize(
122137
"params",
123138
[

0 commit comments

Comments
 (0)