MCPcopy Index your code
hub / github.com/python/cpython / _format_coroutine

Function _format_coroutine

Lib/asyncio/coroutines.py:57–118  ·  view source on GitHub ↗
(coro)

Source from the content-addressed store, hash-verified

55
56
57def _format_coroutine(coro):
58 assert iscoroutine(coro)
59
60 def get_name(coro):
61 # Coroutines compiled with Cython sometimes don't have
62 # proper __qualname__ or __name__. While that is a bug
63 # in Cython, asyncio shouldn't crash with an AttributeError
64 # in its __repr__ functions.
65 if hasattr(coro, '__qualname__') and coro.__qualname__:
66 coro_name = coro.__qualname__
67 elif hasattr(coro, '__name__') and coro.__name__:
68 coro_name = coro.__name__
69 else:
70 # Stop masking Cython bugs, expose them in a friendly way.
71 coro_name = f'<{type(coro).__name__} without __name__>'
72 return f'{coro_name}()'
73
74 def is_running(coro):
75 try:
76 return coro.cr_running
77 except AttributeError:
78 try:
79 return coro.gi_running
80 except AttributeError:
81 return False
82
83 coro_code = None
84 if hasattr(coro, 'cr_code') and coro.cr_code:
85 coro_code = coro.cr_code
86 elif hasattr(coro, 'gi_code') and coro.gi_code:
87 coro_code = coro.gi_code
88
89 coro_name = get_name(coro)
90
91 if not coro_code:
92 # Built-in types might not have __qualname__ or __name__.
93 if is_running(coro):
94 return f'{coro_name} running'
95 else:
96 return coro_name
97
98 coro_frame = None
99 if hasattr(coro, 'gi_frame') and coro.gi_frame:
100 coro_frame = coro.gi_frame
101 elif hasattr(coro, 'cr_frame') and coro.cr_frame:
102 coro_frame = coro.cr_frame
103
104 # If Cython's coroutine has a fake code object without proper
105 # co_filename -- expose that.
106 filename = coro_code.co_filename or '<empty co_filename>'
107
108 lineno = 0
109
110 if coro_frame is not None:
111 lineno = coro_frame.f_lineno
112 coro_repr = f'{coro_name} running at {filename}:{lineno}'
113
114 else:

Callers

nothing calls this directly

Calls 3

get_nameFunction · 0.85
is_runningFunction · 0.85
iscoroutineFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…