From 58a3cc972f81798bc0abb30b68dce799c464ed52 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 21:54:25 +0800 Subject: [PATCH 1/8] add @vectorcall to Argument Clinic --- Modules/clinic/itertoolsmodule.c.h | 577 +++++++++++++++++++++++++++-- Modules/itertoolsmodule.c | 64 +++- 2 files changed, 595 insertions(+), 46 deletions(-) diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index d6365b46f04b26..0b7e341080af6e 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" @@ -309,6 +429,31 @@ itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +itertools__tee_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__tee); + /* 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("_tee", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("_tee", nargs, 1, 1)) { + goto exit; + } + iterable = args[0]; + return_value = itertools__tee_impl(_PyType_CAST(type), iterable); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_tee__doc__, "tee($module, iterable, n=2, /)\n" "--\n" @@ -389,6 +534,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 +593,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 +652,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 +711,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 +803,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 +836,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 +868,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 +948,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 +981,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 +1013,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 +1092,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 +1125,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 +1146,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 +1210,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 +1243,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 +1275,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 +1327,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 +1360,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 +1376,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 +1444,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 +1489,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 +1522,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 +1548,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 +1603,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=4b55088ac24582b7 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=b098887972cb9f43 input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 636b28fa06ef2e..4db42bdf0091db 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}, }; @@ -1094,6 +1102,7 @@ tee_fromiterable(itertools_state *state, PyObject *iterable) } /*[clinic input] +@vectorcall @classmethod itertools._tee.__new__ iterable: object @@ -1103,7 +1112,7 @@ Iterator wrapped to make it copyable. static PyObject * itertools__tee_impl(PyTypeObject *type, PyObject *iterable) -/*[clinic end generated code: output=b02d3fd26c810c3f input=adc0779d2afe37a2]*/ +/*[clinic end generated code: output=b02d3fd26c810c3f input=3aa5305c0301ddda]*/ { itertools_state *state = get_module_state_by_cls(type); return tee_fromiterable(state, iterable); @@ -1149,6 +1158,7 @@ static PyType_Slot tee_slots[] = { {Py_tp_methods, tee_methods}, {Py_tp_members, tee_members}, {Py_tp_new, itertools__tee}, + {Py_tp_vectorcall, itertools__tee_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -1221,6 +1231,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.cycle.__new__ iterable: object @@ -1230,7 +1241,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 +1340,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 +1366,7 @@ typedef struct { #define dropwhileobject_CAST(op) ((dropwhileobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.dropwhile.__new__ predicate as func: object @@ -1366,7 +1379,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 +1466,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 +1493,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.takewhile.__new__ predicate as func: object @@ -1489,7 +1504,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 +1587,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 +1805,7 @@ typedef struct { /*[clinic input] @permit_long_summary +@vectorcall @classmethod itertools.starmap.__new__ function as func: object @@ -1799,7 +1816,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 +1891,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 +2357,7 @@ struct combinationsobject { #define combinationsobject_CAST(op) ((combinationsobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.combinations.__new__ iterable: object @@ -2351,7 +2370,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 +2550,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 +2606,7 @@ struct cwrobject { /*[clinic input] @permit_long_summary @permit_long_docstring_body +@vectorcall @classmethod itertools.combinations_with_replacement.__new__ iterable: object @@ -2599,7 +2620,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 +2794,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 +2849,7 @@ struct permutationsobject { #define permutationsobject_CAST(op) ((permutationsobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.permutations.__new__ iterable: object @@ -2839,7 +2862,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 +3072,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 +3100,7 @@ struct accumulateobject { #define accumulateobject_CAST(op) ((accumulateobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.accumulate.__new__ iterable: object @@ -3088,7 +3113,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 +3213,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 +3245,7 @@ typedef struct { #define compressobject_CAST(op) ((compressobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.compress.__new__ data as seq1: object @@ -3231,7 +3258,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 +3351,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 +3376,7 @@ typedef struct { #define filterfalseobject_CAST(op) ((filterfalseobject *)(op)) /*[clinic input] +@vectorcall @classmethod itertools.filterfalse.__new__ function as func: object @@ -3360,7 +3389,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 +3476,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 +3520,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 +3538,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 +3726,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}, }; From 8bdab82ec4fd676b68c0bb860c2db026eb7195e5 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 23:09:16 +0800 Subject: [PATCH 2/8] replace hand-written constructors to AC + vectorcall --- Modules/clinic/itertoolsmodule.c.h | 404 ++++++++++++++++++++++++++++- Modules/itertoolsmodule.c | 180 +++++++------ 2 files changed, 499 insertions(+), 85 deletions(-) diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index 0b7e341080af6e..b0292b62d39b3e 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -9,6 +9,7 @@ preserve #include "pycore_abstract.h" // _PyNumber_Index() #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() +#include "pycore_tuple.h" // _PyTuple_ITEMS() PyDoc_STRVAR(batched_new__doc__, "batched(iterable, n, *, strict=False)\n" @@ -679,6 +680,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" @@ -738,6 +815,54 @@ itertools_starmap_vectorcall(PyObject *type, PyObject *const *args, return return_value; } +static PyObject * +chain_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length); + +static PyObject * +chain_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = clinic_state()->chain_type; + PyObject * const *iterables; + Py_ssize_t iterables_length; + + if ((type == base_tp || type->tp_init == base_tp->tp_init) && + !_PyArg_NoKeywords("chain", kwargs)) { + goto exit; + } + iterables = _PyTuple_ITEMS(args); + iterables_length = PyTuple_GET_SIZE(args); + return_value = chain_new_impl(type, iterables, iterables_length); + +exit: + return return_value; +} + +static PyObject * +chain_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject * const *iterables; + Py_ssize_t iterables_length; + + assert(_PyType_CAST(type)->tp_new == chain_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("chain", kwnames)) { + goto exit; + } + iterables = args; + iterables_length = nargs; + return_value = chain_new_impl(_PyType_CAST(type), iterables, iterables_length); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_chain_from_iterable__doc__, "from_iterable($type, iterable, /)\n" "--\n" @@ -775,6 +900,102 @@ chain_next(PyObject *self) return return_value; } +static PyObject * +product_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, Py_ssize_t repeat); + +static PyObject * +product_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 1 + 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(repeat), }, + }; + #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[] = {"repeat", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "product", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject * const *iterables; + Py_ssize_t iterables_length; + Py_ssize_t repeat = 1; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = _PyNumber_Index(fastargs[0]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + repeat = ival; + } +skip_optional_kwonly: + iterables = args; + iterables_length = nargs; + return_value = product_new_impl(type, iterables, iterables_length, repeat); + +exit: + return return_value; +} + +static PyObject * +product_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return product_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +product_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == product_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 product_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + static PyObject * product_next_impl(productobject *self); @@ -1589,6 +1810,187 @@ 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_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, PyObject *fillvalue); + +static PyObject * +zip_longest_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 1 + 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(fillvalue), }, + }; + #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[] = {"fillvalue", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "zip_longest", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject * const *iterables; + Py_ssize_t iterables_length; + PyObject *fillvalue = Py_None; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + fillvalue = fastargs[0]; +skip_optional_kwonly: + iterables = args; + iterables_length = nargs; + return_value = zip_longest_new_impl(type, iterables, iterables_length, fillvalue); + +exit: + return return_value; +} + +static PyObject * +zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return zip_longest_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +zip_longest_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == zip_longest_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 zip_longest_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + static PyObject * zip_longest_next_impl(ziplongestobject *self); @@ -1603,4 +2005,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=b098887972cb9f43 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f1ee11f3d6be1e3d input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 4db42bdf0091db..b6835e2d99da32 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)) @@ -1614,25 +1616,27 @@ 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; 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; - - numargs = PyTuple_Size(args); if (numargs == 2) { if (a1 != Py_None) { stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError); @@ -1682,7 +1686,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } /* Get iterator. */ - it = PyObject_GetIter(seq); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -1780,6 +1784,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}, }; @@ -1931,22 +1936,28 @@ chain_new_internal(PyTypeObject *type, PyObject *source) return (PyObject *)lz; } +/*[clinic input] +@vectorcall +@classmethod +itertools.chain.__new__ as chain_new + *iterables: array +[clinic start generated code]*/ + static PyObject * -chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +chain_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length) +/*[clinic end generated code: output=ca3d5f6d6ddedec4 input=0346ceca117a3021]*/ { - PyObject *source; - - itertools_state *state = find_state_by_type(type); - PyTypeObject *chain_type = state->chain_type; - if ((type == chain_type || type->tp_init == chain_type->tp_init) && - !_PyArg_NoKeywords("chain", kwds)) + PyObject *source = PyTuple_FromArray(iterables, iterables_length); + if (source == NULL) { return NULL; - - source = PyObject_GetIter(args); - if (source == NULL) + } + PyObject *it = PyObject_GetIter(source); + Py_DECREF(source); + if (it == NULL) { return NULL; - - return chain_new_internal(type, source); + } + return chain_new_internal(type, it); } /*[clinic input] @@ -2066,6 +2077,7 @@ static PyType_Slot chain_slots[] = { {Py_tp_iternext, chain_next}, {Py_tp_methods, chain_methods}, {Py_tp_new, chain_new}, + {Py_tp_vectorcall, chain_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2091,38 +2103,35 @@ struct productobject { #define productobject_CAST(op) ((productobject *)(op)) +/*[clinic input] +@vectorcall +@classmethod +itertools.product.__new__ as product_new + *iterables: array + repeat: Py_ssize_t = 1 +[clinic start generated code]*/ + static PyObject * -product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +product_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, Py_ssize_t repeat) +/*[clinic end generated code: output=b0881d40e6be4c00 input=2d0325c612c678a1]*/ { productobject *lz; - Py_ssize_t nargs, npools, repeat=1; + Py_ssize_t nargs, npools; PyObject *pools = NULL; Py_ssize_t *indices = NULL; Py_ssize_t i; - if (kwds != NULL) { - char *kwlist[] = {"repeat", 0}; - PyObject *tmpargs = PyTuple_New(0); - if (tmpargs == NULL) - return NULL; - if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", - kwlist, &repeat)) { - Py_DECREF(tmpargs); - return NULL; - } - Py_DECREF(tmpargs); - if (repeat < 0) { - PyErr_SetString(PyExc_ValueError, - "repeat argument cannot be negative"); - return NULL; - } + if (repeat < 0) { + PyErr_SetString(PyExc_ValueError, + "repeat argument cannot be negative"); + return NULL; } - assert(PyTuple_CheckExact(args)); if (repeat == 0) { nargs = 0; } else { - nargs = PyTuple_GET_SIZE(args); + nargs = iterables_length; if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) { PyErr_SetString(PyExc_OverflowError, "repeat argument too large"); return NULL; @@ -2141,7 +2150,7 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) goto error; for (i=0; i < nargs ; ++i) { - PyObject *item = PyTuple_GET_ITEM(args, i); + PyObject *item = iterables[i]; PyObject *pool = PySequence_Tuple(item); if (pool == NULL) goto error; @@ -2330,6 +2339,7 @@ static PyType_Slot product_slots[] = { {Py_tp_iternext, product_next}, {Py_tp_methods, product_methods}, {Py_tp_new, product_new}, + {Py_tp_vectorcall, product_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3750,23 +3760,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) @@ -3857,6 +3875,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}, }; @@ -3883,40 +3902,32 @@ struct ziplongestobject { #define ziplongestobject_CAST(op) ((ziplongestobject *)(op)) +/*[clinic input] +@vectorcall +@classmethod +itertools.zip_longest.__new__ as zip_longest_new + *iterables: array + fillvalue: object = None +[clinic start generated code]*/ + static PyObject * -zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +zip_longest_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, PyObject *fillvalue) +/*[clinic end generated code: output=9f45c950feb8e18d input=2c4cc9910ee8dab7]*/ { ziplongestobject *lz; Py_ssize_t i; PyObject *ittuple; /* tuple of iterators */ PyObject *result; - PyObject *fillvalue = Py_None; - Py_ssize_t tuplesize; - - if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { - fillvalue = NULL; - if (PyDict_GET_SIZE(kwds) == 1) { - fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue)); - } - if (fillvalue == NULL) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "zip_longest() got an unexpected keyword argument"); - } - return NULL; - } - } + Py_ssize_t tuplesize = iterables_length; - /* args must be a tuple */ - assert(PyTuple_Check(args)); - tuplesize = PyTuple_GET_SIZE(args); /* obtain iterators */ ittuple = PyTuple_New(tuplesize); if (ittuple == NULL) return NULL; for (i=0; i < tuplesize; i++) { - PyObject *item = PyTuple_GET_ITEM(args, i); + PyObject *item = iterables[i]; PyObject *it = PyObject_GetIter(item); if (it == NULL) { Py_DECREF(ittuple); @@ -4073,6 +4084,7 @@ static PyType_Slot ziplongest_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, zip_longest_next}, {Py_tp_new, zip_longest_new}, + {Py_tp_vectorcall, zip_longest_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; From 19aeef319c43bcefe4c141ea389f8c44a91feca1 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Wed, 25 Nov 2026 06:52:08 +0800 Subject: [PATCH 3/8] fix test which uses assert itertools.product() with old error message --- Lib/test/test_call.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py index 8d8b7f9bbc38fb..c6be51814cd8f7 100644 --- a/Lib/test/test_call.py +++ b/Lib/test/test_call.py @@ -203,7 +203,7 @@ def test_varargs13_kw(self): self.assertRaisesRegex(TypeError, msg, classmethod, func=id) def test_varargs14_kw(self): - msg = r"^product\(\) takes at most 1 keyword argument \(2 given\)$" + msg = r"^product\(\) got an unexpected keyword argument 'foo'$" self.assertRaisesRegex(TypeError, msg, itertools.product, 0, repeat=1, foo=2) From 83f458fda0aae09ca0e9314c5a1267402067722a Mon Sep 17 00:00:00 2001 From: Tan Long Date: Thu, 26 Nov 2026 10:57:08 +0800 Subject: [PATCH 4/8] Revert "replace hand-written constructors to AC + vectorcall" This reverts commit 8bdab82ec4fd676b68c0bb860c2db026eb7195e5. --- Modules/clinic/itertoolsmodule.c.h | 404 +---------------------------- Modules/itertoolsmodule.c | 180 ++++++------- 2 files changed, 85 insertions(+), 499 deletions(-) diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index b0292b62d39b3e..0b7e341080af6e 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -9,7 +9,6 @@ preserve #include "pycore_abstract.h" // _PyNumber_Index() #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() -#include "pycore_tuple.h" // _PyTuple_ITEMS() PyDoc_STRVAR(batched_new__doc__, "batched(iterable, n, *, strict=False)\n" @@ -680,82 +679,6 @@ 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" @@ -815,54 +738,6 @@ itertools_starmap_vectorcall(PyObject *type, PyObject *const *args, return return_value; } -static PyObject * -chain_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length); - -static PyObject * -chain_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - PyObject *return_value = NULL; - PyTypeObject *base_tp = clinic_state()->chain_type; - PyObject * const *iterables; - Py_ssize_t iterables_length; - - if ((type == base_tp || type->tp_init == base_tp->tp_init) && - !_PyArg_NoKeywords("chain", kwargs)) { - goto exit; - } - iterables = _PyTuple_ITEMS(args); - iterables_length = PyTuple_GET_SIZE(args); - return_value = chain_new_impl(type, iterables, iterables_length); - -exit: - return return_value; -} - -static PyObject * -chain_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) -{ - PyObject *return_value = NULL; - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - PyObject * const *iterables; - Py_ssize_t iterables_length; - - assert(_PyType_CAST(type)->tp_new == chain_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("chain", kwnames)) { - goto exit; - } - iterables = args; - iterables_length = nargs; - return_value = chain_new_impl(_PyType_CAST(type), iterables, iterables_length); - -exit: - return return_value; -} - PyDoc_STRVAR(itertools_chain_from_iterable__doc__, "from_iterable($type, iterable, /)\n" "--\n" @@ -900,102 +775,6 @@ chain_next(PyObject *self) return return_value; } -static PyObject * -product_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, Py_ssize_t repeat); - -static PyObject * -product_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 1 - 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(repeat), }, - }; - #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[] = {"repeat", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "product", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - PyObject * const *fastargs; - Py_ssize_t noptargs = 0 + nkw - 0; - PyObject * const *iterables; - Py_ssize_t iterables_length; - Py_ssize_t repeat = 1; - - fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, - /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); - if (!fastargs) { - goto exit; - } - if (!noptargs) { - goto skip_optional_kwonly; - } - { - Py_ssize_t ival = -1; - PyObject *iobj = _PyNumber_Index(fastargs[0]); - if (iobj != NULL) { - ival = PyLong_AsSsize_t(iobj); - Py_DECREF(iobj); - } - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - repeat = ival; - } -skip_optional_kwonly: - iterables = args; - iterables_length = nargs; - return_value = product_new_impl(type, iterables, iterables_length, repeat); - -exit: - return return_value; -} - -static PyObject * -product_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - return product_new_helper(type, _PyTuple_CAST(args)->ob_item, - PyTuple_GET_SIZE(args), - kwargs ? PyDict_GET_SIZE(kwargs) : 0, - kwargs, NULL); -} - -static PyObject * -product_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) -{ - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - - assert(_PyType_CAST(type)->tp_new == product_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 product_new_helper(_PyType_CAST(type), args, nargs, - kwnames ? PyTuple_GET_SIZE(kwnames) : 0, - NULL, kwnames); -} - static PyObject * product_next_impl(productobject *self); @@ -1810,187 +1589,6 @@ 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_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, PyObject *fillvalue); - -static PyObject * -zip_longest_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 1 - 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(fillvalue), }, - }; - #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[] = {"fillvalue", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "zip_longest", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - PyObject * const *fastargs; - Py_ssize_t noptargs = 0 + nkw - 0; - PyObject * const *iterables; - Py_ssize_t iterables_length; - PyObject *fillvalue = Py_None; - - fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, - /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); - if (!fastargs) { - goto exit; - } - if (!noptargs) { - goto skip_optional_kwonly; - } - fillvalue = fastargs[0]; -skip_optional_kwonly: - iterables = args; - iterables_length = nargs; - return_value = zip_longest_new_impl(type, iterables, iterables_length, fillvalue); - -exit: - return return_value; -} - -static PyObject * -zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - return zip_longest_new_helper(type, _PyTuple_CAST(args)->ob_item, - PyTuple_GET_SIZE(args), - kwargs ? PyDict_GET_SIZE(kwargs) : 0, - kwargs, NULL); -} - -static PyObject * -zip_longest_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) -{ - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - - assert(_PyType_CAST(type)->tp_new == zip_longest_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 zip_longest_new_helper(_PyType_CAST(type), args, nargs, - kwnames ? PyTuple_GET_SIZE(kwnames) : 0, - NULL, kwnames); -} - static PyObject * zip_longest_next_impl(ziplongestobject *self); @@ -2005,4 +1603,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=f1ee11f3d6be1e3d input=a9049054013a1b77]*/ +/*[clinic end generated code: output=b098887972cb9f43 input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index b6835e2d99da32..4db42bdf0091db 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -77,7 +77,6 @@ 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" @@ -97,10 +96,9 @@ 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=f671f9aa97349d64]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1086fb2d308d75d2]*/ #define clinic_state() (find_state_by_type(type)) #define clinic_state_by_cls() (get_module_state_by_cls(base_tp)) @@ -1616,27 +1614,25 @@ 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_impl(PyTypeObject *type, PyObject *iterable, PyObject *a1, - PyObject *a2, PyObject *a3) -/*[clinic end generated code: output=7fa28024c01231db input=aac6a09144cead64]*/ +islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *it; + PyObject *seq; Py_ssize_t start=0, stop=-1, step=1; - Py_ssize_t numargs = (a2 == NULL) ? 2 : ((a3 == NULL) ? 3 : 4); + PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; + Py_ssize_t numargs; 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); @@ -1686,7 +1682,7 @@ islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *a1, } /* Get iterator. */ - it = PyObject_GetIter(iterable); + it = PyObject_GetIter(seq); if (it == NULL) return NULL; @@ -1784,7 +1780,6 @@ 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}, }; @@ -1936,28 +1931,22 @@ chain_new_internal(PyTypeObject *type, PyObject *source) return (PyObject *)lz; } -/*[clinic input] -@vectorcall -@classmethod -itertools.chain.__new__ as chain_new - *iterables: array -[clinic start generated code]*/ - static PyObject * -chain_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length) -/*[clinic end generated code: output=ca3d5f6d6ddedec4 input=0346ceca117a3021]*/ +chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *source = PyTuple_FromArray(iterables, iterables_length); - if (source == NULL) { + PyObject *source; + + itertools_state *state = find_state_by_type(type); + PyTypeObject *chain_type = state->chain_type; + if ((type == chain_type || type->tp_init == chain_type->tp_init) && + !_PyArg_NoKeywords("chain", kwds)) return NULL; - } - PyObject *it = PyObject_GetIter(source); - Py_DECREF(source); - if (it == NULL) { + + source = PyObject_GetIter(args); + if (source == NULL) return NULL; - } - return chain_new_internal(type, it); + + return chain_new_internal(type, source); } /*[clinic input] @@ -2077,7 +2066,6 @@ static PyType_Slot chain_slots[] = { {Py_tp_iternext, chain_next}, {Py_tp_methods, chain_methods}, {Py_tp_new, chain_new}, - {Py_tp_vectorcall, chain_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2103,35 +2091,38 @@ struct productobject { #define productobject_CAST(op) ((productobject *)(op)) -/*[clinic input] -@vectorcall -@classmethod -itertools.product.__new__ as product_new - *iterables: array - repeat: Py_ssize_t = 1 -[clinic start generated code]*/ - static PyObject * -product_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, Py_ssize_t repeat) -/*[clinic end generated code: output=b0881d40e6be4c00 input=2d0325c612c678a1]*/ +product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { productobject *lz; - Py_ssize_t nargs, npools; + Py_ssize_t nargs, npools, repeat=1; PyObject *pools = NULL; Py_ssize_t *indices = NULL; Py_ssize_t i; - if (repeat < 0) { - PyErr_SetString(PyExc_ValueError, - "repeat argument cannot be negative"); - return NULL; + if (kwds != NULL) { + char *kwlist[] = {"repeat", 0}; + PyObject *tmpargs = PyTuple_New(0); + if (tmpargs == NULL) + return NULL; + if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", + kwlist, &repeat)) { + Py_DECREF(tmpargs); + return NULL; + } + Py_DECREF(tmpargs); + if (repeat < 0) { + PyErr_SetString(PyExc_ValueError, + "repeat argument cannot be negative"); + return NULL; + } } + assert(PyTuple_CheckExact(args)); if (repeat == 0) { nargs = 0; } else { - nargs = iterables_length; + nargs = PyTuple_GET_SIZE(args); if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) { PyErr_SetString(PyExc_OverflowError, "repeat argument too large"); return NULL; @@ -2150,7 +2141,7 @@ product_new_impl(PyTypeObject *type, PyObject * const *iterables, goto error; for (i=0; i < nargs ; ++i) { - PyObject *item = iterables[i]; + PyObject *item = PyTuple_GET_ITEM(args, i); PyObject *pool = PySequence_Tuple(item); if (pool == NULL) goto error; @@ -2339,7 +2330,6 @@ static PyType_Slot product_slots[] = { {Py_tp_iternext, product_next}, {Py_tp_methods, product_methods}, {Py_tp_new, product_new}, - {Py_tp_vectorcall, product_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3760,31 +3750,23 @@ 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_impl(PyTypeObject *type, PyObject *element, PyObject *times) -/*[clinic end generated code: output=4ef9bd3593335622 input=c0eafe81f60ae9e6]*/ +repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { repeatobject *ro; - 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; - } - } + 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; ro = (repeatobject *)type->tp_alloc(type, 0); if (ro == NULL) @@ -3875,7 +3857,6 @@ 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}, }; @@ -3902,32 +3883,40 @@ struct ziplongestobject { #define ziplongestobject_CAST(op) ((ziplongestobject *)(op)) -/*[clinic input] -@vectorcall -@classmethod -itertools.zip_longest.__new__ as zip_longest_new - *iterables: array - fillvalue: object = None -[clinic start generated code]*/ - static PyObject * -zip_longest_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, PyObject *fillvalue) -/*[clinic end generated code: output=9f45c950feb8e18d input=2c4cc9910ee8dab7]*/ +zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { ziplongestobject *lz; Py_ssize_t i; PyObject *ittuple; /* tuple of iterators */ PyObject *result; - Py_ssize_t tuplesize = iterables_length; + PyObject *fillvalue = Py_None; + Py_ssize_t tuplesize; + + if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { + fillvalue = NULL; + if (PyDict_GET_SIZE(kwds) == 1) { + fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue)); + } + if (fillvalue == NULL) { + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "zip_longest() got an unexpected keyword argument"); + } + return NULL; + } + } + /* args must be a tuple */ + assert(PyTuple_Check(args)); + tuplesize = PyTuple_GET_SIZE(args); /* obtain iterators */ ittuple = PyTuple_New(tuplesize); if (ittuple == NULL) return NULL; for (i=0; i < tuplesize; i++) { - PyObject *item = iterables[i]; + PyObject *item = PyTuple_GET_ITEM(args, i); PyObject *it = PyObject_GetIter(item); if (it == NULL) { Py_DECREF(ittuple); @@ -4084,7 +4073,6 @@ static PyType_Slot ziplongest_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, zip_longest_next}, {Py_tp_new, zip_longest_new}, - {Py_tp_vectorcall, zip_longest_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; From 92c328780d1ad581f5e470474a7f8583fd65b586 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Thu, 26 Nov 2026 10:57:19 +0800 Subject: [PATCH 5/8] Revert "fix test which uses assert itertools.product() with old error message" This reverts commit 19aeef319c43bcefe4c141ea389f8c44a91feca1. --- Lib/test/test_call.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py index c6be51814cd8f7..8d8b7f9bbc38fb 100644 --- a/Lib/test/test_call.py +++ b/Lib/test/test_call.py @@ -203,7 +203,7 @@ def test_varargs13_kw(self): self.assertRaisesRegex(TypeError, msg, classmethod, func=id) def test_varargs14_kw(self): - msg = r"^product\(\) got an unexpected keyword argument 'foo'$" + msg = r"^product\(\) takes at most 1 keyword argument \(2 given\)$" self.assertRaisesRegex(TypeError, msg, itertools.product, 0, repeat=1, foo=2) From e5a22a98f6c3fe15f16bc5cb32d84dd1cac1dac2 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 23:09:16 +0800 Subject: [PATCH 6/8] replace hand-written constructors to AC + vectorcall --- Modules/clinic/itertoolsmodule.c.h | 404 ++++++++++++++++++++++++++++- Modules/itertoolsmodule.c | 180 +++++++------ 2 files changed, 499 insertions(+), 85 deletions(-) diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index 0b7e341080af6e..b0292b62d39b3e 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -9,6 +9,7 @@ preserve #include "pycore_abstract.h" // _PyNumber_Index() #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() +#include "pycore_tuple.h" // _PyTuple_ITEMS() PyDoc_STRVAR(batched_new__doc__, "batched(iterable, n, *, strict=False)\n" @@ -679,6 +680,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" @@ -738,6 +815,54 @@ itertools_starmap_vectorcall(PyObject *type, PyObject *const *args, return return_value; } +static PyObject * +chain_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length); + +static PyObject * +chain_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyTypeObject *base_tp = clinic_state()->chain_type; + PyObject * const *iterables; + Py_ssize_t iterables_length; + + if ((type == base_tp || type->tp_init == base_tp->tp_init) && + !_PyArg_NoKeywords("chain", kwargs)) { + goto exit; + } + iterables = _PyTuple_ITEMS(args); + iterables_length = PyTuple_GET_SIZE(args); + return_value = chain_new_impl(type, iterables, iterables_length); + +exit: + return return_value; +} + +static PyObject * +chain_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject * const *iterables; + Py_ssize_t iterables_length; + + assert(_PyType_CAST(type)->tp_new == chain_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("chain", kwnames)) { + goto exit; + } + iterables = args; + iterables_length = nargs; + return_value = chain_new_impl(_PyType_CAST(type), iterables, iterables_length); + +exit: + return return_value; +} + PyDoc_STRVAR(itertools_chain_from_iterable__doc__, "from_iterable($type, iterable, /)\n" "--\n" @@ -775,6 +900,102 @@ chain_next(PyObject *self) return return_value; } +static PyObject * +product_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, Py_ssize_t repeat); + +static PyObject * +product_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 1 + 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(repeat), }, + }; + #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[] = {"repeat", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "product", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject * const *iterables; + Py_ssize_t iterables_length; + Py_ssize_t repeat = 1; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + { + Py_ssize_t ival = -1; + PyObject *iobj = _PyNumber_Index(fastargs[0]); + if (iobj != NULL) { + ival = PyLong_AsSsize_t(iobj); + Py_DECREF(iobj); + } + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + repeat = ival; + } +skip_optional_kwonly: + iterables = args; + iterables_length = nargs; + return_value = product_new_impl(type, iterables, iterables_length, repeat); + +exit: + return return_value; +} + +static PyObject * +product_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return product_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +product_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == product_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 product_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + static PyObject * product_next_impl(productobject *self); @@ -1589,6 +1810,187 @@ 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_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, PyObject *fillvalue); + +static PyObject * +zip_longest_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 1 + 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(fillvalue), }, + }; + #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[] = {"fillvalue", NULL}; + static _PyArg_Parser _parser = { + .keywords = _keywords, + .fname = "zip_longest", + .kwtuple = KWTUPLE, + }; + #undef KWTUPLE + PyObject *argsbuf[1]; + PyObject * const *fastargs; + Py_ssize_t noptargs = 0 + nkw - 0; + PyObject * const *iterables; + Py_ssize_t iterables_length; + PyObject *fillvalue = Py_None; + + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, + /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); + if (!fastargs) { + goto exit; + } + if (!noptargs) { + goto skip_optional_kwonly; + } + fillvalue = fastargs[0]; +skip_optional_kwonly: + iterables = args; + iterables_length = nargs; + return_value = zip_longest_new_impl(type, iterables, iterables_length, fillvalue); + +exit: + return return_value; +} + +static PyObject * +zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return zip_longest_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +zip_longest_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == zip_longest_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 zip_longest_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + static PyObject * zip_longest_next_impl(ziplongestobject *self); @@ -1603,4 +2005,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=b098887972cb9f43 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f1ee11f3d6be1e3d input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 4db42bdf0091db..b6835e2d99da32 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)) @@ -1614,25 +1616,27 @@ 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; 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; - - numargs = PyTuple_Size(args); if (numargs == 2) { if (a1 != Py_None) { stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError); @@ -1682,7 +1686,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } /* Get iterator. */ - it = PyObject_GetIter(seq); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -1780,6 +1784,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}, }; @@ -1931,22 +1936,28 @@ chain_new_internal(PyTypeObject *type, PyObject *source) return (PyObject *)lz; } +/*[clinic input] +@vectorcall +@classmethod +itertools.chain.__new__ as chain_new + *iterables: array +[clinic start generated code]*/ + static PyObject * -chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +chain_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length) +/*[clinic end generated code: output=ca3d5f6d6ddedec4 input=0346ceca117a3021]*/ { - PyObject *source; - - itertools_state *state = find_state_by_type(type); - PyTypeObject *chain_type = state->chain_type; - if ((type == chain_type || type->tp_init == chain_type->tp_init) && - !_PyArg_NoKeywords("chain", kwds)) + PyObject *source = PyTuple_FromArray(iterables, iterables_length); + if (source == NULL) { return NULL; - - source = PyObject_GetIter(args); - if (source == NULL) + } + PyObject *it = PyObject_GetIter(source); + Py_DECREF(source); + if (it == NULL) { return NULL; - - return chain_new_internal(type, source); + } + return chain_new_internal(type, it); } /*[clinic input] @@ -2066,6 +2077,7 @@ static PyType_Slot chain_slots[] = { {Py_tp_iternext, chain_next}, {Py_tp_methods, chain_methods}, {Py_tp_new, chain_new}, + {Py_tp_vectorcall, chain_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2091,38 +2103,35 @@ struct productobject { #define productobject_CAST(op) ((productobject *)(op)) +/*[clinic input] +@vectorcall +@classmethod +itertools.product.__new__ as product_new + *iterables: array + repeat: Py_ssize_t = 1 +[clinic start generated code]*/ + static PyObject * -product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +product_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, Py_ssize_t repeat) +/*[clinic end generated code: output=b0881d40e6be4c00 input=2d0325c612c678a1]*/ { productobject *lz; - Py_ssize_t nargs, npools, repeat=1; + Py_ssize_t nargs, npools; PyObject *pools = NULL; Py_ssize_t *indices = NULL; Py_ssize_t i; - if (kwds != NULL) { - char *kwlist[] = {"repeat", 0}; - PyObject *tmpargs = PyTuple_New(0); - if (tmpargs == NULL) - return NULL; - if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", - kwlist, &repeat)) { - Py_DECREF(tmpargs); - return NULL; - } - Py_DECREF(tmpargs); - if (repeat < 0) { - PyErr_SetString(PyExc_ValueError, - "repeat argument cannot be negative"); - return NULL; - } + if (repeat < 0) { + PyErr_SetString(PyExc_ValueError, + "repeat argument cannot be negative"); + return NULL; } - assert(PyTuple_CheckExact(args)); if (repeat == 0) { nargs = 0; } else { - nargs = PyTuple_GET_SIZE(args); + nargs = iterables_length; if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) { PyErr_SetString(PyExc_OverflowError, "repeat argument too large"); return NULL; @@ -2141,7 +2150,7 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) goto error; for (i=0; i < nargs ; ++i) { - PyObject *item = PyTuple_GET_ITEM(args, i); + PyObject *item = iterables[i]; PyObject *pool = PySequence_Tuple(item); if (pool == NULL) goto error; @@ -2330,6 +2339,7 @@ static PyType_Slot product_slots[] = { {Py_tp_iternext, product_next}, {Py_tp_methods, product_methods}, {Py_tp_new, product_new}, + {Py_tp_vectorcall, product_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3750,23 +3760,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) @@ -3857,6 +3875,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}, }; @@ -3883,40 +3902,32 @@ struct ziplongestobject { #define ziplongestobject_CAST(op) ((ziplongestobject *)(op)) +/*[clinic input] +@vectorcall +@classmethod +itertools.zip_longest.__new__ as zip_longest_new + *iterables: array + fillvalue: object = None +[clinic start generated code]*/ + static PyObject * -zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +zip_longest_new_impl(PyTypeObject *type, PyObject * const *iterables, + Py_ssize_t iterables_length, PyObject *fillvalue) +/*[clinic end generated code: output=9f45c950feb8e18d input=2c4cc9910ee8dab7]*/ { ziplongestobject *lz; Py_ssize_t i; PyObject *ittuple; /* tuple of iterators */ PyObject *result; - PyObject *fillvalue = Py_None; - Py_ssize_t tuplesize; - - if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { - fillvalue = NULL; - if (PyDict_GET_SIZE(kwds) == 1) { - fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue)); - } - if (fillvalue == NULL) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "zip_longest() got an unexpected keyword argument"); - } - return NULL; - } - } + Py_ssize_t tuplesize = iterables_length; - /* args must be a tuple */ - assert(PyTuple_Check(args)); - tuplesize = PyTuple_GET_SIZE(args); /* obtain iterators */ ittuple = PyTuple_New(tuplesize); if (ittuple == NULL) return NULL; for (i=0; i < tuplesize; i++) { - PyObject *item = PyTuple_GET_ITEM(args, i); + PyObject *item = iterables[i]; PyObject *it = PyObject_GetIter(item); if (it == NULL) { Py_DECREF(ittuple); @@ -4073,6 +4084,7 @@ static PyType_Slot ziplongest_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, zip_longest_next}, {Py_tp_new, zip_longest_new}, + {Py_tp_vectorcall, zip_longest_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; From fb13c5f1850ba31a6f0340b19a8c68295e828dc4 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Thu, 26 Nov 2026 12:51:38 +0800 Subject: [PATCH 7/8] revert @vectrocall of tee --- Modules/clinic/itertoolsmodule.c.h | 27 +-------------------------- Modules/itertoolsmodule.c | 4 +--- 2 files changed, 2 insertions(+), 29 deletions(-) diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index b0292b62d39b3e..c6a13a002c8889 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -430,31 +430,6 @@ itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } -static PyObject * -itertools__tee_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__tee); - /* 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("_tee", kwnames)) { - goto exit; - } - if (!_PyArg_CheckPositional("_tee", nargs, 1, 1)) { - goto exit; - } - iterable = args[0]; - return_value = itertools__tee_impl(_PyType_CAST(type), iterable); - -exit: - return return_value; -} - PyDoc_STRVAR(itertools_tee__doc__, "tee($module, iterable, n=2, /)\n" "--\n" @@ -2005,4 +1980,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=f1ee11f3d6be1e3d input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fb82d159d4b90045 input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index b6835e2d99da32..f93a4cd2ee5d9e 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -1104,7 +1104,6 @@ tee_fromiterable(itertools_state *state, PyObject *iterable) } /*[clinic input] -@vectorcall @classmethod itertools._tee.__new__ iterable: object @@ -1114,7 +1113,7 @@ Iterator wrapped to make it copyable. static PyObject * itertools__tee_impl(PyTypeObject *type, PyObject *iterable) -/*[clinic end generated code: output=b02d3fd26c810c3f input=3aa5305c0301ddda]*/ +/*[clinic end generated code: output=b02d3fd26c810c3f input=adc0779d2afe37a2]*/ { itertools_state *state = get_module_state_by_cls(type); return tee_fromiterable(state, iterable); @@ -1160,7 +1159,6 @@ static PyType_Slot tee_slots[] = { {Py_tp_methods, tee_methods}, {Py_tp_members, tee_members}, {Py_tp_new, itertools__tee}, - {Py_tp_vectorcall, itertools__tee_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; From e4c9852124d7ecc2c35a6cb1c2c69884e9add78b Mon Sep 17 00:00:00 2001 From: Tan Long Date: Thu, 26 Nov 2026 13:05:05 +0800 Subject: [PATCH 8/8] Revert "replace hand-written constructors to AC + vectorcall" This reverts commit e5a22a98f6c3fe15f16bc5cb32d84dd1cac1dac2. --- Modules/clinic/itertoolsmodule.c.h | 404 +---------------------------- Modules/itertoolsmodule.c | 180 ++++++------- 2 files changed, 85 insertions(+), 499 deletions(-) diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index c6a13a002c8889..3fd1b7a2fc7b90 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -9,7 +9,6 @@ preserve #include "pycore_abstract.h" // _PyNumber_Index() #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() -#include "pycore_tuple.h" // _PyTuple_ITEMS() PyDoc_STRVAR(batched_new__doc__, "batched(iterable, n, *, strict=False)\n" @@ -655,82 +654,6 @@ 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" @@ -790,54 +713,6 @@ itertools_starmap_vectorcall(PyObject *type, PyObject *const *args, return return_value; } -static PyObject * -chain_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length); - -static PyObject * -chain_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - PyObject *return_value = NULL; - PyTypeObject *base_tp = clinic_state()->chain_type; - PyObject * const *iterables; - Py_ssize_t iterables_length; - - if ((type == base_tp || type->tp_init == base_tp->tp_init) && - !_PyArg_NoKeywords("chain", kwargs)) { - goto exit; - } - iterables = _PyTuple_ITEMS(args); - iterables_length = PyTuple_GET_SIZE(args); - return_value = chain_new_impl(type, iterables, iterables_length); - -exit: - return return_value; -} - -static PyObject * -chain_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) -{ - PyObject *return_value = NULL; - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - PyObject * const *iterables; - Py_ssize_t iterables_length; - - assert(_PyType_CAST(type)->tp_new == chain_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("chain", kwnames)) { - goto exit; - } - iterables = args; - iterables_length = nargs; - return_value = chain_new_impl(_PyType_CAST(type), iterables, iterables_length); - -exit: - return return_value; -} - PyDoc_STRVAR(itertools_chain_from_iterable__doc__, "from_iterable($type, iterable, /)\n" "--\n" @@ -875,102 +750,6 @@ chain_next(PyObject *self) return return_value; } -static PyObject * -product_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, Py_ssize_t repeat); - -static PyObject * -product_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 1 - 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(repeat), }, - }; - #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[] = {"repeat", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "product", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - PyObject * const *fastargs; - Py_ssize_t noptargs = 0 + nkw - 0; - PyObject * const *iterables; - Py_ssize_t iterables_length; - Py_ssize_t repeat = 1; - - fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, - /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); - if (!fastargs) { - goto exit; - } - if (!noptargs) { - goto skip_optional_kwonly; - } - { - Py_ssize_t ival = -1; - PyObject *iobj = _PyNumber_Index(fastargs[0]); - if (iobj != NULL) { - ival = PyLong_AsSsize_t(iobj); - Py_DECREF(iobj); - } - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - repeat = ival; - } -skip_optional_kwonly: - iterables = args; - iterables_length = nargs; - return_value = product_new_impl(type, iterables, iterables_length, repeat); - -exit: - return return_value; -} - -static PyObject * -product_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - return product_new_helper(type, _PyTuple_CAST(args)->ob_item, - PyTuple_GET_SIZE(args), - kwargs ? PyDict_GET_SIZE(kwargs) : 0, - kwargs, NULL); -} - -static PyObject * -product_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) -{ - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - - assert(_PyType_CAST(type)->tp_new == product_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 product_new_helper(_PyType_CAST(type), args, nargs, - kwnames ? PyTuple_GET_SIZE(kwnames) : 0, - NULL, kwnames); -} - static PyObject * product_next_impl(productobject *self); @@ -1785,187 +1564,6 @@ 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_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, PyObject *fillvalue); - -static PyObject * -zip_longest_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 1 - 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(fillvalue), }, - }; - #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[] = {"fillvalue", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "zip_longest", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - PyObject * const *fastargs; - Py_ssize_t noptargs = 0 + nkw - 0; - PyObject * const *iterables; - Py_ssize_t iterables_length; - PyObject *fillvalue = Py_None; - - fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, - /*minpos*/ 0, /*maxpos*/ 0, /*minkw*/ 0, /*varpos*/ 1, argsbuf); - if (!fastargs) { - goto exit; - } - if (!noptargs) { - goto skip_optional_kwonly; - } - fillvalue = fastargs[0]; -skip_optional_kwonly: - iterables = args; - iterables_length = nargs; - return_value = zip_longest_new_impl(type, iterables, iterables_length, fillvalue); - -exit: - return return_value; -} - -static PyObject * -zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - return zip_longest_new_helper(type, _PyTuple_CAST(args)->ob_item, - PyTuple_GET_SIZE(args), - kwargs ? PyDict_GET_SIZE(kwargs) : 0, - kwargs, NULL); -} - -static PyObject * -zip_longest_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) -{ - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - - assert(_PyType_CAST(type)->tp_new == zip_longest_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 zip_longest_new_helper(_PyType_CAST(type), args, nargs, - kwnames ? PyTuple_GET_SIZE(kwnames) : 0, - NULL, kwnames); -} - static PyObject * zip_longest_next_impl(ziplongestobject *self); @@ -1980,4 +1578,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=fb82d159d4b90045 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=60939958c9c9348b input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index f93a4cd2ee5d9e..d7daad8c303858 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -77,7 +77,6 @@ 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" @@ -97,10 +96,9 @@ 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=f671f9aa97349d64]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1086fb2d308d75d2]*/ #define clinic_state() (find_state_by_type(type)) #define clinic_state_by_cls() (get_module_state_by_cls(base_tp)) @@ -1614,27 +1612,25 @@ 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_impl(PyTypeObject *type, PyObject *iterable, PyObject *a1, - PyObject *a2, PyObject *a3) -/*[clinic end generated code: output=7fa28024c01231db input=aac6a09144cead64]*/ +islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *it; + PyObject *seq; Py_ssize_t start=0, stop=-1, step=1; - Py_ssize_t numargs = (a2 == NULL) ? 2 : ((a3 == NULL) ? 3 : 4); + PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; + Py_ssize_t numargs; 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); @@ -1684,7 +1680,7 @@ islice_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *a1, } /* Get iterator. */ - it = PyObject_GetIter(iterable); + it = PyObject_GetIter(seq); if (it == NULL) return NULL; @@ -1782,7 +1778,6 @@ 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}, }; @@ -1934,28 +1929,22 @@ chain_new_internal(PyTypeObject *type, PyObject *source) return (PyObject *)lz; } -/*[clinic input] -@vectorcall -@classmethod -itertools.chain.__new__ as chain_new - *iterables: array -[clinic start generated code]*/ - static PyObject * -chain_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length) -/*[clinic end generated code: output=ca3d5f6d6ddedec4 input=0346ceca117a3021]*/ +chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *source = PyTuple_FromArray(iterables, iterables_length); - if (source == NULL) { + PyObject *source; + + itertools_state *state = find_state_by_type(type); + PyTypeObject *chain_type = state->chain_type; + if ((type == chain_type || type->tp_init == chain_type->tp_init) && + !_PyArg_NoKeywords("chain", kwds)) return NULL; - } - PyObject *it = PyObject_GetIter(source); - Py_DECREF(source); - if (it == NULL) { + + source = PyObject_GetIter(args); + if (source == NULL) return NULL; - } - return chain_new_internal(type, it); + + return chain_new_internal(type, source); } /*[clinic input] @@ -2075,7 +2064,6 @@ static PyType_Slot chain_slots[] = { {Py_tp_iternext, chain_next}, {Py_tp_methods, chain_methods}, {Py_tp_new, chain_new}, - {Py_tp_vectorcall, chain_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -2101,35 +2089,38 @@ struct productobject { #define productobject_CAST(op) ((productobject *)(op)) -/*[clinic input] -@vectorcall -@classmethod -itertools.product.__new__ as product_new - *iterables: array - repeat: Py_ssize_t = 1 -[clinic start generated code]*/ - static PyObject * -product_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, Py_ssize_t repeat) -/*[clinic end generated code: output=b0881d40e6be4c00 input=2d0325c612c678a1]*/ +product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { productobject *lz; - Py_ssize_t nargs, npools; + Py_ssize_t nargs, npools, repeat=1; PyObject *pools = NULL; Py_ssize_t *indices = NULL; Py_ssize_t i; - if (repeat < 0) { - PyErr_SetString(PyExc_ValueError, - "repeat argument cannot be negative"); - return NULL; + if (kwds != NULL) { + char *kwlist[] = {"repeat", 0}; + PyObject *tmpargs = PyTuple_New(0); + if (tmpargs == NULL) + return NULL; + if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", + kwlist, &repeat)) { + Py_DECREF(tmpargs); + return NULL; + } + Py_DECREF(tmpargs); + if (repeat < 0) { + PyErr_SetString(PyExc_ValueError, + "repeat argument cannot be negative"); + return NULL; + } } + assert(PyTuple_CheckExact(args)); if (repeat == 0) { nargs = 0; } else { - nargs = iterables_length; + nargs = PyTuple_GET_SIZE(args); if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) { PyErr_SetString(PyExc_OverflowError, "repeat argument too large"); return NULL; @@ -2148,7 +2139,7 @@ product_new_impl(PyTypeObject *type, PyObject * const *iterables, goto error; for (i=0; i < nargs ; ++i) { - PyObject *item = iterables[i]; + PyObject *item = PyTuple_GET_ITEM(args, i); PyObject *pool = PySequence_Tuple(item); if (pool == NULL) goto error; @@ -2337,7 +2328,6 @@ static PyType_Slot product_slots[] = { {Py_tp_iternext, product_next}, {Py_tp_methods, product_methods}, {Py_tp_new, product_new}, - {Py_tp_vectorcall, product_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, }; @@ -3758,31 +3748,23 @@ 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_impl(PyTypeObject *type, PyObject *element, PyObject *times) -/*[clinic end generated code: output=4ef9bd3593335622 input=c0eafe81f60ae9e6]*/ +repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { repeatobject *ro; - 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; - } - } + 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; ro = (repeatobject *)type->tp_alloc(type, 0); if (ro == NULL) @@ -3873,7 +3855,6 @@ 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}, }; @@ -3900,32 +3881,40 @@ struct ziplongestobject { #define ziplongestobject_CAST(op) ((ziplongestobject *)(op)) -/*[clinic input] -@vectorcall -@classmethod -itertools.zip_longest.__new__ as zip_longest_new - *iterables: array - fillvalue: object = None -[clinic start generated code]*/ - static PyObject * -zip_longest_new_impl(PyTypeObject *type, PyObject * const *iterables, - Py_ssize_t iterables_length, PyObject *fillvalue) -/*[clinic end generated code: output=9f45c950feb8e18d input=2c4cc9910ee8dab7]*/ +zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { ziplongestobject *lz; Py_ssize_t i; PyObject *ittuple; /* tuple of iterators */ PyObject *result; - Py_ssize_t tuplesize = iterables_length; + PyObject *fillvalue = Py_None; + Py_ssize_t tuplesize; + + if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { + fillvalue = NULL; + if (PyDict_GET_SIZE(kwds) == 1) { + fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue)); + } + if (fillvalue == NULL) { + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "zip_longest() got an unexpected keyword argument"); + } + return NULL; + } + } + /* args must be a tuple */ + assert(PyTuple_Check(args)); + tuplesize = PyTuple_GET_SIZE(args); /* obtain iterators */ ittuple = PyTuple_New(tuplesize); if (ittuple == NULL) return NULL; for (i=0; i < tuplesize; i++) { - PyObject *item = iterables[i]; + PyObject *item = PyTuple_GET_ITEM(args, i); PyObject *it = PyObject_GetIter(item); if (it == NULL) { Py_DECREF(ittuple); @@ -4082,7 +4071,6 @@ static PyType_Slot ziplongest_slots[] = { {Py_tp_iter, PyObject_SelfIter}, {Py_tp_iternext, zip_longest_next}, {Py_tp_new, zip_longest_new}, - {Py_tp_vectorcall, zip_longest_vectorcall}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, };