Skip to content

Commit

Permalink
pythonGH-121012: Set index to -1 when list iterators become exhausted…
Browse files Browse the repository at this point in the history
… in tier 2 (pythonGH-121483)
  • Loading branch information
markshannon authored Jul 8, 2024
1 parent d69529d commit 8ad6067
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Lib/test/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ def __eq__(self, other):
lst = [X(), X()]
X() in lst

def test_tier2_invalidates_iterator(self):
# GH-121012
for _ in range(100):
a = [1, 2, 3]
it = iter(a)
for _ in it:
pass
a.append(4)
self.assertEqual(list(it), [])

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Tier 2 execution now ensures that list iterators remain exhausted, once they
become exhausted.
5 changes: 4 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2967,7 +2967,10 @@ dummy_func(
assert(Py_TYPE(iter_o) == &PyListIter_Type);
PyListObject *seq = it->it_seq;
EXIT_IF(seq == NULL);
EXIT_IF((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq));
if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) {
it->it_index = -1;
EXIT_IF(1);
}
}

op(_ITER_NEXT_LIST, (iter -- iter, next)) {
Expand Down
7 changes: 5 additions & 2 deletions Python/executor_cases.c.h

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

0 comments on commit 8ad6067

Please sign in to comment.