Files
Jeongseop Lim d634f2649c Check enumerate() arguments before calling iter() (#9241)
EnumerateArgs bound the iterable as PyIter, so argument binding called
__iter__ before start was converted and before surplus positional
arguments were rejected. An exception from __iter__ then replaced the
TypeError CPython raises for a bad start or a third argument.

Bind the iterable as an object and call get_iter() in py_new, after
start is converted, which is the order of CPython's enum_new_impl.

Assisted-by: Claude Code:claude-opus-5-5
2026-10-20 19:55:56 +09:00

38 lines
1.0 KiB
Python

from testutils import TestFailingIter, assert_raises
assert list(enumerate(["a", "b", "c"])) == [(0, "a"), (1, "b"), (2, "c")]
assert type(enumerate([])) == enumerate
assert list(enumerate(["a", "b", "c"], -100)) == [(-100, "a"), (-99, "b"), (-98, "c")]
assert list(enumerate(["a", "b", "c"], 2**200)) == [
(2**200, "a"),
(2**200 + 1, "b"),
(2**200 + 2, "c"),
]
# test infinite iterator
class Counter(object):
counter = 0
def __next__(self):
self.counter += 1
return self.counter
def __iter__(self):
return self
it = enumerate(Counter())
assert next(it) == (0, 1)
assert next(it) == (1, 2)
# start and the argument count are checked before iter() is called
assert_raises(TypeError, enumerate, TestFailingIter(), {})
assert_raises(TypeError, enumerate, TestFailingIter(), 0, 0)
assert_raises(TypeError, enumerate, TestFailingIter(), foo=1)
assert_raises(TypeError, enumerate, iterable=TestFailingIter(), start={})
assert_raises(RuntimeError, enumerate, TestFailingIter(), 0)