Skip to content

Commit ff653ad

Browse files
committed
chore: port changes to CFFI implementation
1 parent 7f4c31b commit ff653ad

6 files changed

Lines changed: 160 additions & 195 deletions

File tree

src/python/backports/zstd/_cffi/__init__.py

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,12 @@
5353

5454

5555
def get_param_bounds(parameter, is_compress):
56-
"""
57-
Get CompressionParameter/DecompressionParameter bounds.
58-
59-
parameter
60-
The parameter to get bounds.
61-
is_compress
62-
True for CompressionParameter, False for DecompressionParameter.
63-
"""
56+
"""Get CompressionParameter/DecompressionParameter bounds.
57+
58+
parameter
59+
The parameter to get bounds.
60+
is_compress
61+
True for CompressionParameter, False for DecompressionParameter."""
6462
if is_compress:
6563
bound = _lib.ZSTD_cParam_getBounds(parameter)
6664
if _lib.ZSTD_isError(bound.error):
@@ -73,12 +71,10 @@ def get_param_bounds(parameter, is_compress):
7371

7472

7573
def get_frame_info(frame_buffer):
76-
"""
77-
Get Zstandard frame infomation from a frame header.
74+
"""Get Zstandard frame infomation from a frame header.
7875
79-
frame_buffer
80-
A bytes-like object, containing the header of a Zstandard frame.
81-
"""
76+
frame_buffer
77+
A bytes-like object, containing the header of a Zstandard frame."""
8278
decompressed_size = _lib.ZSTD_getFrameContentSize(
8379
_ffi.from_buffer(frame_buffer), len(frame_buffer)
8480
)
@@ -98,13 +94,11 @@ def get_frame_info(frame_buffer):
9894

9995

10096
def get_frame_size(frame_buffer):
101-
"""
102-
Get the size of a Zstandard frame, including the header and optional checksum.
97+
"""Get the size of a Zstandard frame, including the header and optional checksum.
10398
104-
frame_buffer
105-
A bytes-like object, it should start from the beginning of a frame,
106-
and contains at least one complete frame.
107-
"""
99+
frame_buffer
100+
A bytes-like object, it should start from the beginning of a frame,
101+
and contains at least one complete frame."""
108102
frame_size = _lib.ZSTD_findFrameCompressedSize(
109103
_ffi.from_buffer(frame_buffer), len(frame_buffer)
110104
)
@@ -144,16 +138,14 @@ def _clinic_train_dict(*args):
144138

145139

146140
def train_dict(samples_bytes, samples_sizes, dict_size):
147-
"""
148-
Train a Zstandard dictionary on sample data.
149-
150-
samples_bytes
151-
Concatenation of samples.
152-
samples_sizes
153-
Tuple of samples' sizes.
154-
dict_size
155-
The size of the dictionary.
156-
"""
141+
"""Train a Zstandard dictionary on sample data.
142+
143+
samples_bytes
144+
Concatenation of samples.
145+
samples_sizes
146+
Tuple of samples' sizes.
147+
dict_size
148+
The size of the dictionary."""
157149
_clinic_train_dict(samples_bytes, samples_sizes, dict_size)
158150

159151
# Check arguments
@@ -193,20 +185,18 @@ def _clinic_finalize_dict(*args):
193185
def finalize_dict(
194186
custom_dict_bytes, samples_bytes, samples_sizes, dict_size, compression_level
195187
):
196-
"""
197-
Finalize a Zstandard dictionary.
198-
199-
custom_dict_bytes
200-
Custom dictionary content.
201-
samples_bytes
202-
Concatenation of samples.
203-
samples_sizes
204-
Tuple of samples' sizes.
205-
dict_size
206-
The size of the dictionary.
207-
compression_level
208-
Optimize for a specific Zstandard compression level, 0 means default.
209-
"""
188+
"""Finalize a Zstandard dictionary.
189+
190+
custom_dict_bytes
191+
Custom dictionary content.
192+
samples_bytes
193+
Concatenation of samples.
194+
samples_sizes
195+
Tuple of samples' sizes.
196+
dict_size
197+
The size of the dictionary.
198+
compression_level
199+
Optimize for a specific Zstandard compression level, 0 means default."""
210200
_clinic_finalize_dict(
211201
custom_dict_bytes, samples_bytes, samples_sizes, dict_size, compression_level
212202
)

src/python/backports/zstd/_cffi/_common.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,12 @@ def _set_parameter_error(is_compress, key, value):
121121

122122

123123
def set_parameter_types(c_parameter_type, d_parameter_type):
124-
"""
125-
Set CompressionParameter and DecompressionParameter types for validity check.
126-
127-
c_parameter_type
128-
CompressionParameter IntEnum type object
129-
d_parameter_type
130-
DecompressionParameter IntEnum type object
131-
"""
124+
"""Set CompressionParameter and DecompressionParameter types for validity check.
125+
126+
c_parameter_type
127+
CompressionParameter IntEnum type object
128+
d_parameter_type
129+
DecompressionParameter IntEnum type object"""
132130
_PARAMETER_TYPES["compression"] = c_parameter_type
133131
_PARAMETER_TYPES["decompression"] = d_parameter_type
134132

src/python/backports/zstd/_cffi/compressor.py

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,24 @@ def _zstd_contentsize_converter(size):
3131
if size < 0 or size >= _lib.ZSTD_CONTENTSIZE_ERROR:
3232
raise ValueError(
3333
"size argument should be a positive int less "
34-
"than %ull" % _lib.ZSTD_CONTENTSIZE_ERROR
34+
"than %u" % _lib.ZSTD_CONTENTSIZE_ERROR
3535
)
3636

3737
return size
3838

3939

4040
class ZstdCompressor:
41-
"""
42-
Create a compressor object for compressing data incrementally.
41+
"""Create a compressor object for compressing data incrementally.
4342
44-
level
45-
The compression level to use. Defaults to COMPRESSION_LEVEL_DEFAULT.
46-
options
47-
A dict object that contains advanced compression parameters.
48-
zstd_dict
49-
A ZstdDict object, a pre-trained Zstandard dictionary.
43+
level
44+
The compression level to use. Defaults to COMPRESSION_LEVEL_DEFAULT.
45+
options
46+
A dict object that contains advanced compression parameters.
47+
zstd_dict
48+
A ZstdDict object, a pre-trained Zstandard dictionary.
5049
51-
Thread-safe at method level. For one-shot compression, use the compress()
52-
function instead.
53-
"""
50+
Thread-safe at method level. For one-shot compression, use the
51+
compress() function instead."""
5452

5553
CONTINUE = _lib.ZSTD_e_continue
5654
FLUSH_BLOCK = _lib.ZSTD_e_flush
@@ -95,13 +93,11 @@ def __init__(self, level=None, options=None, zstd_dict=None):
9593

9694
@property
9795
def last_mode(self):
98-
"""
99-
The last mode used to this compressor object, its value can be .CONTINUE,
100-
.FLUSH_BLOCK, .FLUSH_FRAME. Initialized to .FLUSH_FRAME.
96+
"""The last mode used to this compressor object, its value can be .CONTINUE,
97+
.FLUSH_BLOCK, .FLUSH_FRAME. Initialized to .FLUSH_FRAME.
10198
102-
It can be used to get the current state of a compressor, such as, data
103-
flushed, or a frame ended.
104-
"""
99+
It can be used to get the current state of a compressor, such as, data
100+
flushed, or a frame ended."""
105101
return self._last_mode
106102

107103
def _zstd_set_c_level(self, level):
@@ -203,17 +199,15 @@ def __del__(self):
203199
self._cctx = _ffi.NULL
204200

205201
def compress(self, data, mode=_lib.ZSTD_e_continue):
206-
"""
207-
Provide data to the compressor object.
202+
"""Provide data to the compressor object.
208203
209-
mode
210-
Can be these 3 values ZstdCompressor.CONTINUE,
211-
ZstdCompressor.FLUSH_BLOCK, ZstdCompressor.FLUSH_FRAME
204+
mode
205+
Can be these 3 values ZstdCompressor.CONTINUE,
206+
ZstdCompressor.FLUSH_BLOCK, ZstdCompressor.FLUSH_FRAME
212207
213-
Return a chunk of compressed data if possible, or b'' otherwise. When you have
214-
finished providing data to the compressor, call the flush() method to finish
215-
the compression process.
216-
"""
208+
Return a chunk of compressed data if possible, or b'' otherwise.
209+
When you have finished providing data to the compressor, call the
210+
flush() method to finish the compression process."""
217211
if (
218212
mode != _lib.ZSTD_e_continue
219213
and mode != _lib.ZSTD_e_flush
@@ -327,17 +321,15 @@ def _compress_mt_continue_lock_held(self, data):
327321
return _OutputBuffer_Finish(buffer, out)
328322

329323
def flush(self, mode=_lib.ZSTD_e_end):
330-
"""
331-
Finish the compression process.
324+
"""Finish the compression process.
332325
333-
mode
334-
Can be these 2 values ZstdCompressor.FLUSH_FRAME,
335-
ZstdCompressor.FLUSH_BLOCK
326+
mode
327+
Can be these 2 values ZstdCompressor.FLUSH_FRAME,
328+
ZstdCompressor.FLUSH_BLOCK
336329
337-
Flush any remaining data left in internal buffers. Since Zstandard data
338-
consists of one or more independent frames, the compressor object can still
339-
be used after this method is called.
340-
"""
330+
Flush any remaining data left in internal buffers. Since Zstandard
331+
data consists of one or more independent frames, the compressor
332+
object can still be used after this method is called."""
341333
# Check mode value
342334
if mode != _lib.ZSTD_e_end and mode != _lib.ZSTD_e_flush:
343335
raise ValueError(
@@ -359,20 +351,19 @@ def flush(self, mode=_lib.ZSTD_e_end):
359351
raise
360352

361353
def set_pledged_input_size(self, size, /):
362-
"""
363-
Set the uncompressed content size to be written into the frame header.
354+
"""Set the uncompressed content size to be written into the frame header.
364355
365-
size
366-
The size of the uncompressed data to be provided to the compressor.
356+
size
357+
The size of the uncompressed data to be provided to the compressor.
367358
368-
This method can be used to ensure the header of the frame about to be written
369-
includes the size of the data, unless the CompressionParameter.content_size_flag
370-
is set to False. If last_mode != FLUSH_FRAME, then a RuntimeError is raised.
359+
This method can be used to ensure the header of the frame about to
360+
be written includes the size of the data, unless the
361+
CompressionParameter.content_size_flag is set to False.
362+
If last_mode != FLUSH_FRAME, then a RuntimeError is raised.
371363
372-
It is important to ensure that the pledged data size matches the actual data
373-
size. If they do not match the compressed output data may be corrupted and the
374-
final chunk written may be lost.
375-
"""
364+
It is important to ensure that the pledged data size matches the
365+
actual data size. If they do not match the compressed output data
366+
may be corrupted and the final chunk written may be lost."""
376367
size = _zstd_contentsize_converter(size)
377368

378369
# Thread-safe code

src/python/backports/zstd/_cffi/decompressor.py

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@
2424

2525

2626
class ZstdDecompressor:
27-
"""
28-
Create a decompressor object for decompressing data incrementally.
27+
"""Create a decompressor object for decompressing data incrementally.
2928
30-
zstd_dict
31-
A ZstdDict object, a pre-trained Zstandard dictionary.
32-
options
33-
A dict object that contains advanced decompression parameters.
29+
zstd_dict
30+
A ZstdDict object, a pre-trained Zstandard dictionary.
31+
options
32+
A dict object that contains advanced decompression parameters.
3433
35-
Thread-safe at method level. For one-shot decompression, use the decompress()
36-
function instead.
37-
"""
34+
Thread-safe at method level. For one-shot decompression, use the
35+
decompress() function instead."""
3836

3937
def __init__(self, zstd_dict=None, options=None):
4038
self._input_buffer = _ffi.NULL
@@ -101,7 +99,7 @@ def _zstd_set_d_parameters(self, options):
10199
# Check key type
102100
if isinstance(key, _PARAMETER_TYPES["compression"]):
103101
raise TypeError(
104-
"compression options dictionary key must not be a "
102+
"decompression options dictionary key must not be a "
105103
"CompressionParameter attribute"
106104
)
107105

@@ -125,12 +123,11 @@ def __del__(self):
125123

126124
@property
127125
def unused_data(self):
128-
"""
129-
A bytes object of un-consumed input data.
126+
"""A bytes object of un-consumed input data.
130127
131-
When ZstdDecompressor object stops after a frame is
132-
decompressed, unused input data after the frame. Otherwise this will be b''.
133-
"""
128+
When ZstdDecompressor object stops after a frame is
129+
decompressed, unused input data after the frame. Otherwise this
130+
will be b''."""
134131
with self._lock:
135132
if not self._eof:
136133
return b""
@@ -145,46 +142,41 @@ def unused_data(self):
145142

146143
@property
147144
def eof(self):
148-
"""
149-
True means the end of the first frame has been reached. If decompress data
150-
after that, an EOFError exception will be raised.
151-
"""
145+
"""True means the end of the first frame has been reached. If decompress data
146+
after that, an EOFError exception will be raised."""
152147
return self._eof
153148

154149
@property
155150
def needs_input(self):
156-
"""
157-
If the max_length output limit in .decompress() method has been reached,
158-
and the decompressor has (or may has) unconsumed input data, it will be set
159-
to False. In this case, passing b'' to the .decompress() method may output
160-
further data.
161-
"""
151+
"""If the max_length output limit in .decompress() method has been reached,
152+
and the decompressor has (or may has) unconsumed input data, it will be set
153+
to False. In this case, passing b'' to the .decompress() method may output
154+
further data."""
162155
return self._needs_input
163156

164157
def decompress(self, data, max_length=-1):
165-
"""
166-
Decompress *data*, returning uncompressed bytes if possible, or b'' otherwise.
167-
168-
data
169-
A bytes-like object, Zstandard data to be decompressed.
170-
max_length
171-
Maximum size of returned data. When it is negative, the size of
172-
output buffer is unlimited. When it is nonnegative, returns at
173-
most max_length bytes of decompressed data.
174-
175-
If *max_length* is nonnegative, returns at most *max_length* bytes of
176-
decompressed data. If this limit is reached and further output can be
177-
produced, *self.needs_input* will be set to ``False``. In this case, the next
178-
call to *decompress()* may provide *data* as b'' to obtain more of the output.
179-
180-
If all of the input data was decompressed and returned (either because this
181-
was less than *max_length* bytes, or because *max_length* was negative),
182-
*self.needs_input* will be set to True.
183-
184-
Attempting to decompress data after the end of a frame is reached raises an
185-
EOFError. Any data found after the end of the frame is ignored and saved in
186-
the self.unused_data attribute.
187-
"""
158+
"""Decompress *data*, returning uncompressed bytes if possible, or b'' otherwise.
159+
160+
data
161+
A bytes-like object, Zstandard data to be decompressed.
162+
max_length
163+
Maximum size of returned data. When it is negative, the size of
164+
output buffer is unlimited. When it is nonnegative, returns at
165+
most max_length bytes of decompressed data.
166+
167+
If *max_length* is nonnegative, returns at most *max_length* bytes
168+
of decompressed data. If this limit is reached and further output
169+
can be produced, *self.needs_input* will be set to ``False``. In
170+
this case, the next call to *decompress()* may provide *data* as b''
171+
to obtain more of the output.
172+
173+
If all of the input data was decompressed and returned (either
174+
because this was less than *max_length* bytes, or because
175+
*max_length* was negative), *self.needs_input* will be set to True.
176+
177+
Attempting to decompress data after the end of a frame is reached
178+
raises an EOFError. Any data found after the end of the frame is
179+
ignored and saved in the self.unused_data attribute."""
188180
with self._lock:
189181
return self._stream_decompress_lock_held(data, max_length)
190182

0 commit comments

Comments
 (0)