Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 16 additions & 2 deletions Lib/test/test_enumerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ def test_argumentcheck(self):
self.assertRaises(TypeError, self.enum, 'abc', 'a') # wrong type
self.assertRaises(TypeError, self.enum, 'abc', 2, 3) # too many arguments

def test_kwargs(self):
self.assertEqual(list(self.enum(iterable=Ig(self.seq))), self.res)
expected = list(self.enum(Ig(self.seq), 0))
self.assertEqual(list(self.enum(iterable=Ig(self.seq), start=0)),
expected)
self.assertEqual(list(self.enum(start=0, iterable=Ig(self.seq))),
expected)
self.assertRaises(TypeError, self.enum, iterable=[], x=3)
self.assertRaises(TypeError, self.enum, start=0, x=3)
self.assertRaises(TypeError, self.enum, x=0, y=3)
self.assertRaises(TypeError, self.enum, x=0)

@support.cpython_only
def test_tuple_reuse(self):
# Tests an implementation detail where tuple is reused
Expand Down Expand Up @@ -266,14 +278,16 @@ def test_basicfunction(self):


class TestStart(EnumerateStartTestCase):
def enum(self, iterable, start=11):
return enumerate(iterable, start=start)

enum = lambda self, i: enumerate(i, start=11)
seq, res = 'abc', [(11, 'a'), (12, 'b'), (13, 'c')]


class TestLongStart(EnumerateStartTestCase):
def enum(self, iterable, start=sys.maxsize + 1):
return enumerate(iterable, start=start)

enum = lambda self, i: enumerate(i, start=sys.maxsize+1)
seq, res = 'abc', [(sys.maxsize+1,'a'), (sys.maxsize+2,'b'),
(sys.maxsize+3,'c')]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow passing ``iterable`` as a keyword argument to :func:`enumerate` again.
Patch by Jelle Zijlstra.
44 changes: 38 additions & 6 deletions Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,11 @@ 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);
Expand All @@ -108,14 +104,50 @@ enumerate_vectorcall(PyObject *type, PyObject *const *args,
"'%S' is an invalid keyword argument for enumerate()", kw);
return NULL;
}
} else if (nkwargs == 2) {
PyObject *kw0 = PyTuple_GET_ITEM(kwnames, 0);
PyObject *kw1 = PyTuple_GET_ITEM(kwnames, 1);
if (_PyUnicode_EqualToASCIIString(kw0, "start")) {
if (!_PyUnicode_EqualToASCIIString(kw1, "iterable")) {
PyErr_Format(PyExc_TypeError,
"'%S' is an invalid keyword argument for enumerate()", kw1);
return NULL;
}
return enum_new_impl(tp, args[1], args[0]);
}
if (!_PyUnicode_EqualToASCIIString(kw0, "iterable")) {
PyErr_Format(PyExc_TypeError,
"'%S' is an invalid keyword argument for enumerate()", kw0);
return NULL;
}
if (!_PyUnicode_EqualToASCIIString(kw1, "start")) {
PyErr_Format(PyExc_TypeError,
"'%S' is an invalid keyword argument for enumerate()", kw1);
return NULL;
}

}
return enum_new_impl(tp, args[0], args[1]);
}

if (nargs == 1 && nkwargs == 0) {
if (nargs + nkwargs == 1) {
if (nkwargs == 1) {
PyObject *kw0 = PyTuple_GET_ITEM(kwnames, 0);
if (!_PyUnicode_EqualToASCIIString(kw0, "iterable")) {
PyErr_Format(PyExc_TypeError,
"'%S' is an invalid keyword argument for enumerate()", kw0);
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;
Expand Down