Skip to content

Commit 71fa0fd

Browse files
committed
Set index to -1 when list iterators become exhausted in tier 2
1 parent 59be79a commit 71fa0fd

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

Lib/test/test_list.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ def __eq__(self, other):
299299
lst = [X(), X()]
300300
X() in lst
301301

302+
def test_tier2_invalidates_iterator(self):
303+
# GH-121012
304+
for _ in range(100):
305+
a = [1, 2, 3]
306+
it = iter(a)
307+
for _ in it:
308+
pass
309+
a.append(4)
310+
self.assertEqual(list(it), [])
302311

303312
if __name__ == "__main__":
304313
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Tier 2 execution now ensures that list iterators remain exhausted, once they
2+
become exhausted.

Python/bytecodes.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2967,7 +2967,10 @@ dummy_func(
29672967
assert(Py_TYPE(iter_o) == &PyListIter_Type);
29682968
PyListObject *seq = it->it_seq;
29692969
EXIT_IF(seq == NULL);
2970-
EXIT_IF((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq));
2970+
if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) {
2971+
it->it_index = -1;
2972+
EXIT_IF(1);
2973+
}
29712974
}
29722975

29732976
op(_ITER_NEXT_LIST, (iter -- iter, next)) {

Python/executor_cases.c.h

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)