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

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
Lidi Zheng 2020-03-02 04:45:54 -08:00 committed by GitHub
parent 1382c3289b
commit 4482337dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 3 deletions

View File

@ -24,11 +24,18 @@ def _task_repr_info(task):
def _task_get_stack(task, limit): def _task_get_stack(task, limit):
frames = [] frames = []
try: if hasattr(task._coro, 'cr_frame'):
# 'async def' coroutines # case 1: 'async def' coroutines
f = task._coro.cr_frame f = task._coro.cr_frame
except AttributeError: elif hasattr(task._coro, 'gi_frame'):
# case 2: legacy coroutines
f = task._coro.gi_frame 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: if f is not None:
while f is not None: while f is not None:
if limit is not None: if limit is not None:

View File

@ -1191,5 +1191,20 @@ class AsyncGenAsyncioTest(unittest.TestCase):
self.loop.run_until_complete(run()) 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__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1 @@
Fix AttributeError when calling get_stack on a PyAsyncGenObject Task