Coverage for Lib/asyncio/base_tasks.py: 88%

77 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 05:46 +0000

1import linecache 

2import reprlib 

3import traceback 

4 

5from . import base_futures 

6from . import coroutines 

7 

8 

9def _task_repr_info(task): 

10 info = base_futures._future_repr_info(task) 

11 

12 if task.cancelling() and not task.done(): 

13 # replace status 

14 info[0] = 'cancelling' 

15 

16 info.insert(1, 'name=%r' % task.get_name()) 

17 

18 if task._fut_waiter is not None: 

19 info.insert(2, f'wait_for={task._fut_waiter!r}') 

20 

21 if task._coro: 21 ↛ 25line 21 didn't jump to line 25 because the condition on line 21 was always true

22 coro = coroutines._format_coroutine(task._coro) 

23 info.insert(2, f'coro=<{coro}>') 

24 

25 return info 

26 

27 

28@reprlib.recursive_repr() 

29def _task_repr(task): 

30 info = ' '.join(_task_repr_info(task)) 

31 return f'<{task.__class__.__name__} {info}>' 

32 

33 

34def _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'): 39 ↛ 41line 39 didn't jump to line 41 because the condition on line 39 was never true

40 # case 2: legacy coroutines 

41 f = task._coro.gi_frame 

42 elif hasattr(task._coro, 'ag_frame'): 42 ↛ 44line 42 didn't jump to line 44 because the condition on line 42 was never true

43 # case 3: async generators 

44 f = task._coro.ag_frame 

45 elif not task.done() and hasattr(task._coro, 'aga_gen'): 

46 # case 4: async generator's asend/athrow/aclose 

47 f = task._coro.aga_gen.ag_frame 

48 else: 

49 # case 5: unknown objects 

50 f = None 

51 if f is not None: 

52 while f is not None: 

53 if limit is not None: 

54 if limit <= 0: 

55 break 

56 limit -= 1 

57 frames.append(f) 

58 f = f.f_back 

59 frames.reverse() 

60 elif task._exception is not None: 

61 tb = task._exception.__traceback__ 

62 while tb is not None: 

63 if limit is not None: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 if limit <= 0: 

65 break 

66 limit -= 1 

67 frames.append(tb.tb_frame) 

68 tb = tb.tb_next 

69 return frames 

70 

71 

72def _task_print_stack(task, limit, file): 

73 exc = task._exception 

74 tb = exc.__traceback__ if exc is not None else None 

75 extracted_list = [] 

76 checked = set() 

77 for f in task.get_stack(limit=limit): 

78 if tb is not None: 

79 lineno = tb.tb_lineno 

80 tb = tb.tb_next 

81 else: 

82 lineno = f.f_lineno 

83 co = f.f_code 

84 filename = co.co_filename 

85 name = co.co_name 

86 if filename not in checked: 86 ↛ 89line 86 didn't jump to line 89 because the condition on line 86 was always true

87 checked.add(filename) 

88 linecache.checkcache(filename) 

89 line = linecache.getline(filename, lineno, f.f_globals) 

90 extracted_list.append((filename, lineno, name, line)) 

91 

92 if not extracted_list: 92 ↛ 93line 92 didn't jump to line 93 because the condition on line 92 was never true

93 print(f'No stack for {task!r}', file=file) 

94 elif exc is not None: 

95 print(f'Traceback for {task!r} (most recent call last):', file=file) 

96 else: 

97 print(f'Stack for {task!r} (most recent call last):', file=file) 

98 

99 traceback.print_list(extracted_list, file=file) 

100 if exc is not None: 

101 for line in traceback.format_exception_only(exc.__class__, exc): 

102 print(line, file=file, end='')