Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 1 | import linecache |
| 2 | import traceback |
| 3 | |
| 4 | from . import base_futures |
| 5 | from . import coroutines |
| 6 | |
| 7 | |
| 8 | def _task_repr_info(task): |
| 9 | info = base_futures._future_repr_info(task) |
| 10 | |
| 11 | if task._must_cancel: |
| 12 | # replace status |
| 13 | info[0] = 'cancelling' |
| 14 | |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 15 | info.insert(1, 'name=%r' % task.get_name()) |
| 16 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 17 | coro = coroutines._format_coroutine(task._coro) |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 18 | info.insert(2, f'coro=<{coro}>') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 19 | |
| 20 | if task._fut_waiter is not None: |
Alex Grönholm | cca4eec | 2018-08-09 00:06:47 +0300 | [diff] [blame] | 21 | info.insert(3, f'wait_for={task._fut_waiter!r}') |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 22 | return info |
| 23 | |
| 24 | |
| 25 | def _task_get_stack(task, limit): |
| 26 | frames = [] |
Lidi Zheng | 4482337 | 2020-03-02 04:45:54 -0800 | [diff] [blame] | 27 | if hasattr(task._coro, 'cr_frame'): |
| 28 | # case 1: 'async def' coroutines |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 29 | f = task._coro.cr_frame |
Lidi Zheng | 4482337 | 2020-03-02 04:45:54 -0800 | [diff] [blame] | 30 | elif hasattr(task._coro, 'gi_frame'): |
| 31 | # case 2: legacy coroutines |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 32 | f = task._coro.gi_frame |
Lidi Zheng | 4482337 | 2020-03-02 04:45:54 -0800 | [diff] [blame] | 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 |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 39 | if f is not None: |
| 40 | while f is not None: |
| 41 | if limit is not None: |
| 42 | if limit <= 0: |
| 43 | break |
| 44 | limit -= 1 |
| 45 | frames.append(f) |
| 46 | f = f.f_back |
| 47 | frames.reverse() |
| 48 | elif task._exception is not None: |
| 49 | tb = task._exception.__traceback__ |
| 50 | while tb is not None: |
| 51 | if limit is not None: |
| 52 | if limit <= 0: |
| 53 | break |
| 54 | limit -= 1 |
| 55 | frames.append(tb.tb_frame) |
| 56 | tb = tb.tb_next |
| 57 | return frames |
| 58 | |
| 59 | |
| 60 | def _task_print_stack(task, limit, file): |
| 61 | extracted_list = [] |
| 62 | checked = set() |
| 63 | for f in task.get_stack(limit=limit): |
| 64 | lineno = f.f_lineno |
| 65 | co = f.f_code |
| 66 | filename = co.co_filename |
| 67 | name = co.co_name |
| 68 | if filename not in checked: |
| 69 | checked.add(filename) |
| 70 | linecache.checkcache(filename) |
| 71 | line = linecache.getline(filename, lineno, f.f_globals) |
| 72 | extracted_list.append((filename, lineno, name, line)) |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 73 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 74 | exc = task._exception |
| 75 | if not extracted_list: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 76 | print(f'No stack for {task!r}', file=file) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 77 | elif exc is not None: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 78 | print(f'Traceback for {task!r} (most recent call last):', file=file) |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 79 | else: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 80 | print(f'Stack for {task!r} (most recent call last):', file=file) |
| 81 | |
Yury Selivanov | a0c1ba6 | 2016-10-28 12:52:37 -0400 | [diff] [blame] | 82 | traceback.print_list(extracted_list, file=file) |
| 83 | if exc is not None: |
| 84 | for line in traceback.format_exception_only(exc.__class__, exc): |
| 85 | print(line, file=file, end='') |