diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index 3fd1b7a2fc7b90..e74ebdf2e66c88 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 *a1, + PyObject *a2, PyObject *a3); + +static PyObject * +islice_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = clinic_state()->islice_type; + PyObject *iterable; + PyObject *a1; + PyObject *a2 = NULL; + PyObject *a3 = 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); + a1 = PyTuple_GET_ITEM(args, 1); + if (PyTuple_GET_SIZE(args) < 3) { + goto skip_optional; + } + a2 = PyTuple_GET_ITEM(args, 2); + if (PyTuple_GET_SIZE(args) < 4) { + goto skip_optional; + } + a3 = PyTuple_GET_ITEM(args, 3); +skip_optional: + return_value = islice_new_impl(type, iterable, a1, a2, a3); + +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 *a1; + PyObject *a2 = NULL; + PyObject *a3 = 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]; + a1 = args[1]; + if (nargs < 3) { + goto skip_optional; + } + a2 = args[2]; + if (nargs < 4) { + goto skip_optional; + } + a3 = args[3]; +skip_optional: + return_value = islice_new_impl(_PyType_CAST(type), iterable, a1, a2, a3); + +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=392dbd56fc2a10e5 input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index d7daad8c303858..20ee9fc5de25c1 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,25 +1614,34 @@ typedef struct { #define isliceobject_CAST(op) ((isliceobject *)(op)) +/*[clinic input] +@vectorcall +@classmethod +itertools.islice.__new__ as islice_new + iterable: object + a1: object + a2: object(c_default="NULL") = None + a3: 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 *a1, + PyObject *a2, PyObject *a3) +/*[clinic end generated code: output=7fa28024c01231db input=aac6a09144cead64]*/ { - PyObject *seq; + PyObject *it = NULL; Py_ssize_t start=0, stop=-1, step=1; - PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; - Py_ssize_t numargs; + Py_ssize_t numargs = (a2 == NULL) ? 2 : ((a3 == 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; + /* vectorcall passes borrowed references; hold strong references before + PyNumber_AsSsize_t() or PyObject_GetIter() can execute arbitrary Python code. */ + Py_XINCREF(iterable); + Py_XINCREF(a1); + Py_XINCREF(a2); + Py_XINCREF(a3); - numargs = PyTuple_Size(args); if (numargs == 2) { if (a1 != Py_None) { stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError); @@ -1640,7 +1651,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyErr_SetString(PyExc_ValueError, "Stop argument for islice() must be None or " "an integer: 0 <= x <= sys.maxsize."); - return NULL; + goto error; } } } else { @@ -1656,7 +1667,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyErr_SetString(PyExc_ValueError, "Stop argument for islice() must be None or " "an integer: 0 <= x <= sys.maxsize."); - return NULL; + goto error; } } } @@ -1664,7 +1675,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyErr_SetString(PyExc_ValueError, "Indices for islice() must be None or " "an integer: 0 <= x <= sys.maxsize."); - return NULL; + goto error; } if (a3 != NULL) { @@ -1676,27 +1687,38 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (step<1) { PyErr_SetString(PyExc_ValueError, "Step for islice() must be a positive integer or None."); - return NULL; + goto error; } /* Get iterator. */ - it = PyObject_GetIter(seq); + it = PyObject_GetIter(iterable); if (it == NULL) - return NULL; + goto error; /* create isliceobject structure */ lz = (isliceobject *)type->tp_alloc(type, 0); - if (lz == NULL) { - Py_DECREF(it); - return NULL; - } + if (lz == NULL) + goto error; lz->it = it; + it = NULL; lz->next = start; lz->stop = stop; lz->step = step; lz->cnt = 0L; + Py_DECREF(iterable); + Py_XDECREF(a1); + Py_XDECREF(a2); + Py_XDECREF(a3); return (PyObject *)lz; + +error: + Py_XDECREF(it); + Py_XDECREF(iterable); + Py_XDECREF(a1); + Py_XDECREF(a2); + Py_XDECREF(a3); + return NULL; } static void @@ -1778,6 +1800,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,30 +3771,46 @@ typedef struct { #define repeatobject_CAST(op) ((repeatobject *)(op)) +/*[clinic input] +@vectorcall +@classmethod +itertools.repeat.__new__ as repeat_new + object as element: object + times: object = NULL +[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=57cee9a4aba720da]*/ { 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; + + /* vectorcall passes borrowed references; hold them before PyNumber_AsSsize_t() can + execute arbitrary Python code. */ + Py_INCREF(element); + + if (times != NULL) { + /* Does user supply times argument? */ + cnt = PyNumber_AsSsize_t(times, PyExc_OverflowError); + if (cnt == -1 && PyErr_Occurred()) { + goto error; + } + if (cnt < 0) { + cnt = 0; + } + } ro = (repeatobject *)type->tp_alloc(type, 0); if (ro == NULL) - return NULL; - ro->element = Py_NewRef(element); + goto error; + ro->element = element; /* transfer the reference taken above */ ro->cnt = cnt; return (PyObject *)ro; + +error: + Py_DECREF(element); + return NULL; } static void @@ -3855,6 +3894,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}, };