[Python-checkins] bpo-39764: Make Task.get_stack accept ag_frame (GH-18669)

Miss Islington (bot) webhook-mailer at python.org
Mon Mar 2 08:04:08 EST 2020


https://github.com/python/cpython/commit/43932dc1eaf36d75b2ee0420d8be747001315c26
commit: 43932dc1eaf36d75b2ee0420d8be747001315c26
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-02T05:03:50-08:00
summary:

bpo-39764: Make Task.get_stack accept ag_frame (GH-18669)


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 at gmail.com>

files:
A Misc/NEWS.d/next/Library/2020-02-27-18-21-07.bpo-39764.wqPk68.rst
M Lib/asyncio/base_tasks.py
M Lib/test/test_asyncgen.py

diff --git a/Lib/asyncio/base_tasks.py b/Lib/asyncio/base_tasks.py
index e2da462fde740..09bb171a2ce75 100644
--- a/Lib/asyncio/base_tasks.py
+++ b/Lib/asyncio/base_tasks.py
@@ -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:
diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py
index fb6321d2264f3..62bf877416652 100644
--- a/Lib/test/test_asyncgen.py
+++ b/Lib/test/test_asyncgen.py
@@ -1191,5 +1191,20 @@ def test_async_gen_aclose_after_exhaustion(self):
 
         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
index 0000000000000..d61db2ea221f2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-02-27-18-21-07.bpo-39764.wqPk68.rst
@@ -0,0 +1 @@
+Fix AttributeError when calling get_stack on a PyAsyncGenObject Task
\ No newline at end of file



More information about the Python-checkins mailing list