Skip to content

Commit d9571c9

Browse files
[3.14] GH-132983: PEP 7 and Argument Clinic changes for zstd (GH-133791) (#133792)
GH-132983: PEP 7 and Argument Clinic changes for zstd (GH-133791) (cherry picked from commit 1978904) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 9023b6f commit d9571c9

File tree

8 files changed

+132
-151
lines changed

8 files changed

+132
-151
lines changed

Lib/compression/zstd/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_frame_info(frame_buffer):
7171
the frame may or may not need a dictionary to be decoded,
7272
and the ID of such a dictionary is not specified.
7373
"""
74-
return FrameInfo(*_zstd._get_frame_info(frame_buffer))
74+
return FrameInfo(*_zstd.get_frame_info(frame_buffer))
7575

7676

7777
def train_dict(samples, dict_size):
@@ -91,7 +91,7 @@ def train_dict(samples, dict_size):
9191
chunk_sizes = tuple(_nbytes(sample) for sample in samples)
9292
if not chunks:
9393
raise ValueError("samples contained no data; can't train dictionary.")
94-
dict_content = _zstd._train_dict(chunks, chunk_sizes, dict_size)
94+
dict_content = _zstd.train_dict(chunks, chunk_sizes, dict_size)
9595
return ZstdDict(dict_content)
9696

9797

@@ -127,7 +127,7 @@ def finalize_dict(zstd_dict, /, samples, dict_size, level):
127127
if not chunks:
128128
raise ValueError("The samples are empty content, can't finalize the"
129129
"dictionary.")
130-
dict_content = _zstd._finalize_dict(zstd_dict.dict_content,
130+
dict_content = _zstd.finalize_dict(zstd_dict.dict_content,
131131
chunks, chunk_sizes,
132132
dict_size, level)
133133
return ZstdDict(dict_content)
@@ -201,7 +201,7 @@ def bounds(self):
201201
202202
Both the lower and upper bounds are inclusive.
203203
"""
204-
return _zstd._get_param_bounds(self.value, is_compress=True)
204+
return _zstd.get_param_bounds(self.value, is_compress=True)
205205

206206

207207
class DecompressionParameter(enum.IntEnum):
@@ -214,7 +214,7 @@ def bounds(self):
214214
215215
Both the lower and upper bounds are inclusive.
216216
"""
217-
return _zstd._get_param_bounds(self.value, is_compress=False)
217+
return _zstd.get_param_bounds(self.value, is_compress=False)
218218

219219

220220
class Strategy(enum.IntEnum):
@@ -237,4 +237,4 @@ class Strategy(enum.IntEnum):
237237

238238

239239
# Check validity of the CompressionParameter & DecompressionParameter types
240-
_zstd._set_parameter_types(CompressionParameter, DecompressionParameter)
240+
_zstd.set_parameter_types(CompressionParameter, DecompressionParameter)

Lib/test/test_zstd.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,43 +1174,43 @@ def test_finalize_dict_arguments(self):
11741174
def test_train_dict_c(self):
11751175
# argument wrong type
11761176
with self.assertRaises(TypeError):
1177-
_zstd._train_dict({}, (), 100)
1177+
_zstd.train_dict({}, (), 100)
11781178
with self.assertRaises(TypeError):
1179-
_zstd._train_dict(b'', 99, 100)
1179+
_zstd.train_dict(b'', 99, 100)
11801180
with self.assertRaises(TypeError):
1181-
_zstd._train_dict(b'', (), 100.1)
1181+
_zstd.train_dict(b'', (), 100.1)
11821182

11831183
# size > size_t
11841184
with self.assertRaises(ValueError):
1185-
_zstd._train_dict(b'', (2**64+1,), 100)
1185+
_zstd.train_dict(b'', (2**64+1,), 100)
11861186

11871187
# dict_size <= 0
11881188
with self.assertRaises(ValueError):
1189-
_zstd._train_dict(b'', (), 0)
1189+
_zstd.train_dict(b'', (), 0)
11901190

11911191
def test_finalize_dict_c(self):
11921192
with self.assertRaises(TypeError):
1193-
_zstd._finalize_dict(1, 2, 3, 4, 5)
1193+
_zstd.finalize_dict(1, 2, 3, 4, 5)
11941194

11951195
# argument wrong type
11961196
with self.assertRaises(TypeError):
1197-
_zstd._finalize_dict({}, b'', (), 100, 5)
1197+
_zstd.finalize_dict({}, b'', (), 100, 5)
11981198
with self.assertRaises(TypeError):
1199-
_zstd._finalize_dict(TRAINED_DICT.dict_content, {}, (), 100, 5)
1199+
_zstd.finalize_dict(TRAINED_DICT.dict_content, {}, (), 100, 5)
12001200
with self.assertRaises(TypeError):
1201-
_zstd._finalize_dict(TRAINED_DICT.dict_content, b'', 99, 100, 5)
1201+
_zstd.finalize_dict(TRAINED_DICT.dict_content, b'', 99, 100, 5)
12021202
with self.assertRaises(TypeError):
1203-
_zstd._finalize_dict(TRAINED_DICT.dict_content, b'', (), 100.1, 5)
1203+
_zstd.finalize_dict(TRAINED_DICT.dict_content, b'', (), 100.1, 5)
12041204
with self.assertRaises(TypeError):
1205-
_zstd._finalize_dict(TRAINED_DICT.dict_content, b'', (), 100, 5.1)
1205+
_zstd.finalize_dict(TRAINED_DICT.dict_content, b'', (), 100, 5.1)
12061206

12071207
# size > size_t
12081208
with self.assertRaises(ValueError):
1209-
_zstd._finalize_dict(TRAINED_DICT.dict_content, b'', (2**64+1,), 100, 5)
1209+
_zstd.finalize_dict(TRAINED_DICT.dict_content, b'', (2**64+1,), 100, 5)
12101210

12111211
# dict_size <= 0
12121212
with self.assertRaises(ValueError):
1213-
_zstd._finalize_dict(TRAINED_DICT.dict_content, b'', (), 0, 5)
1213+
_zstd.finalize_dict(TRAINED_DICT.dict_content, b'', (), 0, 5)
12141214

12151215
def test_train_buffer_protocol_samples(self):
12161216
def _nbytes(dat):
@@ -1232,19 +1232,19 @@ def _nbytes(dat):
12321232
# wrong size list
12331233
with self.assertRaisesRegex(ValueError,
12341234
"The samples size tuple doesn't match the concatenation's size"):
1235-
_zstd._train_dict(concatenation, tuple(wrong_size_lst), 100*_1K)
1235+
_zstd.train_dict(concatenation, tuple(wrong_size_lst), 100*_1K)
12361236

12371237
# correct size list
1238-
_zstd._train_dict(concatenation, tuple(correct_size_lst), 3*_1K)
1238+
_zstd.train_dict(concatenation, tuple(correct_size_lst), 3*_1K)
12391239

12401240
# wrong size list
12411241
with self.assertRaisesRegex(ValueError,
12421242
"The samples size tuple doesn't match the concatenation's size"):
1243-
_zstd._finalize_dict(TRAINED_DICT.dict_content,
1243+
_zstd.finalize_dict(TRAINED_DICT.dict_content,
12441244
concatenation, tuple(wrong_size_lst), 300*_1K, 5)
12451245

12461246
# correct size list
1247-
_zstd._finalize_dict(TRAINED_DICT.dict_content,
1247+
_zstd.finalize_dict(TRAINED_DICT.dict_content,
12481248
concatenation, tuple(correct_size_lst), 300*_1K, 5)
12491249

12501250
def test_as_prefix(self):

Modules/_zstd/_zstdmodule.c

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ typedef struct {
6969
char parameter_name[32];
7070
} ParameterInfo;
7171

72-
static const ParameterInfo cp_list[] =
73-
{
72+
static const ParameterInfo cp_list[] = {
7473
{ZSTD_c_compressionLevel, "compression_level"},
7574
{ZSTD_c_windowLog, "window_log"},
7675
{ZSTD_c_hashLog, "hash_log"},
@@ -95,8 +94,7 @@ static const ParameterInfo cp_list[] =
9594
{ZSTD_c_overlapLog, "overlap_log"}
9695
};
9796

98-
static const ParameterInfo dp_list[] =
99-
{
97+
static const ParameterInfo dp_list[] = {
10098
{ZSTD_d_windowLogMax, "window_log_max"}
10199
};
102100

@@ -173,7 +171,7 @@ get_zstd_state(PyObject *module)
173171

174172

175173
/*[clinic input]
176-
_zstd._train_dict
174+
_zstd.train_dict
177175
178176
samples_bytes: PyBytesObject
179177
Concatenation of samples.
@@ -187,9 +185,9 @@ Internal function, train a zstd dictionary on sample data.
187185
[clinic start generated code]*/
188186

189187
static PyObject *
190-
_zstd__train_dict_impl(PyObject *module, PyBytesObject *samples_bytes,
191-
PyObject *samples_sizes, Py_ssize_t dict_size)
192-
/*[clinic end generated code: output=b5b4f36347c0addd input=2dce5b57d63923e2]*/
188+
_zstd_train_dict_impl(PyObject *module, PyBytesObject *samples_bytes,
189+
PyObject *samples_sizes, Py_ssize_t dict_size)
190+
/*[clinic end generated code: output=8e87fe43935e8f77 input=70fcd8937f2528b6]*/
193191
{
194192
// TODO(emmatyping): The preamble and suffix to this function and _finalize_dict
195193
// are pretty similar. We should see if we can refactor them to share that code.
@@ -277,7 +275,7 @@ _zstd__train_dict_impl(PyObject *module, PyBytesObject *samples_bytes,
277275
}
278276

279277
/*[clinic input]
280-
_zstd._finalize_dict
278+
_zstd.finalize_dict
281279
282280
custom_dict_bytes: PyBytesObject
283281
Custom dictionary content.
@@ -295,11 +293,11 @@ Internal function, finalize a zstd dictionary.
295293
[clinic start generated code]*/
296294

297295
static PyObject *
298-
_zstd__finalize_dict_impl(PyObject *module, PyBytesObject *custom_dict_bytes,
299-
PyBytesObject *samples_bytes,
300-
PyObject *samples_sizes, Py_ssize_t dict_size,
301-
int compression_level)
302-
/*[clinic end generated code: output=5dc5b520fddba37f input=8afd42a249078460]*/
296+
_zstd_finalize_dict_impl(PyObject *module, PyBytesObject *custom_dict_bytes,
297+
PyBytesObject *samples_bytes,
298+
PyObject *samples_sizes, Py_ssize_t dict_size,
299+
int compression_level)
300+
/*[clinic end generated code: output=f91821ba5ae85bda input=130d1508adb55ba1]*/
303301
{
304302
Py_ssize_t chunks_number;
305303
size_t *chunk_sizes = NULL;
@@ -396,7 +394,7 @@ _zstd__finalize_dict_impl(PyObject *module, PyBytesObject *custom_dict_bytes,
396394

397395

398396
/*[clinic input]
399-
_zstd._get_param_bounds
397+
_zstd.get_param_bounds
400398
401399
parameter: int
402400
The parameter to get bounds.
@@ -407,9 +405,8 @@ Internal function, get CompressionParameter/DecompressionParameter bounds.
407405
[clinic start generated code]*/
408406

409407
static PyObject *
410-
_zstd__get_param_bounds_impl(PyObject *module, int parameter,
411-
int is_compress)
412-
/*[clinic end generated code: output=9892cd822f937e79 input=884cd1a01125267d]*/
408+
_zstd_get_param_bounds_impl(PyObject *module, int parameter, int is_compress)
409+
/*[clinic end generated code: output=4acf5a876f0620ca input=84e669591e487008]*/
413410
{
414411
ZSTD_bounds bound;
415412
if (is_compress) {
@@ -466,7 +463,7 @@ _zstd_get_frame_size_impl(PyObject *module, Py_buffer *frame_buffer)
466463
}
467464

468465
/*[clinic input]
469-
_zstd._get_frame_info
466+
_zstd.get_frame_info
470467
471468
frame_buffer: Py_buffer
472469
A bytes-like object, containing the header of a zstd frame.
@@ -475,8 +472,8 @@ Internal function, get zstd frame infomation from a frame header.
475472
[clinic start generated code]*/
476473

477474
static PyObject *
478-
_zstd__get_frame_info_impl(PyObject *module, Py_buffer *frame_buffer)
479-
/*[clinic end generated code: output=5462855464ecdf81 input=67f1f8e4b7b89c4d]*/
475+
_zstd_get_frame_info_impl(PyObject *module, Py_buffer *frame_buffer)
476+
/*[clinic end generated code: output=56e033cf48001929 input=1816f14656b6aa22]*/
480477
{
481478
uint64_t decompressed_size;
482479
uint32_t dict_id;
@@ -508,7 +505,7 @@ _zstd__get_frame_info_impl(PyObject *module, Py_buffer *frame_buffer)
508505
}
509506

510507
/*[clinic input]
511-
_zstd._set_parameter_types
508+
_zstd.set_parameter_types
512509
513510
c_parameter_type: object(subclass_of='&PyType_Type')
514511
CompressionParameter IntEnum type object
@@ -519,9 +516,9 @@ Internal function, set CompressionParameter/DecompressionParameter types for val
519516
[clinic start generated code]*/
520517

521518
static PyObject *
522-
_zstd__set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type,
523-
PyObject *d_parameter_type)
524-
/*[clinic end generated code: output=a13d4890ccbd2873 input=4535545d903853d3]*/
519+
_zstd_set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type,
520+
PyObject *d_parameter_type)
521+
/*[clinic end generated code: output=f3313b1294f19502 input=30402523871b8280]*/
525522
{
526523
_zstd_state* const mod_state = get_zstd_state(module);
527524

@@ -544,14 +541,13 @@ _zstd__set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type,
544541
}
545542

546543
static PyMethodDef _zstd_methods[] = {
547-
_ZSTD__TRAIN_DICT_METHODDEF
548-
_ZSTD__FINALIZE_DICT_METHODDEF
549-
_ZSTD__GET_PARAM_BOUNDS_METHODDEF
544+
_ZSTD_TRAIN_DICT_METHODDEF
545+
_ZSTD_FINALIZE_DICT_METHODDEF
546+
_ZSTD_GET_PARAM_BOUNDS_METHODDEF
550547
_ZSTD_GET_FRAME_SIZE_METHODDEF
551-
_ZSTD__GET_FRAME_INFO_METHODDEF
552-
_ZSTD__SET_PARAMETER_TYPES_METHODDEF
553-
554-
{0}
548+
_ZSTD_GET_FRAME_INFO_METHODDEF
549+
_ZSTD_SET_PARAMETER_TYPES_METHODDEF
550+
{NULL, NULL}
555551
};
556552

557553
static int
@@ -595,7 +591,7 @@ do { \
595591
ADD_TYPE(mod_state->ZstdCompressor_type, zstd_compressor_type_spec);
596592
ADD_TYPE(mod_state->ZstdDecompressor_type, zstd_decompressor_type_spec);
597593
mod_state->ZstdError = PyErr_NewExceptionWithDoc(
598-
"_zstd.ZstdError",
594+
"compression.zstd.ZstdError",
599595
"An error occurred in the zstd library.",
600596
NULL, NULL);
601597
if (mod_state->ZstdError == NULL) {
@@ -732,14 +728,15 @@ static struct PyModuleDef_Slot _zstd_slots[] = {
732728
};
733729

734730
struct PyModuleDef _zstdmodule = {
735-
PyModuleDef_HEAD_INIT,
731+
.m_base = PyModuleDef_HEAD_INIT,
736732
.m_name = "_zstd",
733+
.m_doc = "Implementation module for Zstandard compression.",
737734
.m_size = sizeof(_zstd_state),
738735
.m_slots = _zstd_slots,
739736
.m_methods = _zstd_methods,
740737
.m_traverse = _zstd_traverse,
741738
.m_clear = _zstd_clear,
742-
.m_free = _zstd_free
739+
.m_free = _zstd_free,
743740
};
744741

745742
PyMODINIT_FUNC

Modules/_zstd/_zstdmodule.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ extern PyModuleDef _zstdmodule;
2020

2121
/* For clinic type calculations */
2222
static inline _zstd_state *
23-
get_zstd_state_from_type(PyTypeObject *type) {
23+
get_zstd_state_from_type(PyTypeObject *type)
24+
{
2425
PyObject *module = PyType_GetModuleByDef(type, &_zstdmodule);
2526
if (module == NULL) {
2627
return NULL;
@@ -149,7 +150,8 @@ typedef enum {
149150
} dictionary_type;
150151

151152
static inline int
152-
mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out) {
153+
mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out)
154+
{
153155
return in->size == in->pos && out->size != out->pos;
154156
}
155157

@@ -163,13 +165,3 @@ set_parameter_error(const _zstd_state* const state, int is_compress,
163165
int key_v, int value_v);
164166

165167
static const char init_twice_msg[] = "__init__ method is called twice.";
166-
167-
extern PyObject *
168-
decompress_impl(ZstdDecompressor *self, ZSTD_inBuffer *in,
169-
Py_ssize_t max_length,
170-
Py_ssize_t initial_size,
171-
decompress_type type);
172-
173-
extern PyObject *
174-
compress_impl(ZstdCompressor *self, Py_buffer *data,
175-
ZSTD_EndDirective end_directive);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy