bpo-46527: allow calling enumerate(iterable=...) again (GH-30904)

This commit is contained in:
Jelle Zijlstra 2022-01-26 07:46:48 -08:00 committed by GitHub
parent 1e8a3a5579
commit ac0c6e128c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 12 deletions

View file

@ -83,6 +83,18 @@ enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start)
return (PyObject *)en;
}
static int check_keyword(PyObject *kwnames, int index,
const char *name)
{
PyObject *kw = PyTuple_GET_ITEM(kwnames, index);
if (!_PyUnicode_EqualToASCIIString(kw, name)) {
PyErr_Format(PyExc_TypeError,
"'%S' is an invalid keyword argument for enumerate()", kw);
return 0;
}
return 1;
}
// TODO: Use AC when bpo-43447 is supported
static PyObject *
enumerate_vectorcall(PyObject *type, PyObject *const *args,
@ -91,31 +103,46 @@ enumerate_vectorcall(PyObject *type, PyObject *const *args,
PyTypeObject *tp = _PyType_CAST(type);
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Py_ssize_t nkwargs = 0;
if (nargs == 0) {
PyErr_SetString(PyExc_TypeError,
"enumerate() missing required argument 'iterable'");
return NULL;
}
if (kwnames != NULL) {
nkwargs = PyTuple_GET_SIZE(kwnames);
}
// Manually implement enumerate(iterable, start=...)
if (nargs + nkwargs == 2) {
if (nkwargs == 1) {
PyObject *kw = PyTuple_GET_ITEM(kwnames, 0);
if (!_PyUnicode_EqualToASCIIString(kw, "start")) {
PyErr_Format(PyExc_TypeError,
"'%S' is an invalid keyword argument for enumerate()", kw);
if (!check_keyword(kwnames, 0, "start")) {
return NULL;
}
} else if (nkwargs == 2) {
PyObject *kw0 = PyTuple_GET_ITEM(kwnames, 0);
if (_PyUnicode_EqualToASCIIString(kw0, "start")) {
if (!check_keyword(kwnames, 1, "iterable")) {
return NULL;
}
return enum_new_impl(tp, args[1], args[0]);
}
if (!check_keyword(kwnames, 0, "iterable") ||
!check_keyword(kwnames, 1, "start")) {
return NULL;
}
}
return enum_new_impl(tp, args[0], args[1]);
}
if (nargs == 1 && nkwargs == 0) {
if (nargs + nkwargs == 1) {
if (nkwargs == 1 && !check_keyword(kwnames, 0, "iterable")) {
return NULL;
}
return enum_new_impl(tp, args[0], NULL);
}
if (nargs == 0) {
PyErr_SetString(PyExc_TypeError,
"enumerate() missing required argument 'iterable'");
return NULL;
}
PyErr_Format(PyExc_TypeError,
"enumerate() takes at most 2 arguments (%d given)", nargs + nkwargs);
return NULL;