File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -24,11 +24,18 @@ def _task_repr_info(task):
24
24
25
25
def _task_get_stack (task , limit ):
26
26
frames = []
27
- try :
28
- # 'async def' coroutines
27
+ if hasattr ( task . _coro , 'cr_frame' ) :
28
+ # case 1: 'async def' coroutines
29
29
f = task ._coro .cr_frame
30
- except AttributeError :
30
+ elif hasattr (task ._coro , 'gi_frame' ):
31
+ # case 2: legacy coroutines
31
32
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
32
39
if f is not None :
33
40
while f is not None :
34
41
if limit is not None :
Original file line number Diff line number Diff line change @@ -1191,5 +1191,20 @@ async def run():
1191
1191
1192
1192
self .loop .run_until_complete (run ())
1193
1193
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
+
1194
1209
if __name__ == "__main__" :
1195
1210
unittest .main ()
Original file line number Diff line number Diff line change
1
+ Fix AttributeError when calling get_stack on a PyAsyncGenObject Task
You can’t perform that action at this time.
0 commit comments