1
- """Introspection utils for tasks call stacks ."""
1
+ """Introspection utils for tasks call graphs ."""
2
2
3
3
import dataclasses
4
4
import sys
18
18
19
19
# Sadly, we can't re-use the traceback's module datastructures as those
20
20
# are tailored for error reporting, whereas we need to represent an
21
- # async call stack .
21
+ # async call graph .
22
22
#
23
23
# Going with pretty verbose names as we'd like to export them to the
24
24
# top level asyncio namespace, and want to avoid future name clashes.
@@ -36,7 +36,7 @@ class FutureCallGraph:
36
36
awaited_by : list [FutureCallGraph ]
37
37
38
38
39
- def _build_stack_for_future (future : futures .Future ) -> FutureCallGraph :
39
+ def _build_graph_for_future (future : futures .Future ) -> FutureCallGraph :
40
40
if not isinstance (future , futures .Future ):
41
41
raise TypeError (
42
42
f"{ future !r} object does not appear to be compatible "
@@ -64,7 +64,7 @@ def _build_stack_for_future(future: futures.Future) -> FutureCallGraph:
64
64
65
65
if future ._asyncio_awaited_by :
66
66
for parent in future ._asyncio_awaited_by :
67
- awaited_by .append (_build_stack_for_future (parent ))
67
+ awaited_by .append (_build_graph_for_future (parent ))
68
68
69
69
st .reverse ()
70
70
return FutureCallGraph (future , st , awaited_by )
@@ -75,9 +75,9 @@ def capture_call_graph(
75
75
future : futures .Future | None = None ,
76
76
depth : int = 1 ,
77
77
) -> FutureCallGraph | None :
78
- """Capture async call stack for the current task or the provided Future.
78
+ """Capture async call graph for the current task or the provided Future.
79
79
80
- The stack is represented with three data structures:
80
+ The graph is represented with three data structures:
81
81
82
82
* FutureCallGraph(future, call_stack, awaited_by)
83
83
@@ -109,7 +109,7 @@ def capture_call_graph(
109
109
# if yes - check if the passed future is the currently
110
110
# running task or not.
111
111
if loop is None or future is not tasks .current_task (loop = loop ):
112
- return _build_stack_for_future (future )
112
+ return _build_graph_for_future (future )
113
113
# else: future is the current task, move on.
114
114
else :
115
115
if loop is None :
@@ -151,7 +151,7 @@ def capture_call_graph(
151
151
awaited_by = []
152
152
if future ._asyncio_awaited_by :
153
153
for parent in future ._asyncio_awaited_by :
154
- awaited_by .append (_build_stack_for_future (parent ))
154
+ awaited_by .append (_build_graph_for_future (parent ))
155
155
156
156
return FutureCallGraph (future , call_stack , awaited_by )
157
157
@@ -162,7 +162,7 @@ def print_call_graph(
162
162
file : typing .TextIO | None = None ,
163
163
depth : int = 1 ,
164
164
) -> None :
165
- """Print async call stack for the current task or the provided Future."""
165
+ """Print async call graph for the current task or the provided Future."""
166
166
167
167
def render_level (st : FutureCallGraph , buf : list [str ], level : int ) -> None :
168
168
def add_line (line : str ) -> None :
@@ -221,16 +221,16 @@ def add_line(line: str) -> None:
221
221
for fut in st .awaited_by :
222
222
render_level (fut , buf , level + 1 )
223
223
224
- stack = capture_call_graph (future = future , depth = depth + 1 )
225
- if stack is None :
224
+ graph = capture_call_graph (future = future , depth = depth + 1 )
225
+ if graph is None :
226
226
return
227
227
228
228
try :
229
229
buf : list [str ] = []
230
- render_level (stack , buf , 0 )
230
+ render_level (graph , buf , 0 )
231
231
rendered = '\n ' .join (buf )
232
232
print (rendered , file = file )
233
233
finally :
234
- # 'stack ' has references to frames so we should
234
+ # 'graph ' has references to frames so we should
235
235
# make sure it's GC'ed as soon as we don't need it.
236
- del stack
236
+ del graph
0 commit comments