Skip to content

Commit bf4b07d

Browse files
committed
Remove _set_parameter_types
Python, in general, works on protocols rather than nominal or specific types. I think this check is overly restrictive, with only minor benefit. Users wanting validation can use static type checkers.
1 parent 7f8c350 commit bf4b07d

File tree

6 files changed

+1
-144
lines changed

6 files changed

+1
-144
lines changed

Lib/compression/zstd/__init__.py

-4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,3 @@ class Strategy(enum.IntEnum):
228228
btopt = _zstd._ZSTD_btopt
229229
btultra = _zstd._ZSTD_btultra
230230
btultra2 = _zstd._ZSTD_btultra2
231-
232-
233-
# Check validity of the CompressionParameter & DecompressionParameter types
234-
_zstd._set_parameter_types(CompressionParameter, DecompressionParameter)

Modules/_zstd/_zstdmodule.c

-46
Original file line numberDiff line numberDiff line change
@@ -510,49 +510,12 @@ _zstd__get_frame_info_impl(PyObject *module, Py_buffer *frame_buffer)
510510
return Py_BuildValue("KI", decompressed_size, dict_id);
511511
}
512512

513-
/*[clinic input]
514-
_zstd._set_parameter_types
515-
516-
c_parameter_type: object(subclass_of='&PyType_Type')
517-
CompressionParameter IntEnum type object
518-
d_parameter_type: object(subclass_of='&PyType_Type')
519-
DecompressionParameter IntEnum type object
520-
521-
Internal function, set CompressionParameter/DecompressionParameter types for validity check.
522-
[clinic start generated code]*/
523-
524-
static PyObject *
525-
_zstd__set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type,
526-
PyObject *d_parameter_type)
527-
/*[clinic end generated code: output=a13d4890ccbd2873 input=4535545d903853d3]*/
528-
{
529-
_zstd_state* const mod_state = get_zstd_state(module);
530-
531-
if (!PyType_Check(c_parameter_type) || !PyType_Check(d_parameter_type)) {
532-
PyErr_SetString(PyExc_ValueError,
533-
"The two arguments should be CompressionParameter and "
534-
"DecompressionParameter types.");
535-
return NULL;
536-
}
537-
538-
Py_XDECREF(mod_state->CParameter_type);
539-
Py_INCREF(c_parameter_type);
540-
mod_state->CParameter_type = (PyTypeObject*)c_parameter_type;
541-
542-
Py_XDECREF(mod_state->DParameter_type);
543-
Py_INCREF(d_parameter_type);
544-
mod_state->DParameter_type = (PyTypeObject*)d_parameter_type;
545-
546-
Py_RETURN_NONE;
547-
}
548-
549513
static PyMethodDef _zstd_methods[] = {
550514
_ZSTD__TRAIN_DICT_METHODDEF
551515
_ZSTD__FINALIZE_DICT_METHODDEF
552516
_ZSTD__GET_PARAM_BOUNDS_METHODDEF
553517
_ZSTD_GET_FRAME_SIZE_METHODDEF
554518
_ZSTD__GET_FRAME_INFO_METHODDEF
555-
_ZSTD__SET_PARAMETER_TYPES_METHODDEF
556519

557520
{0}
558521
};
@@ -766,9 +729,6 @@ static int _zstd_exec(PyObject *module) {
766729
ADD_STR_TO_STATE_MACRO(write);
767730
ADD_STR_TO_STATE_MACRO(flush);
768731

769-
mod_state->CParameter_type = NULL;
770-
mod_state->DParameter_type = NULL;
771-
772732
/* Add variables to module */
773733
if (add_vars_to_module(module) < 0) {
774734
return -1;
@@ -852,9 +812,6 @@ _zstd_traverse(PyObject *module, visitproc visit, void *arg)
852812
Py_VISIT(mod_state->ZstdDecompressor_type);
853813

854814
Py_VISIT(mod_state->ZstdError);
855-
856-
Py_VISIT(mod_state->CParameter_type);
857-
Py_VISIT(mod_state->DParameter_type);
858815
return 0;
859816
}
860817

@@ -876,9 +833,6 @@ _zstd_clear(PyObject *module)
876833
Py_CLEAR(mod_state->ZstdDecompressor_type);
877834

878835
Py_CLEAR(mod_state->ZstdError);
879-
880-
Py_CLEAR(mod_state->CParameter_type);
881-
Py_CLEAR(mod_state->DParameter_type);
882836
return 0;
883837
}
884838

Modules/_zstd/_zstdmodule.h

-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ struct _zstd_state {
5454
PyTypeObject *ZstdCompressor_type;
5555
PyTypeObject *ZstdDecompressor_type;
5656
PyObject *ZstdError;
57-
58-
PyTypeObject *CParameter_type;
59-
PyTypeObject *DParameter_type;
6057
};
6158

6259
typedef struct {

Modules/_zstd/clinic/_zstdmodule.c.h

+1-75
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_zstd/compressor.c

-8
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ _PyZstd_set_c_parameters(ZstdCompressor *self, PyObject *level_or_options,
6767
Py_ssize_t pos = 0;
6868

6969
while (PyDict_Next(level_or_options, &pos, &key, &value)) {
70-
/* Check key type */
71-
if (Py_TYPE(key) == mod_state->DParameter_type) {
72-
PyErr_SetString(PyExc_TypeError,
73-
"Key of compression option dict should "
74-
"NOT be DecompressionParameter.");
75-
return -1;
76-
}
77-
7870
int key_v = PyLong_AsInt(key);
7971
if (key_v == -1 && PyErr_Occurred()) {
8072
PyErr_SetString(PyExc_ValueError,

Modules/_zstd/decompressor.c

-8
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@ _PyZstd_set_d_parameters(ZstdDecompressor *self, PyObject *options)
8080

8181
pos = 0;
8282
while (PyDict_Next(options, &pos, &key, &value)) {
83-
/* Check key type */
84-
if (Py_TYPE(key) == mod_state->CParameter_type) {
85-
PyErr_SetString(PyExc_TypeError,
86-
"Key of decompression options dict should "
87-
"NOT be CompressionParameter.");
88-
return -1;
89-
}
90-
9183
/* Both key & value should be 32-bit signed int */
9284
int key_v = PyLong_AsInt(key);
9385
if (key_v == -1 && PyErr_Occurred()) {

0 commit comments

Comments
 (0)