From b91e884799573c1d69cb2e9fc163e285632b587f Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Thu, 12 May 2022 14:56:57 -0400 Subject: [PATCH 1/2] Let frame_setlineno jump over listcomps --- Lib/test/test_sys_settrace.py | 48 +++++++++++++++++++++++++++++++++++ Objects/frameobject.c | 5 +++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index 7519309c5e5895..8884bf45a9991c 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -1603,6 +1603,54 @@ def gen(): 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__": unittest.main() diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 4ae17bcfc2109e..10db68ef1e0724 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -173,7 +173,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: From 212c0ab54821c31a9dc48f883b22ab7de82d27bc Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 12 May 2022 18:59:29 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-05-12-18-59-27.gh-issue-92311.VEgtts.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-05-12-18-59-27.gh-issue-92311.VEgtts.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-12-18-59-27.gh-issue-92311.VEgtts.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-12-18-59-27.gh-issue-92311.VEgtts.rst new file mode 100644 index 00000000000000..b800def656cbfd --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-12-18-59-27.gh-issue-92311.VEgtts.rst @@ -0,0 +1 @@ +Fixed a bug where setting ``frame.f_lineno`` to jump over a list comprehension could misbehave or crash.