https://github.com/python/cpython/commit/2f8919ee3c152d6249a0a0dc14c86c0547bfe4b6
commit: 2f8919ee3c152d6249a0a0dc14c86c0547bfe4b6
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: markshannon <[email protected]>
date: 2024-07-08T18:09:54+01:00
summary:

[3.13] GH-121012: Set index to -1 when list iterators become exhausted in tier 
2 (GH-121483) (GH-121494)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-07-08-10-31-08.gh-issue-121012.M5hHk-.rst
M Lib/test/test_list.py
M Python/bytecodes.c
M Python/executor_cases.c.h

diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py
index 4d2d54705fc894..ad7accf2099f43 100644
--- a/Lib/test/test_list.py
+++ b/Lib/test/test_list.py
@@ -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()
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-07-08-10-31-08.gh-issue-121012.M5hHk-.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-07-08-10-31-08.gh-issue-121012.M5hHk-.rst
new file mode 100644
index 00000000000000..7b04eb68b03752
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-07-08-10-31-08.gh-issue-121012.M5hHk-.rst 
@@ -0,0 +1,2 @@
+Tier 2 execution now ensures that list iterators remain exhausted, once they
+become exhausted.
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 0dc60bbbb6a579..a6e9a73cb0e57f 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -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)) {
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index 4e0f73f7e23a0a..8080d20915f1d9 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -2675,8 +2675,11 @@
                 JUMP_TO_JUMP_TARGET();
             }
             if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) {
-                UOP_STAT_INC(uopcode, miss);
-                JUMP_TO_JUMP_TARGET();
+                it->it_index = -1;
+                if (1) {
+                    UOP_STAT_INC(uopcode, miss);
+                    JUMP_TO_JUMP_TARGET();
+                }
             }
             break;
         }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to