Skip to content

[3.10] gh-92311: Let frame_setlineno jump over listcomps #92717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,54 @@ def gen():
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__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug where setting ``frame.f_lineno`` to jump over a list comprehension could misbehave or crash.
5 changes: 4 additions & 1 deletion Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down