]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.10] gh-92311: Let frame_setlineno jump over listcomps (GH-92717)
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Thu, 12 May 2022 15:31:43 +0000 (11:31 -0400)
committerGitHub <noreply@github.com>
Thu, 12 May 2022 15:31:43 +0000 (16:31 +0100)
Lib/test/test_sys_settrace.py
Misc/NEWS.d/next/Core and Builtins/2022-05-12-09-38-20.gh-issue-92311.VEgtts.rst [new file with mode: 0644]
Objects/frameobject.c

index 4f13bbd6bfda2f3190669059076f552e38926daa..76d1aeecf69e458c5e941c3b059458ae2f3f04cb 100644 (file)
@@ -2094,6 +2094,54 @@ output.append(4)
             yield 3
         next(gen())
         output.append(5)
+    
+    @jump_test(2, 3, [1, 3])
+    def test_jump_forward_over_listcomp(output):
+        output.append(1)
+        x = [i for i in range(10)]
+        output.append(3)
+
+    # checking for segfaults.
+    # See https://github.com/python/cpython/issues/92311
+    @jump_test(3, 1, [])
+    def test_jump_backward_over_listcomp(output):
+        a = 1
+        x = [i for i in range(10)]
+        c = 3
+
+    @jump_test(8, 2, [2, 7, 2])
+    def test_jump_backward_over_listcomp_v2(output):
+        flag = False
+        output.append(2)
+        if flag:
+            return
+        x = [i for i in range(5)]
+        flag = 6
+        output.append(7)
+        output.append(8)
+
+    @async_jump_test(2, 3, [1, 3])
+    async def test_jump_forward_over_async_listcomp(output):
+        output.append(1)
+        x = [i async for i in asynciter(range(10))]
+        output.append(3)
+
+    @async_jump_test(3, 1, [])
+    async def test_jump_backward_over_async_listcomp(output):
+        a = 1
+        x = [i async for i in asynciter(range(10))]
+        c = 3
+
+    @async_jump_test(8, 2, [2, 7, 2])
+    async def test_jump_backward_over_async_listcomp_v2(output):
+        flag = False
+        output.append(2)
+        if flag:
+            return
+        x = [i async for i in asynciter(range(5))]
+        flag = 6
+        output.append(7)
+        output.append(8)
 
 
 if __name__ == "__main__":
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-12-09-38-20.gh-issue-92311.VEgtts.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-12-09-38-20.gh-issue-92311.VEgtts.rst
new file mode 100644 (file)
index 0000000..b800def
--- /dev/null
@@ -0,0 +1 @@
+Fixed a bug where setting ``frame.f_lineno`` to jump over a list comprehension could misbehave or crash.
index d02cf9d3ba9f0689b329c5136d210d5296356fd4..be84d33bf52389f202fd7c9d25222eeb607c8424 100644 (file)
@@ -195,7 +195,10 @@ markblocks(PyCodeObject *code_obj, int len)
                     break;
                 case GET_ITER:
                 case GET_AITER:
-                    block_stack = push_block(block_stack, Loop);
+                    // For-loops get a Loop block, but comprehensions do not.
+                    if (_Py_OPCODE(code[i + 1]) != CALL_FUNCTION) {
+                        block_stack = push_block(block_stack, Loop);
+                    }
                     blocks[i+1] = block_stack;
                     break;
                 case FOR_ITER: