mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.13] GH-121012: Set index to -1 when list iterators become exhausted in tier 2 (GH-121483) (GH-121494)
This commit is contained in:
parent
bccfd8a53f
commit
2f8919ee3c
4 changed files with 20 additions and 3 deletions
|
@ -299,6 +299,15 @@ class ListTest(list_tests.CommonTest):
|
||||||
lst = [X(), X()]
|
lst = [X(), X()]
|
||||||
X() in lst
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Tier 2 execution now ensures that list iterators remain exhausted, once they
|
||||||
|
become exhausted.
|
|
@ -2695,7 +2695,10 @@ dummy_func(
|
||||||
assert(Py_TYPE(iter) == &PyListIter_Type);
|
assert(Py_TYPE(iter) == &PyListIter_Type);
|
||||||
PyListObject *seq = it->it_seq;
|
PyListObject *seq = it->it_seq;
|
||||||
EXIT_IF(seq == NULL);
|
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)) {
|
op(_ITER_NEXT_LIST, (iter -- iter, next)) {
|
||||||
|
|
7
Python/executor_cases.c.h
generated
7
Python/executor_cases.c.h
generated
|
@ -2675,8 +2675,11 @@
|
||||||
JUMP_TO_JUMP_TARGET();
|
JUMP_TO_JUMP_TARGET();
|
||||||
}
|
}
|
||||||
if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) {
|
if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) {
|
||||||
UOP_STAT_INC(uopcode, miss);
|
it->it_index = -1;
|
||||||
JUMP_TO_JUMP_TARGET();
|
if (1) {
|
||||||
|
UOP_STAT_INC(uopcode, miss);
|
||||||
|
JUMP_TO_JUMP_TARGET();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue