]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-93061: Mark as artificial: backwards jump after async for (GH-93062) (GH-93110)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 23 May 2022 18:58:53 +0000 (11:58 -0700)
committerGitHub <noreply@github.com>
Mon, 23 May 2022 18:58:53 +0000 (14:58 -0400)
(cherry picked from commit a458be3263b4cb92f3fde726461e8ef44b2a4a9d)

Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Lib/test/test_sys_settrace.py
Misc/NEWS.d/next/Core and Builtins/2022-05-22-02-37-50.gh-issue-93061.r70Imp.rst [new file with mode: 0644]
Python/compile.c

index 9cc6bcfcab5e1ca2f7acedd23072f4cc17e38f60..162fd8328582ca8bb5f26a70570d967613f9fd68 100644 (file)
@@ -609,6 +609,58 @@ class TraceTestCase(unittest.TestCase):
         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/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 (file)
index 0000000..d41e590
--- /dev/null
@@ -0,0 +1 @@
+Backward jumps after ``async for`` loops are no longer given dubious line numbers.
index 45944ae85e383ef5fdf0c0f732f0f9c472f15fd6..cc0d76e0384c42e4b25a57e93581b34487d64d56 100644 (file)
@@ -3152,6 +3152,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);