Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
183 changes: 182 additions & 1 deletion Modules/clinic/itertoolsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 33 additions & 42 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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},
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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},
};
Expand Down
Loading