Skip to content

Commit 43932dc

Browse files
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 4482337) Co-authored-by: Lidi Zheng <[email protected]>
1 parent f28b0c7 commit 43932dc

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Lib/asyncio/base_tasks.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ def _task_repr_info(task):
2424

2525
def _task_get_stack(task, limit):
2626
frames = []
27-
try:
28-
# 'async def' coroutines
27+
if hasattr(task._coro, 'cr_frame'):
28+
# case 1: 'async def' coroutines
2929
f = task._coro.cr_frame
30-
except AttributeError:
30+
elif hasattr(task._coro, 'gi_frame'):
31+
# case 2: legacy coroutines
3132
f = task._coro.gi_frame
33+
elif hasattr(task._coro, 'ag_frame'):
34+
# case 3: async generators
35+
f = task._coro.ag_frame
36+
else:
37+
# case 4: unknown objects
38+
f = None
3239
if f is not None:
3340
while f is not None:
3441
if limit is not None:

Lib/test/test_asyncgen.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,5 +1191,20 @@ async def run():
11911191

11921192
self.loop.run_until_complete(run())
11931193

1194+
def test_async_gen_aclose_compatible_with_get_stack(self):
1195+
async def async_generator():
1196+
yield object()
1197+
1198+
async def run():
1199+
ag = async_generator()
1200+
asyncio.create_task(ag.aclose())
1201+
tasks = asyncio.all_tasks()
1202+
for task in tasks:
1203+
# No AttributeError raised
1204+
task.get_stack()
1205+
1206+
self.loop.run_until_complete(run())
1207+
1208+
11941209
if __name__ == "__main__":
11951210
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix AttributeError when calling get_stack on a PyAsyncGenObject Task

0 commit comments

Comments
 (0)