Skip to content

Commit 04e8c29

Browse files
pythongh-101277: Add compress type to module state
1 parent 89b8e34 commit 04e8c29

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

Modules/itertoolsmodule.c

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
typedef struct {
1414
PyTypeObject *accumulate_type;
1515
PyTypeObject *combinations_type;
16+
PyTypeObject *compress_type;
1617
PyTypeObject *cwr_type;
1718
PyTypeObject *cycle_type;
1819
PyTypeObject *dropwhile_type;
@@ -57,17 +58,16 @@ class itertools.combinations "combinationsobject *" "clinic_state()->combination
5758
class itertools.combinations_with_replacement "cwr_object *" "clinic_state()->cwr_type"
5859
class itertools.permutations "permutationsobject *" "clinic_state()->permutations_type"
5960
class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type"
60-
class itertools.compress "compressobject *" "&compress_type"
61+
class itertools.compress "compressobject *" "clinic_state()->compress_type"
6162
class itertools.filterfalse "filterfalseobject *" "&filterfalse_type"
6263
class itertools.count "countobject *" "&count_type"
6364
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
6465
[clinic start generated code]*/
65-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e0155dd6d01d40dd]*/
66+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=18f0df1fc6fbed08]*/
6667

6768
static PyTypeObject teedataobject_type;
6869
static PyTypeObject tee_type;
6970
static PyTypeObject batched_type;
70-
static PyTypeObject compress_type;
7171
static PyTypeObject filterfalse_type;
7272
static PyTypeObject count_type;
7373
static PyTypeObject pairwise_type;
@@ -3831,15 +3831,18 @@ itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2)
38313831
static void
38323832
compress_dealloc(compressobject *lz)
38333833
{
3834+
PyTypeObject *tp = Py_TYPE(lz);
38343835
PyObject_GC_UnTrack(lz);
38353836
Py_XDECREF(lz->data);
38363837
Py_XDECREF(lz->selectors);
3837-
Py_TYPE(lz)->tp_free(lz);
3838+
tp->tp_free(lz);
3839+
Py_DECREF(tp);
38383840
}
38393841

38403842
static int
38413843
compress_traverse(compressobject *lz, visitproc visit, void *arg)
38423844
{
3845+
Py_VISIT(Py_TYPE(lz));
38433846
Py_VISIT(lz->data);
38443847
Py_VISIT(lz->selectors);
38453848
return 0;
@@ -3894,48 +3897,25 @@ static PyMethodDef compress_methods[] = {
38943897
{NULL, NULL} /* sentinel */
38953898
};
38963899

3897-
static PyTypeObject compress_type = {
3898-
PyVarObject_HEAD_INIT(NULL, 0)
3899-
"itertools.compress", /* tp_name */
3900-
sizeof(compressobject), /* tp_basicsize */
3901-
0, /* tp_itemsize */
3902-
/* methods */
3903-
(destructor)compress_dealloc, /* tp_dealloc */
3904-
0, /* tp_vectorcall_offset */
3905-
0, /* tp_getattr */
3906-
0, /* tp_setattr */
3907-
0, /* tp_as_async */
3908-
0, /* tp_repr */
3909-
0, /* tp_as_number */
3910-
0, /* tp_as_sequence */
3911-
0, /* tp_as_mapping */
3912-
0, /* tp_hash */
3913-
0, /* tp_call */
3914-
0, /* tp_str */
3915-
PyObject_GenericGetAttr, /* tp_getattro */
3916-
0, /* tp_setattro */
3917-
0, /* tp_as_buffer */
3918-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3919-
Py_TPFLAGS_BASETYPE, /* tp_flags */
3920-
itertools_compress__doc__, /* tp_doc */
3921-
(traverseproc)compress_traverse, /* tp_traverse */
3922-
0, /* tp_clear */
3923-
0, /* tp_richcompare */
3924-
0, /* tp_weaklistoffset */
3925-
PyObject_SelfIter, /* tp_iter */
3926-
(iternextfunc)compress_next, /* tp_iternext */
3927-
compress_methods, /* tp_methods */
3928-
0, /* tp_members */
3929-
0, /* tp_getset */
3930-
0, /* tp_base */
3931-
0, /* tp_dict */
3932-
0, /* tp_descr_get */
3933-
0, /* tp_descr_set */
3934-
0, /* tp_dictoffset */
3935-
0, /* tp_init */
3936-
0, /* tp_alloc */
3937-
itertools_compress, /* tp_new */
3938-
PyObject_GC_Del, /* tp_free */
3900+
static PyType_Slot compress_slots[] = {
3901+
{Py_tp_dealloc, compress_dealloc},
3902+
{Py_tp_getattro, PyObject_GenericGetAttr},
3903+
{Py_tp_doc, (void *)itertools_compress__doc__},
3904+
{Py_tp_traverse, compress_traverse},
3905+
{Py_tp_iter, PyObject_SelfIter},
3906+
{Py_tp_iternext, compress_next},
3907+
{Py_tp_methods, compress_methods},
3908+
{Py_tp_new, itertools_compress},
3909+
{Py_tp_free, PyObject_GC_Del},
3910+
{0, NULL},
3911+
};
3912+
3913+
static PyType_Spec compress_spec = {
3914+
.name = "itertools.compress",
3915+
.basicsize = sizeof(compressobject),
3916+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
3917+
Py_TPFLAGS_IMMUTABLETYPE),
3918+
.slots = compress_slots,
39393919
};
39403920

39413921

@@ -4804,6 +4784,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
48044784
itertools_state *state = get_module_state(mod);
48054785
Py_VISIT(state->accumulate_type);
48064786
Py_VISIT(state->combinations_type);
4787+
Py_VISIT(state->compress_type);
48074788
Py_VISIT(state->cwr_type);
48084789
Py_VISIT(state->cycle_type);
48094790
Py_VISIT(state->dropwhile_type);
@@ -4821,6 +4802,7 @@ itertoolsmodule_clear(PyObject *mod)
48214802
itertools_state *state = get_module_state(mod);
48224803
Py_CLEAR(state->accumulate_type);
48234804
Py_CLEAR(state->combinations_type);
4805+
Py_CLEAR(state->compress_type);
48244806
Py_CLEAR(state->cwr_type);
48254807
Py_CLEAR(state->cycle_type);
48264808
Py_CLEAR(state->dropwhile_type);
@@ -4855,6 +4837,7 @@ itertoolsmodule_exec(PyObject *mod)
48554837
itertools_state *state = get_module_state(mod);
48564838
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
48574839
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
4840+
ADD_TYPE(mod, state->compress_type, &compress_spec);
48584841
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
48594842
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
48604843
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
@@ -4868,7 +4851,6 @@ itertoolsmodule_exec(PyObject *mod)
48684851
&batched_type,
48694852
&islice_type,
48704853
&chain_type,
4871-
&compress_type,
48724854
&filterfalse_type,
48734855
&count_type,
48744856
&ziplongest_type,

0 commit comments

Comments
 (0)