Skip to content

Commit

Permalink
pythongh-101277: Add count type to module state
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Jan 25, 2023
1 parent b29e885 commit 1e0c23d
Showing 1 changed file with 30 additions and 47 deletions.
77 changes: 30 additions & 47 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct {
PyTypeObject *accumulate_type;
PyTypeObject *combinations_type;
PyTypeObject *compress_type;
PyTypeObject *count_type;
PyTypeObject *cwr_type;
PyTypeObject *cycle_type;
PyTypeObject *dropwhile_type;
Expand Down Expand Up @@ -61,15 +62,14 @@ class itertools.permutations "permutationsobject *" "clinic_state()->permutation
class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type"
class itertools.compress "compressobject *" "clinic_state()->compress_type"
class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_type"
class itertools.count "countobject *" "&count_type"
class itertools.count "countobject *" "clinic_state()->count_type"
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=041cf92c608e0a3b]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=338b4d26465f3eb1]*/

static PyTypeObject teedataobject_type;
static PyTypeObject tee_type;
static PyTypeObject batched_type;
static PyTypeObject count_type;
static PyTypeObject pairwise_type;

#include "clinic/itertoolsmodule.c.h"
Expand Down Expand Up @@ -4172,15 +4172,18 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
static void
count_dealloc(countobject *lz)
{
PyTypeObject *tp = Py_TYPE(lz);
PyObject_GC_UnTrack(lz);
Py_XDECREF(lz->long_cnt);
Py_XDECREF(lz->long_step);
Py_TYPE(lz)->tp_free(lz);
tp->tp_free(lz);
Py_DECREF(tp);
}

static int
count_traverse(countobject *lz, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(lz));
Py_VISIT(lz->long_cnt);
Py_VISIT(lz->long_step);
return 0;
Expand Down Expand Up @@ -4254,48 +4257,26 @@ static PyMethodDef count_methods[] = {
{NULL, NULL} /* sentinel */
};

static PyTypeObject count_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"itertools.count", /* tp_name */
sizeof(countobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)count_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)count_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
itertools_count__doc__, /* tp_doc */
(traverseproc)count_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)count_next, /* tp_iternext */
count_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
itertools_count, /* tp_new */
PyObject_GC_Del, /* tp_free */
static PyType_Slot count_slots[] = {
{Py_tp_dealloc, count_dealloc},
{Py_tp_repr, count_repr},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)itertools_count__doc__},
{Py_tp_traverse, count_traverse},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, count_next},
{Py_tp_methods, count_methods},
{Py_tp_new, itertools_count},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};

static PyType_Spec count_spec = {
.name = "itertools.count",
.basicsize = sizeof(countobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = count_slots,
};


Expand Down Expand Up @@ -4765,6 +4746,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
Py_VISIT(state->accumulate_type);
Py_VISIT(state->combinations_type);
Py_VISIT(state->compress_type);
Py_VISIT(state->count_type);
Py_VISIT(state->cwr_type);
Py_VISIT(state->cycle_type);
Py_VISIT(state->dropwhile_type);
Expand All @@ -4784,6 +4766,7 @@ itertoolsmodule_clear(PyObject *mod)
Py_CLEAR(state->accumulate_type);
Py_CLEAR(state->combinations_type);
Py_CLEAR(state->compress_type);
Py_CLEAR(state->count_type);
Py_CLEAR(state->cwr_type);
Py_CLEAR(state->cycle_type);
Py_CLEAR(state->dropwhile_type);
Expand Down Expand Up @@ -4820,6 +4803,7 @@ itertoolsmodule_exec(PyObject *mod)
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
ADD_TYPE(mod, state->compress_type, &compress_spec);
ADD_TYPE(mod, state->count_type, &count_spec);
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
Expand All @@ -4834,7 +4818,6 @@ itertoolsmodule_exec(PyObject *mod)
&batched_type,
&islice_type,
&chain_type,
&count_type,
&ziplongest_type,
&pairwise_type,
&product_type,
Expand Down

0 comments on commit 1e0c23d

Please sign in to comment.