1 | n/a | import linecache |
---|
2 | n/a | import traceback |
---|
3 | n/a | |
---|
4 | n/a | from . import base_futures |
---|
5 | n/a | from . import coroutines |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | def _task_repr_info(task): |
---|
9 | n/a | info = base_futures._future_repr_info(task) |
---|
10 | n/a | |
---|
11 | n/a | if task._must_cancel: |
---|
12 | n/a | # replace status |
---|
13 | n/a | info[0] = 'cancelling' |
---|
14 | n/a | |
---|
15 | n/a | coro = coroutines._format_coroutine(task._coro) |
---|
16 | n/a | info.insert(1, 'coro=<%s>' % coro) |
---|
17 | n/a | |
---|
18 | n/a | if task._fut_waiter is not None: |
---|
19 | n/a | info.insert(2, 'wait_for=%r' % task._fut_waiter) |
---|
20 | n/a | return info |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | def _task_get_stack(task, limit): |
---|
24 | n/a | frames = [] |
---|
25 | n/a | try: |
---|
26 | n/a | # 'async def' coroutines |
---|
27 | n/a | f = task._coro.cr_frame |
---|
28 | n/a | except AttributeError: |
---|
29 | n/a | f = task._coro.gi_frame |
---|
30 | n/a | if f is not None: |
---|
31 | n/a | while f is not None: |
---|
32 | n/a | if limit is not None: |
---|
33 | n/a | if limit <= 0: |
---|
34 | n/a | break |
---|
35 | n/a | limit -= 1 |
---|
36 | n/a | frames.append(f) |
---|
37 | n/a | f = f.f_back |
---|
38 | n/a | frames.reverse() |
---|
39 | n/a | elif task._exception is not None: |
---|
40 | n/a | tb = task._exception.__traceback__ |
---|
41 | n/a | while tb is not None: |
---|
42 | n/a | if limit is not None: |
---|
43 | n/a | if limit <= 0: |
---|
44 | n/a | break |
---|
45 | n/a | limit -= 1 |
---|
46 | n/a | frames.append(tb.tb_frame) |
---|
47 | n/a | tb = tb.tb_next |
---|
48 | n/a | return frames |
---|
49 | n/a | |
---|
50 | n/a | |
---|
51 | n/a | def _task_print_stack(task, limit, file): |
---|
52 | n/a | extracted_list = [] |
---|
53 | n/a | checked = set() |
---|
54 | n/a | for f in task.get_stack(limit=limit): |
---|
55 | n/a | lineno = f.f_lineno |
---|
56 | n/a | co = f.f_code |
---|
57 | n/a | filename = co.co_filename |
---|
58 | n/a | name = co.co_name |
---|
59 | n/a | if filename not in checked: |
---|
60 | n/a | checked.add(filename) |
---|
61 | n/a | linecache.checkcache(filename) |
---|
62 | n/a | line = linecache.getline(filename, lineno, f.f_globals) |
---|
63 | n/a | extracted_list.append((filename, lineno, name, line)) |
---|
64 | n/a | exc = task._exception |
---|
65 | n/a | if not extracted_list: |
---|
66 | n/a | print('No stack for %r' % task, file=file) |
---|
67 | n/a | elif exc is not None: |
---|
68 | n/a | print('Traceback for %r (most recent call last):' % task, |
---|
69 | n/a | file=file) |
---|
70 | n/a | else: |
---|
71 | n/a | print('Stack for %r (most recent call last):' % task, |
---|
72 | n/a | file=file) |
---|
73 | n/a | traceback.print_list(extracted_list, file=file) |
---|
74 | n/a | if exc is not None: |
---|
75 | n/a | for line in traceback.format_exception_only(exc.__class__, exc): |
---|
76 | n/a | print(line, file=file, end='') |
---|