]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39764: Make Task.get_stack accept ag_frame (GH-18669)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Mar 2020 13:03:51 +0000 (05:03 -0800)
committerGitHub <noreply@github.com>
Mon, 2 Mar 2020 13:03:50 +0000 (05:03 -0800)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
(cherry picked from commit 4482337decdbd0c6e2150346a68b3616bda664aa)

Co-authored-by: Lidi Zheng <scallopsky@gmail.com>
Lib/asyncio/base_tasks.py
Lib/test/test_asyncgen.py
Misc/NEWS.d/next/Library/2020-02-27-18-21-07.bpo-39764.wqPk68.rst [new file with mode: 0644]

index e2da462fde7400555b3332d4f040440d53205895..09bb171a2ce750a06a39e978bea0d3a7accf25f4 100644 (file)
@@ -24,11 +24,18 @@ def _task_repr_info(task):
 
 def _task_get_stack(task, limit):
     frames = []
-    try:
-        # 'async def' coroutines
+    if hasattr(task._coro, 'cr_frame'):
+        # case 1: 'async def' coroutines
         f = task._coro.cr_frame
-    except AttributeError:
+    elif hasattr(task._coro, 'gi_frame'):
+        # case 2: legacy coroutines
         f = task._coro.gi_frame
+    elif hasattr(task._coro, 'ag_frame'):
+        # case 3: async generators
+        f = task._coro.ag_frame
+    else:
+        # case 4: unknown objects
+        f = None
     if f is not None:
         while f is not None:
             if limit is not None:
index fb6321d2264f31d065c71ede3491ff36c03e663a..62bf8774166529a430d98cb699476ad2ec639fff 100644 (file)
@@ -1191,5 +1191,20 @@ class AsyncGenAsyncioTest(unittest.TestCase):
 
         self.loop.run_until_complete(run())
 
+    def test_async_gen_aclose_compatible_with_get_stack(self):
+        async def async_generator():
+            yield object()
+
+        async def run():
+            ag = async_generator()
+            asyncio.create_task(ag.aclose())
+            tasks = asyncio.all_tasks()
+            for task in tasks:
+                # No AttributeError raised
+                task.get_stack()
+
+        self.loop.run_until_complete(run())
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2020-02-27-18-21-07.bpo-39764.wqPk68.rst b/Misc/NEWS.d/next/Library/2020-02-27-18-21-07.bpo-39764.wqPk68.rst
new file mode 100644 (file)
index 0000000..d61db2e
--- /dev/null
@@ -0,0 +1 @@
+Fix AttributeError when calling get_stack on a PyAsyncGenObject Task
\ No newline at end of file