Skip to content

gh-93061: Mark as artificial: backwards jump after async for #93062

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 2 commits into from
May 23, 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
52 changes: 52 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,58 @@ def run(tracer):
self.compare_events(doit_async.__code__.co_firstlineno,
tracer.events, events)

def test_async_for_backwards_jump_has_no_line(self):
async def arange(n):
for i in range(n):
yield i
async def f():
async for i in arange(3):
if i > 100:
break # should never be traced

tracer = self.make_tracer()
coro = f()
try:
sys.settrace(tracer.trace)
coro.send(None)
except Exception:
pass
finally:
sys.settrace(None)

events = [
(0, 'call'),
(1, 'line'),
(-3, 'call'),
(-2, 'line'),
(-1, 'line'),
(-1, 'return'),
(1, 'exception'),
(2, 'line'),
(1, 'line'),
(-1, 'call'),
(-2, 'line'),
(-1, 'line'),
(-1, 'return'),
(1, 'exception'),
(2, 'line'),
(1, 'line'),
(-1, 'call'),
(-2, 'line'),
(-1, 'line'),
(-1, 'return'),
(1, 'exception'),
(2, 'line'),
(1, 'line'),
(-1, 'call'),
(-2, 'line'),
(-2, 'return'),
(1, 'exception'),
(1, 'return'),
]
self.compare_events(f.__code__.co_firstlineno,
tracer.events, events)

def test_21_repeated_pass(self):
def func():
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Backward jumps after ``async for`` loops are no longer given dubious line numbers.
2 changes: 2 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,8 @@ compiler_async_for(struct compiler *c, stmt_ty s)
/* Success block for __anext__ */
VISIT(c, expr, s->v.AsyncFor.target);
VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
/* Mark jump as artificial */
UNSET_LOC(c);
ADDOP_JUMP(c, JUMP, start);

compiler_pop_fblock(c, FOR_LOOP, start);
Expand Down