|
11 | 11 |
|
12 | 12 | __all__ = ( |
13 | 13 | 'capture_call_graph', |
| 14 | + 'format_call_graph', |
14 | 15 | 'print_call_graph', |
15 | 16 | 'FrameCallGraphEntry', |
16 | 17 | 'FutureCallGraph', |
@@ -156,13 +157,15 @@ def capture_call_graph( |
156 | 157 | return FutureCallGraph(future, call_stack, awaited_by) |
157 | 158 |
|
158 | 159 |
|
159 | | -def print_call_graph( |
| 160 | +def format_call_graph( |
160 | 161 | *, |
161 | 162 | future: futures.Future | None = None, |
162 | | - file: typing.TextIO | None = None, |
163 | 163 | depth: int = 1, |
164 | | -) -> None: |
165 | | - """Print async call graph for the current task or the provided Future.""" |
| 164 | +) -> str: |
| 165 | + """Return async call graph as a string for `future`. |
| 166 | +
|
| 167 | + If `future` is not provided, format the call graph for the current task. |
| 168 | + """ |
166 | 169 |
|
167 | 170 | def render_level(st: FutureCallGraph, buf: list[str], level: int) -> None: |
168 | 171 | def add_line(line: str) -> None: |
@@ -228,9 +231,17 @@ def add_line(line: str) -> None: |
228 | 231 | try: |
229 | 232 | buf: list[str] = [] |
230 | 233 | render_level(graph, buf, 0) |
231 | | - rendered = '\n'.join(buf) |
232 | | - print(rendered, file=file) |
| 234 | + return '\n'.join(buf) |
233 | 235 | finally: |
234 | 236 | # 'graph' has references to frames so we should |
235 | 237 | # make sure it's GC'ed as soon as we don't need it. |
236 | 238 | del graph |
| 239 | + |
| 240 | +def print_call_graph( |
| 241 | + *, |
| 242 | + future: futures.Future | None = None, |
| 243 | + file: typing.TextIO | None = None, |
| 244 | + depth: int = 1, |
| 245 | +) -> None: |
| 246 | + """Print async call graph for the current task or the provided Future.""" |
| 247 | + print(format_call_graph(future=future, depth=depth), file=file) |
0 commit comments