From 323a42f3a87093b87de8dfc1a6c4a7daf6e5fa69 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sat, 21 May 2022 22:24:49 -0400 Subject: [PATCH 1/2] Mark as artifical: backwards jump after async for --- Lib/test/test_sys_settrace.py | 52 +++++++++++++++++++++++++++++++++++ Python/compile.c | 2 ++ 2 files changed, 54 insertions(+) diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index 9cc6bcfcab5e1c..162fd8328582ca 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -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 diff --git a/Python/compile.c b/Python/compile.c index c862c10a83e53c..a4738c1f48f2a3 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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); From bdc29495959c2ded6e9907d21fa2017a78692b2e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 22 May 2022 02:37:52 +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-22-02-37-50.gh-issue-93061.r70Imp.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-05-22-02-37-50.gh-issue-93061.r70Imp.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-22-02-37-50.gh-issue-93061.r70Imp.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-22-02-37-50.gh-issue-93061.r70Imp.rst new file mode 100644 index 00000000000000..d41e59028ad570 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-22-02-37-50.gh-issue-93061.r70Imp.rst @@ -0,0 +1 @@ +Backward jumps after ``async for`` loops are no longer given dubious line numbers.