Skip to content

Commit 446b751

Browse files
pythongh-101277: Add compress type to module state
1 parent 19d43d8 commit 446b751

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;
@@ -3830,15 +3830,18 @@ itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2)
38303830
static void
38313831
compress_dealloc(compressobject *lz)
38323832
{
3833+
PyTypeObject *tp = Py_TYPE(lz);
38333834
PyObject_GC_UnTrack(lz);
38343835
Py_XDECREF(lz->data);
38353836
Py_XDECREF(lz->selectors);
3836-
Py_TYPE(lz)->tp_free(lz);
3837+
tp->tp_free(lz);
3838+
Py_DECREF(tp);
38373839
}
38383840

38393841
static int
38403842
compress_traverse(compressobject *lz, visitproc visit, void *arg)
38413843
{
3844+
Py_VISIT(Py_TYPE(lz));
38423845
Py_VISIT(lz->data);
38433846
Py_VISIT(lz->selectors);
38443847
return 0;
@@ -3893,48 +3896,25 @@ static PyMethodDef compress_methods[] = {
38933896
{NULL, NULL} /* sentinel */
38943897
};
38953898

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

39403920

@@ -4803,6 +4783,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
48034783
itertools_state *state = get_module_state(mod);
48044784
Py_VISIT(state->accumulate_type);
48054785
Py_VISIT(state->combinations_type);
4786+
Py_VISIT(state->compress_type);
48064787
Py_VISIT(state->cwr_type);
48074788
Py_VISIT(state->cycle_type);
48084789
Py_VISIT(state->dropwhile_type);
@@ -4820,6 +4801,7 @@ itertoolsmodule_clear(PyObject *mod)
48204801
itertools_state *state = get_module_state(mod);
48214802
Py_CLEAR(state->accumulate_type);
48224803
Py_CLEAR(state->combinations_type);
4804+
Py_CLEAR(state->compress_type);
48234805
Py_CLEAR(state->cwr_type);
48244806
Py_CLEAR(state->cycle_type);
48254807
Py_CLEAR(state->dropwhile_type);
@@ -4854,6 +4836,7 @@ itertoolsmodule_exec(PyObject *mod)
48544836
itertools_state *state = get_module_state(mod);
48554837
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
48564838
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
4839+
ADD_TYPE(mod, state->compress_type, &compress_spec);
48574840
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
48584841
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
48594842
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
@@ -4867,7 +4850,6 @@ itertoolsmodule_exec(PyObject *mod)
48674850
&batched_type,
48684851
&islice_type,
48694852
&chain_type,
4870-
&compress_type,
48714853
&filterfalse_type,
48724854
&count_type,
48734855
&ziplongest_type,

0 commit comments

Comments
 (0)