| 32 | |
| 33 | |
| 34 | def _task_get_stack(task, limit): |
| 35 | frames = [] |
| 36 | if hasattr(task._coro, 'cr_frame'): |
| 37 | # case 1: 'async def' coroutines |
| 38 | f = task._coro.cr_frame |
| 39 | elif hasattr(task._coro, 'gi_frame'): |
| 40 | # case 2: legacy coroutines |
| 41 | f = task._coro.gi_frame |
| 42 | elif hasattr(task._coro, 'ag_frame'): |
| 43 | # case 3: async generators |
| 44 | f = task._coro.ag_frame |
| 45 | else: |
| 46 | # case 4: unknown objects |
| 47 | f = None |
| 48 | if f is not None: |
| 49 | while f is not None: |
| 50 | if limit is not None: |
| 51 | if limit <= 0: |
| 52 | break |
| 53 | limit -= 1 |
| 54 | frames.append(f) |
| 55 | f = f.f_back |
| 56 | frames.reverse() |
| 57 | elif task._exception is not None: |
| 58 | tb = task._exception.__traceback__ |
| 59 | while tb is not None: |
| 60 | if limit is not None: |
| 61 | if limit <= 0: |
| 62 | break |
| 63 | limit -= 1 |
| 64 | frames.append(tb.tb_frame) |
| 65 | tb = tb.tb_next |
| 66 | return frames |
| 67 | |
| 68 | |
| 69 | def _task_print_stack(task, limit, file): |