diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index 3fd1b7a2fc7b90..9f25aff14c8e30 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -654,6 +654,82 @@ itertools_takewhile_vectorcall(PyObject *type, PyObject *const *args, return return_value; } +static PyObject * +islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start, + PyObject *stop, PyObject *step); + +static PyObject * +islice_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = clinic_state()->islice_type; + PyObject *iterable; + PyObject *start; + PyObject *stop = NULL; + PyObject *step = NULL; + + if ((type == base_tp || type->tp_init == base_tp->tp_init) && + !_PyArg_NoKeywords("islice", kwargs)) { + goto exit; + } + if (!_PyArg_CheckPositional("islice", PyTuple_GET_SIZE(args), 2, 4)) { + goto exit; + } + iterable = PyTuple_GET_ITEM(args, 0); + start = PyTuple_GET_ITEM(args, 1); + if (PyTuple_GET_SIZE(args) < 3) { + goto skip_optional; + } + stop = PyTuple_GET_ITEM(args, 2); + if (PyTuple_GET_SIZE(args) < 4) { + goto skip_optional; + } + step = PyTuple_GET_ITEM(args, 3); +skip_optional: + return_value = islice_new_impl(type, iterable, start, stop, step); + +exit: + return return_value; +} + +static PyObject * +islice_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + PyObject *start; + PyObject *stop = NULL; + PyObject *step = NULL; + + assert(_PyType_CAST(type)->tp_new == islice_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("islice", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("islice", nargs, 2, 4)) { + goto exit; + } + iterable = args[0]; + start = args[1]; + if (nargs < 3) { + goto skip_optional; + } + stop = args[2]; + if (nargs < 4) { + goto skip_optional; + } + step = args[3]; +skip_optional: + return_value = islice_new_impl(_PyType_CAST(type), iterable, start, stop, step); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_starmap__doc__, "starmap(function, iterable, /)\n" "--\n" @@ -1564,6 +1640,102 @@ itertools_count_vectorcall(PyObject *type, PyObject *const *args, return return_value; } +static PyObject * +repeat_new_impl(PyTypeObject *type, PyObject *element, PyObject *times); + +static PyObject * +repeat_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) + + #define NUM_KEYWORDS 2 + static struct { + PyGC_Head _this_is_not_used; + PyObject_VAR_HEAD + Py_hash_t ob_hash; + PyObject *ob_item[NUM_KEYWORDS]; + } _kwtuple = { + .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) + .ob_hash = -1, + .ob_item = { &_Py_ID(object), &_Py_ID(times), }, + }; + #undef NUM_KEYWORDS + #define KWTUPLE (&_kwtuple.ob_base.ob_base) + + #else // !Py_BUILD_CORE + # define KWTUPLE NULL + #endif // !Py_BUILD_CORE + + static const char * const _keywords[] = {"object", "times", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "repeat", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[2]; + PyObject * const *fastargs; + Py_ssize_t noptargs = nargs + nkw - 1; + PyObject *element; + PyObject *times = NULL; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); + if (!fastargs) { + goto exit; + } + element = fastargs[0]; + if (!noptargs) { + goto skip_optional_pos; + } + times = fastargs[1]; +skip_optional_pos: + return_value = repeat_new_impl(type, element, times); + +exit: + return return_value; +} + +static PyObject * +repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return repeat_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +repeat_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *element; + PyObject *times = NULL; + + assert(_PyType_CAST(type)->tp_new == repeat_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (kwnames != NULL || nargs < 1 || nargs > 2) { + return repeat_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + element = args[0]; + if (nargs < 2) { + goto skip_optional; + } + times = args[1]; +skip_optional: + return_value = repeat_new_impl(_PyType_CAST(type), element, times); + + return return_value; +} + static PyObject * zip_longest_next_impl(ziplongestobject *self); @@ -1578,4 +1750,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=60939958c9c9348b input=a9049054013a1b77]*/ +/*[clinic end generated code: output=b8e251713ba66cfc input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index d7daad8c303858..c6c9d9254d54a8 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -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" @@ -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)) @@ -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, @@ -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, @@ -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; @@ -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; @@ -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}, }; @@ -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) @@ -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}, };