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) diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index 3fd1b7a2fc7b90..a3f4a34da76a6a 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -750,6 +750,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); @@ -1564,6 +1660,91 @@ itertools_count_vectorcall(PyObject *type, PyObject *const *args, 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); @@ -1578,4 +1759,4 @@ zip_longest_next(PyObject *self) return return_value; } -/*[clinic end generated code: output=60939958c9c9348b input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e7b0783cc2edf0c0 input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index d7daad8c303858..05915684de0d24 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -2089,38 +2089,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; @@ -2139,7 +2136,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; @@ -2328,6 +2325,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}, }; @@ -3881,40 +3879,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); @@ -4071,6 +4061,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}, };