8000 [3.14] gh-132983: Make _zstd C code PEP 7 compliant (GH-134605) (#134… · python/cpython@38a3824 · GitHub
[go: up one dir, main page]

Skip to content

Commit 38a3824

Browse files
[3.14] gh-132983: Make _zstd C code PEP 7 compliant (GH-134605) (#134609)
gh-132983: Make _zstd C code PEP 7 compliant (GH-134605) Make _zstd C code PEP 7 compliant (cherry picked from commit 973b8f6) Co-authored-by: Emma Smith <emma@emmatyping.dev>
1 parent f301af6 commit 38a3824

File tree

6 files changed

+137
-109
lines changed

6 files changed

+137
-109
lines changed

Modules/_zstd/_zstdmodule.c

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,42 @@ set_zstd_error(const _zstd_state* const state,
2828
char *msg;
2929
assert(ZSTD_isError(zstd_ret));
3030

31-
switch (type)
32-
{
33-
case ERR_DECOMPRESS:
34-
msg = "Unable to decompress Zstandard data: %s";
35-
break;
36-
case ERR_COMPRESS:
37-
msg = "Unable to compress Zstandard data: %s";
38-
break;
39-
40-
case ERR_LOAD_D_DICT:
41-
msg = "Unable to load Zstandard dictionary or prefix for decompression: %s";
42-
break;
43-
case ERR_LOAD_C_DICT:
44-
msg = "Unable to load Zstandard dictionary or prefix for compression: %s";
45-
break;
46-
47-
case ERR_GET_C_BOUNDS:
48-
msg = "Unable to get zstd compression parameter bounds: %s";
49-
break;
50-
case ERR_GET_D_BOUNDS:
51-
msg = "Unable to get zstd decompression parameter bounds: %s";
52-
break;
53-
case ERR_SET_C_LEVEL:
54-
msg = "Unable to set zstd compression level: %s";
55-
break;
56-
57-
case ERR_TRAIN_DICT:
58-
msg = "Unable to train the Zstandard dictionary: %s";
59-
break;
60-
case ERR_FINALIZE_DICT:
61-
msg = "Unable to finalize the Zstandard dictionary: %s";
62-
break;
63-
64-
default:
65-
Py_UNREACHABLE();
31+
switch (type) {
32+
case ERR_DECOMPRESS:
33+
msg = "Unable to decompress Zstandard data: %s";
34+
break;
35+
case ERR_COMPRESS:
36+
msg = "Unable to compress Zstandard data: %s";
37+
break;
38+
39+
case ERR_LOAD_D_DICT:
40+
msg = "Unable to load Zstandard dictionary or prefix for "
41+
"decompression: %s";
42+
break;
43+
case ERR_LOAD_C_DICT:
44+
msg = "Unable to load Zstandard dictionary or prefix for "
45+
"compression: %s";
46+
break;
47+
48+
case ERR_GET_C_BOUNDS:
49+
msg = "Unable to get zstd compression parameter bounds: %s";
50+
break;
51+
case ERR_GET_D_BOUNDS:
52+
msg = "Unable to get zstd decompression parameter bounds: %s";
53+
break;
54+
case ERR_SET_C_LEVEL:
55+
msg = "Unable to set zstd compression level: %s";
56+
break;
57+
58+
case ERR_TRAIN_DICT:
59+
msg = "Unable to train the Zstandard dictionary: %s";
60+
break;
61+
case ERR_FINALIZE_DICT:
62+
msg = "Unable to finalize the Zstandard dictionary: %s";
63+
break;
64+
65+
default:
66+
Py_UNREACHABLE();
6667
}
6768
PyErr_Format(state->ZstdError, msg, ZSTD_getErrorName(zstd_ret));
6869
}
@@ -183,7 +184,7 @@ calculate_samples_stats(PyBytesObject *samples_bytes, PyObject *samples_sizes,
183184
chunks_number = Py_SIZE(samples_sizes);
184185
if ((size_t) chunks_number > UINT32_MAX) {
185186
PyErr_Format(PyExc_ValueError,
186-
"The number of samples should be <= %u.", UINT32_MAX);
187+
"The number of samples should be <= %u.", UINT32_MAX);
187188
return -1;
188189
}
189190

@@ -200,16 +201,17 @@ calculate_samples_stats(PyBytesObject *samples_bytes, PyObject *samples_sizes,
200201
(*chunk_sizes)[i] = PyLong_AsSize_t(size);
201202
if ((*chunk_sizes)[i] == (size_t)-1 && PyErr_Occurred()) {
202203
PyErr_Format(PyExc_ValueError,
203-
"Items in samples_sizes should be an int "
204-
"object, with a value between 0 and %u.", SIZE_MAX);
204+
"Items in samples_sizes should be an int "
205+
"object, with a value between 0 and %u.", SIZE_MAX);
205206
return -1;
206207
}
207208
sizes_sum += (*chunk_sizes)[i];
208209
}
209210

210211
if (sizes_sum != Py_SIZE(samples_bytes)) {
211212
PyErr_SetString(PyExc_ValueError,
212-
"The samples size tuple doesn't match the concatenation's size.");
213+
"The samples size tuple doesn't match the "
214+
"concatenation's size.");
213215
return -1;
214216
}
215217
return chunks_number;
@@ -242,15 F438 +244,15 @@ _zstd_train_dict_impl(PyObject *module, PyBytesObject *samples_bytes,
242244

243245
/* Check arguments */
244246
if (dict_size <= 0) {
245-
PyErr_SetString(PyExc_ValueError, "dict_size argument should be positive number.");
247+
PyErr_SetString(PyExc_ValueError,
248+
"dict_size argument should be positive number.");
246249
return NULL;
247250
}
248251

249252
/* Check that the samples are valid and get their sizes */
250253
chunks_number = calculate_samples_stats(samples_bytes, samples_sizes,
251254
&chunk_sizes);
252-
if (chunks_number < 0)
253-
{
255+
if (chunks_number < 0) {
254256
goto error;
255257
}
256258

@@ -271,7 +273,7 @@ _zstd_train_dict_impl(PyObject *module, PyBytesObject *samples_bytes,
271273

272274
/* Check Zstandard dict error */
273275
if (ZDICT_isError(zstd_ret)) {
274-
_zstd_state* const mod_state = get_zstd_state(module);
276+
_zstd_state* mod_state = get_zstd_state(module);
275277
set_zstd_error(mod_state, ERR_TRAIN_DICT, zstd_ret);
276278
goto error;
277279
}
@@ -324,15 +326,15 @@ _zstd_finalize_dict_impl(PyObject *module, PyBytesObject *custom_dict_bytes,
324326

325327
/* Check arguments */
326328
if (dict_size <= 0) {
327-
PyErr_SetString(PyExc_ValueError, "dict_size argument should be positive number.");
329+
PyErr_SetString(PyExc_ValueError,
330+
"dict_size argument should be positive number.");
328331
return NULL;
329332
}
330333

331334
/* Check that the samples are valid and get their sizes */
332335
chunks_number = calculate_samples_stats(samples_bytes, samples_sizes,
333336
&chunk_sizes);
334-
if (chunks_number < 0)
335-
{
337+
if (chunks_number < 0) {
336338
goto error;
337339
}
338340

@@ -355,14 +357,15 @@ _zstd_finalize_dict_impl(PyObject *module, PyBytesObject *custom_dict_bytes,
355357
Py_BEGIN_ALLOW_THREADS
356358
zstd_ret = ZDICT_finalizeDictionary(
357359
PyBytes_AS_STRING(dst_dict_bytes), dict_size,
358-
PyBytes_AS_STRING(custom_dict_bytes), Py_SIZE(custom_dict_bytes),
360+
PyBytes_AS_STRING(custom_dict_bytes),
361+
Py_SIZE(custom_dict_bytes),
359362
PyBytes_AS_STRING(samples_bytes), chunk_sizes,
360363
(uint32_t)chunks_number, params);
361364
Py_END_ALLOW_THREADS
362365

363366
/* Check Zstandard dict error */
364367
if (ZDICT_isError(zstd_ret)) {
365-
_zstd_state* const mod_state = get_zstd_state(module);
368+
_zstd_state* mod_state = get_zstd_state(module);
366369
set_zstd_error(mod_state, ERR_FINALIZE_DICT, zstd_ret);
367370
goto error;
368371
}
@@ -402,15 +405,15 @@ _zstd_get_param_bounds_impl(PyObject *module, int parameter, int is_compress)
402405
if (is_compress) {
403406
bound = ZSTD_cParam_getBounds(parameter);
404407
if (ZSTD_isError(bound.error)) {
405-
_zstd_state* const mod_state = get_zstd_state(module);
408+
_zstd_state* mod_state = get_zstd_state(module);
406409
set_zstd_error(mod_state, ERR_GET_C_BOUNDS, bound.error);
407410
return NULL;
408411
}
409412
}
410413
else {
411414
bound = ZSTD_dParam_getBounds(parameter);
412415
if (ZSTD_isError(bound.error)) {
413-
_zstd_state* const mod_state = get_zstd_state(module);
416+
_zstd_state* mod_state = get_zstd_state(module);
414417
set_zstd_error(mod_state, ERR_GET_D_BOUNDS, bound.error);
415418
return NULL;
416419
}
@@ -435,9 +438,10 @@ _zstd_get_frame_size_impl(PyObject *module, Py_buffer *frame_buffer)
435438
{
436439
size_t frame_size;
437440

438-
frame_size = ZSTD_findFrameCompressedSize(frame_buffer->buf, frame_buffer->len);
441+
frame_size = ZSTD_findFrameCompressedSize(frame_buffer->buf,
442+
frame_buffer->len);
439443
if (ZSTD_isError(frame_size)) {
440-
_zstd_state* const mod_state = get_zstd_state(module);
444+
_zstd_state* mod_state = get_zstd_state(module);
441445
PyErr_Format(mod_state->ZstdError,
442446
"Error when finding the compressed size of a Zstandard frame. "
443447
"Ensure the frame_buffer argument starts from the "
@@ -473,7 +477,7 @@ _zstd_get_frame_info_impl(PyObject *module, Py_buffer *frame_buffer)
473477
/* #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
474478
#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) */
475479
if (decompressed_size == ZSTD_CONTENTSIZE_ERROR) {
476-
_zstd_state* const mod_state = get_zstd_state(module);
480+
_zstd_state* mod_state = get_zstd_state(module);
477481
PyErr_SetString(mod_state->ZstdError,
478482
"Error when getting information from the header of "
479483
"a Zstandard frame. Ensure the frame_buffer argument "
@@ -508,7 +512,7 @@ _zstd_set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type,
508512
PyObject *d_parameter_type)
509513
/*[clinic end generated code: output=f3313b1294f19502 input=75d7a953580fae5f]*/
510514
{
511-
_zstd_state* const mod_state = get_zstd_state(module);
515+
_zstd_state* mod_state = get_zstd_state(module);
512516

513517
if (!PyType_Check(c_parameter_type) || !PyType_Check(d_parameter_type)) {
514518
PyErr_SetString(PyExc_ValueError,
@@ -568,7 +572,7 @@ do { \
568572
Py_DECREF(v); \
569573
} while (0)
570574

571-
_zstd_state* const mod_state = get_zstd_state(m);
575+
_zstd_state* mod_state = get_zstd_state(m);
572576

573577
/* Reusable objects & variables */
574578
mod_state->CParameter_type = NULL;
@@ -674,7 +678,7 @@ do { \
674678
static int
675679
_zstd_traverse(PyObject *module, visitproc visit, void *arg)
676680
{
677-
_zstd_state* const mod_state = get_zstd_state(module);
681+
_zstd_state* mod_state = get_zstd_state(module);
678682

679683
Py_VISIT(mod_state->ZstdDict_type);
680684
Py_VISIT(mod_state->ZstdCompressor_type);
@@ -691,7 +695,7 @@ _zstd_traverse(PyObject *module, visitproc visit, void *arg)
691695
static int
692696
_zstd_clear(PyObject *module)
693697
{
694-
_zstd_state* const mod_state = get_zstd_state(module);
698+
_zstd_state* mod_state = get_zstd_state(module);
695699

696700
Py_CLEAR(mod_state->ZstdDict_type);
697701
Py_CLEAR(mod_state->ZstdCompressor_type);

Modules/_zstd/buffer.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ _OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
1919
/* Ensure .list was set to NULL */
2020
assert(buffer->list == NULL);
2121

22-
Py_ssize_t res = _BlocksOutputBuffer_InitAndGrow(buffer, max_length, &ob->dst);
22+
Py_ssize_t res = _BlocksOutputBuffer_InitAndGrow(buffer, max_length,
23+
&ob->dst);
2324
if (res < 0) {
2425
return -1;
2526
}
@@ -34,8 +35,7 @@ _OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
3435
Return -1 on failure */
3536
static inline int
3637
_OutputBuffer_InitWithSize(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
37-
Py_ssize_t max_length,
38-
Py_ssize_t init_size)
38+
Py_ssize_t max_length, Py_ssize_t init_size)
3939
{
4040
Py_ssize_t block_size;
4141

@@ -50,7 +50,8 @@ _OutputBuffer_InitWithSize(_BlocksOutputBuffer *buffer, ZSTD_outBuffer *ob,
5050
block_size = init_size;
5151
}
5252

53-
Py_ssize_t res = _BlocksOutputBuffer_InitWithSize(buffer, block_size, &ob->dst);
53+
Py_ssize_t res = _BlocksOutputBuffer_InitWithSize(buffer, block_size,
54+
&ob->dst);
5455
if (res < 0) {
5556
return -1;
5657
}

Modules/_zstd/clinic/zstddict.c.h

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0