blob: 3ce51f6a9866e4d8b469638efcf0c9be982a3e3d [file] [log] [blame]
Yury Selivanova0c1ba62016-10-28 12:52:37 -04001import linecache
2import traceback
3
4from . import base_futures
5from . import coroutines
6
7
8def _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
15 coro = coroutines._format_coroutine(task._coro)
Yury Selivanov6370f342017-12-10 18:36:12 -050016 info.insert(1, f'coro=<{coro}>')
Yury Selivanova0c1ba62016-10-28 12:52:37 -040017
18 if task._fut_waiter is not None:
Yury Selivanov6370f342017-12-10 18:36:12 -050019 info.insert(2, f'wait_for={task._fut_waiter!r}')
Yury Selivanova0c1ba62016-10-28 12:52:37 -040020 return info
21
22
23def _task_get_stack(task, limit):
24 frames = []
25 try:
26 # 'async def' coroutines
27 f = task._coro.cr_frame
28 except AttributeError:
29 f = task._coro.gi_frame
30 if f is not None:
31 while f is not None:
32 if limit is not None:
33 if limit <= 0:
34 break
35 limit -= 1
36 frames.append(f)
37 f = f.f_back
38 frames.reverse()
39 elif task._exception is not None:
40 tb = task._exception.__traceback__
41 while tb is not None:
42 if limit is not None:
43 if limit <= 0:
44 break
45 limit -= 1
46 frames.append(tb.tb_frame)
47 tb = tb.tb_next
48 return frames
49
50
51def _task_print_stack(task, limit, file):
52 extracted_list = []
53 checked = set()
54 for f in task.get_stack(limit=limit):
55 lineno = f.f_lineno
56 co = f.f_code
57 filename = co.co_filename
58 name = co.co_name
59 if filename not in checked:
60 checked.add(filename)
61 linecache.checkcache(filename)
62 line = linecache.getline(filename, lineno, f.f_globals)
63 extracted_list.append((filename, lineno, name, line))
Yury Selivanov6370f342017-12-10 18:36:12 -050064
Yury Selivanova0c1ba62016-10-28 12:52:37 -040065 exc = task._exception
66 if not extracted_list:
Yury Selivanov6370f342017-12-10 18:36:12 -050067 print(f'No stack for {task!r}', file=file)
Yury Selivanova0c1ba62016-10-28 12:52:37 -040068 elif exc is not None:
Yury Selivanov6370f342017-12-10 18:36:12 -050069 print(f'Traceback for {task!r} (most recent call last):', file=file)
Yury Selivanova0c1ba62016-10-28 12:52:37 -040070 else:
Yury Selivanov6370f342017-12-10 18:36:12 -050071 print(f'Stack for {task!r} (most recent call last):', file=file)
72
Yury Selivanova0c1ba62016-10-28 12:52:37 -040073 traceback.print_list(extracted_list, file=file)
74 if exc is not None:
75 for line in traceback.format_exception_only(exc.__class__, exc):
76 print(line, file=file, end='')