Skip to content

Commit e42e834

Browse files
pythongh-101277: Add compress type to module state
1 parent 6d5d4d2 commit e42e834

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
@@ -14,6 +14,7 @@
1414
typedef struct {
1515
PyTypeObject *accumulate_type;
1616
PyTypeObject *combinations_type;
17+
PyTypeObject *compress_type;
1718
PyTypeObject *cwr_type;
1819
PyTypeObject *cycle_type;
1920
PyTypeObject *dropwhile_type;
@@ -67,17 +68,16 @@ class itertools.combinations "combinationsobject *" "clinic_state()->combination
6768
class itertools.combinations_with_replacement "cwr_object *" "clinic_state()->cwr_type"
6869
class itertools.permutations "permutationsobject *" "clinic_state()->permutations_type"
6970
class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type"
70-
class itertools.compress "compressobject *" "&compress_type"
71+
class itertools.compress "compressobject *" "clinic_state()->compress_type"
7172
class itertools.filterfalse "filterfalseobject *" "&filterfalse_type"
7273
class itertools.count "countobject *" "&count_type"
7374
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
7475
[clinic start generated code]*/
75-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e0155dd6d01d40dd]*/
76+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=18f0df1fc6fbed08]*/
7677

7778
static PyTypeObject teedataobject_type;
7879
static PyTypeObject tee_type;
7980
static PyTypeObject batched_type;
80-
static PyTypeObject compress_type;
8181
static PyTypeObject filterfalse_type;
8282
static PyTypeObject count_type;
8383
static PyTypeObject pairwise_type;
@@ -3844,15 +3844,18 @@ itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2)
38443844
static void
38453845
compress_dealloc(compressobject *lz)
38463846
{
3847+
PyTypeObject *tp = Py_TYPE(lz);
38473848
PyObject_GC_UnTrack(lz);
38483849
Py_XDECREF(lz->data);
38493850
Py_XDECREF(lz->selectors);
3850-
Py_TYPE(lz)->tp_free(lz);
3851+
tp->tp_free(lz);
3852+
Py_DECREF(tp);
38513853
}
38523854

38533855
static int
38543856
compress_traverse(compressobject *lz, visitproc visit, void *arg)
38553857
{
3858+
Py_VISIT(Py_TYPE(lz));
38563859
Py_VISIT(lz->data);
38573860
Py_VISIT(lz->selectors);
38583861
return 0;
@@ -3907,48 +3910,25 @@ static PyMethodDef compress_methods[] = {
39073910
{NULL, NULL} /* sentinel */
39083911
};
39093912

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

39543934

@@ -4817,6 +4797,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
48174797
itertools_state *state = get_module_state(mod);
48184798
Py_VISIT(state->accumulate_type);
48194799
Py_VISIT(state->combinations_type);
4800+
Py_VISIT(state->compress_type);
48204801
Py_VISIT(state->cwr_type);
48214802
Py_VISIT(state->cycle_type);
48224803
Py_VISIT(state->dropwhile_type);
@@ -4834,6 +4815,7 @@ itertoolsmodule_clear(PyObject *mod)
48344815
itertools_state *state = get_module_state(mod);
48354816
Py_CLEAR(state->accumulate_type);
48364817
Py_CLEAR(state->combinations_type);
4818+
Py_CLEAR(state->compress_type);
48374819
Py_CLEAR(state->cwr_type);
48384820
Py_CLEAR(state->cycle_type);
48394821
Py_CLEAR(state->dropwhile_type);
@@ -4868,6 +4850,7 @@ itertoolsmodule_exec(PyObject *mod)
48684850
itertools_state *state = get_module_state(mod);
48694851
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
48704852
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
4853+
ADD_TYPE(mod, state->compress_type, &compress_spec);
48714854
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
48724855
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
48734856
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
@@ -4881,7 +4864,6 @@ itertoolsmodule_exec(PyObject *mod)
48814864
&batched_type,
48824865
&islice_type,
48834866
&chain_type,
4884-
&compress_type,
48854867
&filterfalse_type,
48864868
&count_type,
48874869
&ziplongest_type,

0 commit comments

Comments
 (0)