base_tasks.py 2.1 KB

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