Skip to content

Commit

Permalink
[3.13] GH-121012: Set index to -1 when list iterators become exhauste…
Browse files Browse the repository at this point in the history
…d in tier 2 (GH-121483) (GH-121494)
  • Loading branch information
miss-islington authored Jul 8, 2024
1 parent bccfd8a commit 2f8919e
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 @@ -2695,7 +2695,10 @@ dummy_func(
assert(Py_TYPE(iter) == &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 2f8919e

Please sign in to comment.