diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index d6365b46f04b26..3fd1b7a2fc7b90 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -36,7 +36,8 @@ batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n, int strict); static PyObject * -batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +batched_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) @@ -68,13 +69,12 @@ batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[3]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; + Py_ssize_t noptargs = nargs + nkw - 2; PyObject *iterable; Py_ssize_t n; int strict = 0; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -106,6 +106,30 @@ batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return batched_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +batched_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == batched_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)); + return batched_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + PyDoc_STRVAR(pairwise_new__doc__, "pairwise(iterable, /)\n" "--\n" @@ -138,6 +162,31 @@ pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +pairwise_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + + assert(_PyType_CAST(type)->tp_new == pairwise_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("pairwise", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("pairwise", nargs, 1, 1)) { + goto exit; + } + iterable = args[0]; + return_value = pairwise_new_impl(_PyType_CAST(type), iterable); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_groupby__doc__, "groupby(iterable, key=None)\n" "--\n" @@ -155,7 +204,8 @@ static PyObject * itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc); static PyObject * -itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_groupby_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) @@ -187,12 +237,11 @@ itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *it; PyObject *keyfunc = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -209,6 +258,44 @@ itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_groupby_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_groupby_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *it; + PyObject *keyfunc = Py_None; + + assert(_PyType_CAST(type)->tp_new == itertools_groupby); + /* 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 itertools_groupby_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + it = args[0]; + if (nargs < 2) { + goto skip_optional; + } + keyfunc = args[1]; +skip_optional: + return_value = itertools_groupby_impl(_PyType_CAST(type), it, keyfunc); + + return return_value; +} + static PyObject * itertools__grouper_impl(PyTypeObject *type, PyObject *parent, PyObject *tgtkey); @@ -279,6 +366,39 @@ itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_teedataobject_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *it; + PyObject *values; + PyObject *next; + + assert(_PyType_CAST(type)->tp_new == itertools_teedataobject); + /* 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("teedataobject", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("teedataobject", nargs, 3, 3)) { + goto exit; + } + it = args[0]; + if (!PyList_Check(args[1])) { + _PyArg_BadArgument("teedataobject", "argument 2", "list", args[1]); + goto exit; + } + values = args[1]; + next = args[2]; + return_value = itertools_teedataobject_impl(_PyType_CAST(type), it, values, next); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools__tee__doc__, "_tee(iterable, /)\n" "--\n" @@ -389,6 +509,31 @@ itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_cycle_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + + assert(_PyType_CAST(type)->tp_new == itertools_cycle); + /* 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("cycle", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("cycle", nargs, 1, 1)) { + goto exit; + } + iterable = args[0]; + return_value = itertools_cycle_impl(_PyType_CAST(type), iterable); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_dropwhile__doc__, "dropwhile(predicate, iterable, /)\n" "--\n" @@ -423,6 +568,33 @@ itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_dropwhile_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_dropwhile); + /* 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("dropwhile", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("dropwhile", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_dropwhile_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_takewhile__doc__, "takewhile(predicate, iterable, /)\n" "--\n" @@ -455,6 +627,33 @@ itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_takewhile_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_takewhile); + /* 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("takewhile", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("takewhile", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_takewhile_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_starmap__doc__, "starmap(function, iterable, /)\n" "--\n" @@ -487,6 +686,33 @@ itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_starmap_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_starmap); + /* 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("starmap", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("starmap", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_starmap_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_chain_from_iterable__doc__, "from_iterable($type, iterable, /)\n" "--\n" @@ -552,7 +778,8 @@ itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r); static PyObject * -itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_combinations_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) @@ -584,11 +811,10 @@ itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *iterable; Py_ssize_t r; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -617,6 +843,57 @@ itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_combinations_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_combinations_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + Py_ssize_t r; + + assert(_PyType_CAST(type)->tp_new == itertools_combinations); + /* 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 < 2 || nargs > 2) { + return itertools_combinations_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + iterable = args[0]; + { + Py_ssize_t ival = -1; + PyObject *iobj = _PyNumber_Index(args[1]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + r = ival; + if (r < 0) { + PyErr_SetString(PyExc_ValueError, + "r cannot be negative"); + goto exit; + } + } + return_value = itertools_combinations_impl(_PyType_CAST(type), iterable, r); + +exit: + return return_value; +} + static PyObject * combinations_next_impl(combinationsobject *self); @@ -646,7 +923,8 @@ itertools_combinations_with_replacement_impl(PyTypeObject *type, Py_ssize_t r); static PyObject * -itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_combinations_with_replacement_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) @@ -678,11 +956,10 @@ itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyOb #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *iterable; Py_ssize_t r; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -711,6 +988,57 @@ itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyOb return return_value; } +static PyObject * +itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_combinations_with_replacement_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_combinations_with_replacement_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *iterable; + Py_ssize_t r; + + assert(_PyType_CAST(type)->tp_new == itertools_combinations_with_replacement); + /* 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 < 2 || nargs > 2) { + return itertools_combinations_with_replacement_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + iterable = args[0]; + { + Py_ssize_t ival = -1; + PyObject *iobj = _PyNumber_Index(args[1]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + r = ival; + if (r < 0) { + PyErr_SetString(PyExc_ValueError, + "r cannot be negative"); + goto exit; + } + } + return_value = itertools_combinations_with_replacement_impl(_PyType_CAST(type), iterable, r); + +exit: + return return_value; +} + static PyObject * cwr_next_impl(cwrobject *self); @@ -739,7 +1067,8 @@ itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, PyObject *robj); static PyObject * -itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_permutations_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) @@ -771,12 +1100,11 @@ itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *iterable; PyObject *robj = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -793,6 +1121,44 @@ itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_permutations_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_permutations_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 *robj = Py_None; + + assert(_PyType_CAST(type)->tp_new == itertools_permutations); + /* 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 itertools_permutations_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + iterable = args[0]; + if (nargs < 2) { + goto skip_optional; + } + robj = args[1]; +skip_optional: + return_value = itertools_permutations_impl(_PyType_CAST(type), iterable, robj); + + return return_value; +} + static PyObject * permutations_next_impl(permutationsobject *self); @@ -819,7 +1185,8 @@ itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, PyObject *binop, PyObject *initial); static PyObject * -itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_accumulate_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) @@ -851,13 +1218,12 @@ itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[3]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; + Py_ssize_t noptargs = nargs + nkw - 1; PyObject *iterable; PyObject *binop = Py_None; PyObject *initial = Py_None; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -884,6 +1250,30 @@ itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_accumulate_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_accumulate_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == itertools_accumulate); + /* 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)); + return itertools_accumulate_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + static PyObject * accumulate_next_impl(accumulateobject *self); @@ -912,7 +1302,8 @@ static PyObject * itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2); static PyObject * -itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_compress_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) @@ -944,11 +1335,10 @@ itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); PyObject *seq1; PyObject *seq2; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -961,6 +1351,40 @@ itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_compress_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_compress_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *seq1; + PyObject *seq2; + + assert(_PyType_CAST(type)->tp_new == itertools_compress); + /* 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 < 2 || nargs > 2) { + return itertools_compress_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + seq1 = args[0]; + seq2 = args[1]; + return_value = itertools_compress_impl(_PyType_CAST(type), seq1, seq2); + + return return_value; +} + PyDoc_STRVAR(itertools_filterfalse__doc__, "filterfalse(function, iterable, /)\n" "--\n" @@ -995,6 +1419,33 @@ itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_filterfalse_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *func; + PyObject *seq; + + assert(_PyType_CAST(type)->tp_new == itertools_filterfalse); + /* 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("filterfalse", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("filterfalse", nargs, 2, 2)) { + goto exit; + } + func = args[0]; + seq = args[1]; + return_value = itertools_filterfalse_impl(_PyType_CAST(type), func, seq); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_count__doc__, "count(start=0, step=1)\n" "--\n" @@ -1013,7 +1464,8 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, PyObject *long_step); static PyObject * -itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) +itertools_count_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) @@ -1045,12 +1497,11 @@ itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) #undef KWTUPLE PyObject *argsbuf[2]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; + Py_ssize_t noptargs = nargs + nkw - 0; PyObject *long_cnt = NULL; PyObject *long_step = NULL; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; @@ -1072,6 +1523,47 @@ itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return itertools_count_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +itertools_count_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *long_cnt = NULL; + PyObject *long_step = NULL; + + assert(_PyType_CAST(type)->tp_new == itertools_count); + /* 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 > 2) { + return itertools_count_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); + } + if (nargs < 1) { + goto skip_optional; + } + long_cnt = args[0]; + if (nargs < 2) { + goto skip_optional; + } + long_step = args[1]; +skip_optional: + return_value = itertools_count_impl(_PyType_CAST(type), long_cnt, long_step); + + return return_value; +} + static PyObject * zip_longest_next_impl(ziplongestobject *self); @@ -1086,4 +1578,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=4b55088ac24582b7 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=60939958c9c9348b input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 636b28fa06ef2e..d7daad8c303858 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -120,6 +120,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.batched.__new__ as batched_new iterable: object @@ -149,7 +150,7 @@ than n. static PyObject * batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n, int strict) -/*[clinic end generated code: output=c6de11b061529d3e input=b31d8be8e8577a34]*/ +/*[clinic end generated code: output=c6de11b061529d3e input=e225f892d3eee347]*/ { PyObject *it; batchedobject *bo; @@ -270,6 +271,7 @@ static PyType_Slot batched_slots[] = { {Py_tp_iternext, batched_next}, {Py_tp_alloc, PyType_GenericAlloc}, {Py_tp_new, batched_new}, + {Py_tp_vectorcall, batched_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -295,6 +297,7 @@ typedef struct { #define pairwiseobject_CAST(op) ((pairwiseobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.pairwise.__new__ as pairwise_new iterable: object @@ -307,7 +310,7 @@ Return an iterator of overlapping pairs taken from the input iterator. static PyObject * pairwise_new_impl(PyTypeObject *type, PyObject *iterable) -/*[clinic end generated code: output=9f0267062d384456 input=6e7c3cddb431a8d6]*/ +/*[clinic end generated code: output=9f0267062d384456 input=32624f8712dcf0a3]*/ { PyObject *it; pairwiseobject *po; @@ -419,6 +422,7 @@ static PyType_Slot pairwise_slots[] = { {Py_tp_iternext, pairwise_next}, {Py_tp_alloc, PyType_GenericAlloc}, {Py_tp_new, pairwise_new}, + {Py_tp_vectorcall, pairwise_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -451,6 +455,7 @@ static PyObject *_grouper_create(groupbyobject *, PyObject *); /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.groupby.__new__ @@ -466,7 +471,7 @@ make an iterator that returns consecutive keys and groups from the iterable static PyObject * itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc) -/*[clinic end generated code: output=cbb1ae3a90fd4141 input=9f89fe625b20ef1a]*/ +/*[clinic end generated code: output=cbb1ae3a90fd4141 input=83700c6814fea634]*/ { groupbyobject *gbo; @@ -594,6 +599,7 @@ static PyType_Slot groupby_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, groupby_next}, {Py_tp_new, itertools_groupby}, + {Py_tp_vectorcall, itertools_groupby_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -884,6 +890,7 @@ teedataobject_dealloc(PyObject *op) } /*[clinic input] +@vectorcall @classmethod itertools.teedataobject.__new__ iterable as it: object @@ -896,7 +903,7 @@ Data container common to multiple tee objects. static PyObject * itertools_teedataobject_impl(PyTypeObject *type, PyObject *it, PyObject *values, PyObject *next) -/*[clinic end generated code: output=3343ceb07e08df5e input=be60f2fabd2b72ba]*/ +/*[clinic end generated code: output=3343ceb07e08df5e input=02647670a03714c0]*/ { teedataobject *tdo; Py_ssize_t i, len; @@ -944,6 +951,7 @@ static PyType_Slot teedataobject_slots[] = { {Py_tp_traverse, teedataobject_traverse}, {Py_tp_clear, teedataobject_clear}, {Py_tp_new, itertools_teedataobject}, + {Py_tp_vectorcall, itertools_teedataobject_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1221,6 +1229,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.cycle.__new__ iterable: object @@ -1230,7 +1239,7 @@ Return elements from the iterable until it is exhausted. Then repeat the sequenc static PyObject * itertools_cycle_impl(PyTypeObject *type, PyObject *iterable) -/*[clinic end generated code: output=f60e5ec17a45b35c input=ead392f4aac7afd8]*/ +/*[clinic end generated code: output=f60e5ec17a45b35c input=183a6615ee9f1440]*/ { PyObject *it; PyObject *saved; @@ -1329,6 +1338,7 @@ static PyType_Slot cycle_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, cycle_next}, {Py_tp_new, itertools_cycle}, + {Py_tp_vectorcall, itertools_cycle_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1354,6 +1364,7 @@ typedef struct { #define dropwhileobject_CAST(op) ((dropwhileobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.dropwhile.__new__ predicate as func: object @@ -1366,7 +1377,7 @@ Afterwards, return every element until the iterable is exhausted. static PyObject * itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=92f9d0d89af149e4 input=d39737147c9f0a26]*/ +/*[clinic end generated code: output=92f9d0d89af149e4 input=38ed294cc91d3aa8]*/ { PyObject *it; dropwhileobject *lz; @@ -1453,6 +1464,7 @@ static PyType_Slot dropwhile_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, dropwhile_next}, {Py_tp_new, itertools_dropwhile}, + {Py_tp_vectorcall, itertools_dropwhile_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1479,6 +1491,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.takewhile.__new__ predicate as func: object @@ -1489,7 +1502,7 @@ Return successive entries from an iterable as long as the predicate evaluates to static PyObject * itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=bb179ea7864e2ef6 input=61e42255dd0a7657]*/ +/*[clinic end generated code: output=bb179ea7864e2ef6 input=168fa0aa716792be]*/ { PyObject *it; takewhileobject *lz; @@ -1572,6 +1585,7 @@ static PyType_Slot takewhile_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, takewhile_next}, {Py_tp_new, itertools_takewhile}, + {Py_tp_vectorcall, itertools_takewhile_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1789,6 +1803,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.starmap.__new__ function as func: object @@ -1799,7 +1814,7 @@ Return an iterator whose values are returned from the function evaluated with an static PyObject * itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=79eeb81d452c6e8d input=8c9068da0692d6d2]*/ +/*[clinic end generated code: output=79eeb81d452c6e8d input=a7986310dcdb9694]*/ { PyObject *it; starmapobject *lz; @@ -1874,6 +1889,7 @@ static PyType_Slot starmap_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, starmap_next}, {Py_tp_new, itertools_starmap}, + {Py_tp_vectorcall, itertools_starmap_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2339,6 +2355,7 @@ struct combinationsobject { #define combinationsobject_CAST(op) ((combinationsobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.combinations.__new__ iterable: object @@ -2351,7 +2368,7 @@ combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3) static PyObject * itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) -/*[clinic end generated code: output=87a689b39c40039c input=a32f07a15cfa4676]*/ +/*[clinic end generated code: output=87a689b39c40039c input=119eb0d9196abfe3]*/ { combinationsobject *co; Py_ssize_t n; @@ -2531,6 +2548,7 @@ static PyType_Slot combinations_slots[] = { {Py_tp_iternext, combinations_next}, {Py_tp_methods, combinations_methods}, {Py_tp_new, itertools_combinations}, + {Py_tp_vectorcall, itertools_combinations_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2586,6 +2604,7 @@ struct cwrobject { /*[clinic input] @permit_long_summary @permit_long_docstring_body +@vectorcall @classmethod itertools.combinations_with_replacement.__new__ iterable: object @@ -2599,7 +2618,7 @@ static PyObject * itertools_combinations_with_replacement_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) -/*[clinic end generated code: output=48b26856d4e659ca input=828696750169e84f]*/ +/*[clinic end generated code: output=48b26856d4e659ca input=8bd0382a7253de95]*/ { cwrobject *co; Py_ssize_t n; @@ -2773,6 +2792,7 @@ static PyType_Slot cwr_slots[] = { {Py_tp_iternext, cwr_next}, {Py_tp_methods, cwr_methods}, {Py_tp_new, itertools_combinations_with_replacement}, + {Py_tp_vectorcall, itertools_combinations_with_replacement_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2827,6 +2847,7 @@ struct permutationsobject { #define permutationsobject_CAST(op) ((permutationsobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.permutations.__new__ iterable: object @@ -2839,7 +2860,7 @@ permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1) static PyObject * itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, PyObject *robj) -/*[clinic end generated code: output=296a72fa76d620ea input=57d0170a4ac0ec7a]*/ +/*[clinic end generated code: output=296a72fa76d620ea input=3679c9c6e757d17d]*/ { permutationsobject *po; Py_ssize_t n; @@ -3049,6 +3070,7 @@ static PyType_Slot permutations_slots[] = { {Py_tp_iternext, permutations_next}, {Py_tp_methods, permuations_methods}, {Py_tp_new, itertools_permutations}, + {Py_tp_vectorcall, itertools_permutations_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3076,6 +3098,7 @@ struct accumulateobject { #define accumulateobject_CAST(op) ((accumulateobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.accumulate.__new__ iterable: object @@ -3088,7 +3111,7 @@ Return series of accumulated sums (or other binary function results). static PyObject * itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, PyObject *binop, PyObject *initial) -/*[clinic end generated code: output=66da2650627128f8 input=c4ce20ac59bf7ffd]*/ +/*[clinic end generated code: output=66da2650627128f8 input=9c7edffb7110edc9]*/ { PyObject *it; accumulateobject *lz; @@ -3188,6 +3211,7 @@ static PyType_Slot accumulate_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, accumulate_next}, {Py_tp_new, itertools_accumulate}, + {Py_tp_vectorcall, itertools_accumulate_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3219,6 +3243,7 @@ typedef struct { #define compressobject_CAST(op) ((compressobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.compress.__new__ data as seq1: object @@ -3231,7 +3256,7 @@ to choose the data elements. static PyObject * itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2) -/*[clinic end generated code: output=7e67157212ed09e0 input=32ca4347dbc46749]*/ +/*[clinic end generated code: output=7e67157212ed09e0 input=a886cf13635c9780]*/ { PyObject *data=NULL, *selectors=NULL; compressobject *lz; @@ -3324,6 +3349,7 @@ static PyType_Slot compress_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, compress_next}, {Py_tp_new, itertools_compress}, + {Py_tp_vectorcall, itertools_compress_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3348,6 +3374,7 @@ typedef struct { #define filterfalseobject_CAST(op) ((filterfalseobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.filterfalse.__new__ function as func: object @@ -3360,7 +3387,7 @@ If function is None, return the items that are false. static PyObject * itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq) -/*[clinic end generated code: output=55f87eab9fc0484e input=2d684a2c66f99cde]*/ +/*[clinic end generated code: output=55f87eab9fc0484e input=a80f7d973f6efc81]*/ { PyObject *it; filterfalseobject *lz; @@ -3447,6 +3474,7 @@ static PyType_Slot filterfalse_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, filterfalse_next}, {Py_tp_new, itertools_filterfalse}, + {Py_tp_vectorcall, itertools_filterfalse_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3490,6 +3518,7 @@ slow_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float. /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.count.__new__ start as long_cnt: object(c_default="NULL") = 0 @@ -3507,7 +3536,7 @@ Equivalent to: static PyObject * itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, PyObject *long_step) -/*[clinic end generated code: output=09a9250aebd00b1c input=91e4b12c0e88b9f4]*/ +/*[clinic end generated code: output=09a9250aebd00b1c input=b8a57119ba1cdd54]*/ { countobject *lz; int fast_mode; @@ -3695,6 +3724,7 @@ static PyType_Slot count_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, count_next}, {Py_tp_new, itertools_count}, + {Py_tp_vectorcall, itertools_count_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, };