Skip to content

Commit b0da7c7

Browse files
authored
gh-156327: Fix asyncio.print_call_graph() reporting its own frame (#156329)
1 parent a175da7 commit b0da7c7

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

Lib/asyncio/graph.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,5 @@ def print_call_graph(
274274
limit: int | None = None,
275275
) -> None:
276276
"""Print the async call graph for the current task or the provided Future."""
277-
print(format_call_graph(future, depth=depth, limit=limit), file=file)
277+
# gh-156327: print_call_graph() must not report its own frame
278+
print(format_call_graph(future, depth=depth + 1, limit=limit), file=file)

Lib/test/test_asyncio/test_graph.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def walk(s):
4141
return ret
4242

4343
buf = io.StringIO()
44-
asyncio.print_call_graph(fut, file=buf, depth=depth+1)
44+
asyncio.print_call_graph(fut, file=buf, depth=depth)
4545

4646
stack = asyncio.capture_call_graph(fut, depth=depth)
4747
return walk(stack), buf.getvalue()
@@ -484,6 +484,14 @@ def test_capture_call_graph_non_future(self):
484484
with self.assertRaises(TypeError):
485485
asyncio.capture_call_graph("not a future")
486486

487+
async def test_print_call_graph_innermost_frame(self):
488+
# gh-156327: print_call_graph() must not report its own frame
489+
buf = io.StringIO()
490+
lineno = sys._getframe().f_lineno + 1
491+
asyncio.print_call_graph(file=buf)
492+
first_frame = buf.getvalue().splitlines()[2]
493+
self.assertIn(f'File {__file__!r}, line {lineno},', first_frame)
494+
487495
async def test_call_graph_finished_task(self):
488496
# gh-156408: the call graph must not record a finished coroutine's None frame
489497
async def boom():
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`asyncio.print_call_graph` including its own frame in the printed
2+
call stack.

0 commit comments

Comments
 (0)