]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] GH-121012: Set index to -1 when list iterators become exhausted in tier 2...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 8 Jul 2024 17:09:54 +0000 (19:09 +0200)
committerGitHub <noreply@github.com>
Mon, 8 Jul 2024 17:09:54 +0000 (18:09 +0100)
Lib/test/test_list.py
Misc/NEWS.d/next/Core and Builtins/2024-07-08-10-31-08.gh-issue-121012.M5hHk-.rst [new file with mode: 0644]
Python/bytecodes.c
Python/executor_cases.c.h

index 4d2d54705fc894fc215c5d1f3c69da8d8abb6858..ad7accf2099f43dac2e0a2d8fb1eaf91954835cb 100644 (file)
@@ -299,6 +299,15 @@ class ListTest(list_tests.CommonTest):
         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 (file)
index 0000000..7b04eb6
--- /dev/null
@@ -0,0 +1,2 @@
+Tier 2 execution now ensures that list iterators remain exhausted, once they
+become exhausted.
index 0dc60bbbb6a57990b400d13c2f7bdcf5342a0e63..a6e9a73cb0e57f81f1040bc3300665b2369b5df2 100644 (file)
@@ -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)) {
index 4e0f73f7e23a0a801b0642f04d8f55cf0f5882ad..8080d20915f1d947b1b5c6b59f291a0969a20107 100644 (file)
                 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;
         }