Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 173 additions & 1 deletion Modules/clinic/itertoolsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 63 additions & 49 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef struct ziplongestobject ziplongestobject;

/*[clinic input]
module itertools
class itertools.islice "isliceobject *" "clinic_state()->islice_type"
class itertools.groupby "groupbyobject *" "clinic_state()->groupby_type"
class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type"
class itertools.teedataobject "teedataobject *" "clinic_state()->teedataobject_type"
Expand All @@ -96,9 +97,10 @@ class itertools.zip_longest "ziplongestobject *" "clinic_state()->ziplongest_typ
class itertools.compress "compressobject *" "clinic_state()->compress_type"
class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_type"
class itertools.count "countobject *" "clinic_state()->count_type"
class itertools.repeat "repeatobject *" "clinic_state()->repeat_type"
class itertools.pairwise "pairwiseobject *" "clinic_state()->pairwise_type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1086fb2d308d75d2]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f671f9aa97349d64]*/

#define clinic_state() (find_state_by_type(type))
#define clinic_state_by_cls() (get_module_state_by_cls(base_tp))
Expand Down Expand Up @@ -1612,29 +1614,31 @@ typedef struct {

#define isliceobject_CAST(op) ((isliceobject *)(op))

/*[clinic input]
@vectorcall
@classmethod
itertools.islice.__new__ as islice_new
iterable: object
start: object
stop: object(c_default="NULL") = None
step: object(c_default="NULL") = None
/
[clinic start generated code]*/

static PyObject *
islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start,
PyObject *stop, PyObject *step)
/*[clinic end generated code: output=9291712502695467 input=33c4713c4aa2d211]*/
{
PyObject *seq;
Py_ssize_t start=0, stop=-1, step=1;
PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL;
Py_ssize_t numargs;
PyObject *it;
Py_ssize_t start_val=0, stop_val=-1, step_val=1;
Py_ssize_t numargs = (stop == NULL) ? 2 : ((step == NULL) ? 3 : 4);
isliceobject *lz;

itertools_state *st = find_state_by_type(type);
PyTypeObject *islice_type = st->islice_type;
if ((type == islice_type || type->tp_init == islice_type->tp_init) &&
!_PyArg_NoKeywords("islice", kwds))
return NULL;

if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
return NULL;

numargs = PyTuple_Size(args);
if (numargs == 2) {
if (a1 != Py_None) {
stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError);
if (stop == -1) {
if (start != Py_None) {
stop_val = PyNumber_AsSsize_t(start, PyExc_OverflowError);
if (stop_val == -1) {
if (PyErr_Occurred())
PyErr_Clear();
PyErr_SetString(PyExc_ValueError,
Expand All @@ -1644,13 +1648,13 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
}
} else {
if (a1 != Py_None)
start = PyNumber_AsSsize_t(a1, PyExc_OverflowError);
if (start == -1 && PyErr_Occurred())
if (start != Py_None)
start_val = PyNumber_AsSsize_t(start, PyExc_OverflowError);
if (start_val == -1 && PyErr_Occurred())
PyErr_Clear();
if (a2 != Py_None) {
stop = PyNumber_AsSsize_t(a2, PyExc_OverflowError);
if (stop == -1) {
if (stop != Py_None) {
stop_val = PyNumber_AsSsize_t(stop, PyExc_OverflowError);
if (stop_val == -1) {
if (PyErr_Occurred())
PyErr_Clear();
PyErr_SetString(PyExc_ValueError,
Expand All @@ -1660,27 +1664,27 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
}
}
if (start<0 || stop<-1) {
if (start_val<0 || stop_val<-1) {
PyErr_SetString(PyExc_ValueError,
"Indices for islice() must be None or "
"an integer: 0 <= x <= sys.maxsize.");
return NULL;
}

if (a3 != NULL) {
if (a3 != Py_None)
step = PyNumber_AsSsize_t(a3, PyExc_OverflowError);
if (step == -1 && PyErr_Occurred())
if (step != NULL) {
if (step != Py_None)
step_val = PyNumber_AsSsize_t(step, PyExc_OverflowError);
if (step_val == -1 && PyErr_Occurred())
PyErr_Clear();
}
if (step<1) {
if (step_val<1) {
PyErr_SetString(PyExc_ValueError,
"Step for islice() must be a positive integer or None.");
return NULL;
}

/* Get iterator. */
it = PyObject_GetIter(seq);
it = PyObject_GetIter(iterable);
if (it == NULL)
return NULL;

Expand All @@ -1691,9 +1695,9 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
lz->it = it;
lz->next = start;
lz->stop = stop;
lz->step = step;
lz->next = start_val;
lz->stop = stop_val;
lz->step = step_val;
lz->cnt = 0L;

return (PyObject *)lz;
Expand Down Expand Up @@ -1778,6 +1782,7 @@ static PyType_Slot islice_slots[] = {
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, islice_next},
{Py_tp_new, islice_new},
{Py_tp_vectorcall, islice_vectorcall},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
Expand Down Expand Up @@ -3748,23 +3753,31 @@ typedef struct {

#define repeatobject_CAST(op) ((repeatobject *)(op))

/*[clinic input]
@vectorcall
@classmethod
itertools.repeat.__new__ as repeat_new
object as element: object
times: object(c_default="NULL") = None
[clinic start generated code]*/

static PyObject *
repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
repeat_new_impl(PyTypeObject *type, PyObject *element, PyObject *times)
/*[clinic end generated code: output=4ef9bd3593335622 input=c0eafe81f60ae9e6]*/
{
repeatobject *ro;
PyObject *element;
Py_ssize_t cnt = -1, n_args;
static char *kwargs[] = {"object", "times", NULL};

n_args = PyTuple_GET_SIZE(args);
if (kwds != NULL)
n_args += PyDict_GET_SIZE(kwds);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
&element, &cnt))
return NULL;
/* Does user supply times argument? */
if (n_args == 2 && cnt < 0)
cnt = 0;
Py_ssize_t cnt = -1;

if (times != NULL) {
/* Does user supply times argument? */
cnt = PyNumber_AsSsize_t(times, PyExc_OverflowError);
if (cnt == -1 && PyErr_Occurred()) {
return NULL;
}
if (cnt < 0) {
cnt = 0;
}
}

ro = (repeatobject *)type->tp_alloc(type, 0);
if (ro == NULL)
Expand Down Expand Up @@ -3855,6 +3868,7 @@ static PyType_Slot repeat_slots[] = {
{Py_tp_iternext, repeat_next},
{Py_tp_methods, repeat_methods},
{Py_tp_new, repeat_new},
{Py_tp_vectorcall, repeat_vectorcall},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};
Expand Down
Loading