Skip to content

Commit 8bdfcea

Browse files
committed
Reintroduce _row_header, add _fmt_table_row() utility
1 parent 637e7f8 commit 8bdfcea

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

Lib/asyncio/__main__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import argparse
22
import ast
33
import asyncio
4-
import asyncio.tools
54
import concurrent.futures
65
import contextvars
76
import inspect
@@ -11,6 +10,9 @@
1110
import threading
1211
import types
1312
import warnings
13+
from asyncio.tools import (TaskTableOutputFormat,
14+
display_awaited_by_tasks_table,
15+
display_awaited_by_tasks_tree)
1416

1517
from _colorize import get_theme
1618
from _pyrepl.console import InteractiveColoredConsole
@@ -153,7 +155,7 @@ def interrupt(self) -> None:
153155
"ps", help="Display a table of all pending tasks in a process"
154156
)
155157
ps.add_argument("pid", type=int, help="Process ID to inspect")
156-
formats = [fmt.value for fmt in asyncio.tools.TaskTableOutputFormat]
158+
formats = [fmt.value for fmt in TaskTableOutputFormat]
157159
ps.add_argument("--format", choices=formats, default="table")
158160
pstree = subparsers.add_parser(
159161
"pstree", help="Display a tree of all pending tasks in a process"
@@ -162,10 +164,10 @@ def interrupt(self) -> None:
162164
args = parser.parse_args()
163165
match args.command:
164166
case "ps":
165-
asyncio.tools.display_awaited_by_tasks_table(args.pid, args.format)
167+
display_awaited_by_tasks_table(args.pid, format=args.format)
166168
sys.exit(0)
167169
case "pstree":
168-
asyncio.tools.display_awaited_by_tasks_tree(args.pid)
170+
display_awaited_by_tasks_tree(args.pid)
169171
sys.exit(0)
170172
case None:
171173
pass # continue to the interactive shell

Lib/asyncio/tools.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Tools to analyze tasks running in asyncio programs."""
22

3-
from collections import defaultdict, namedtuple
43
import csv
5-
from itertools import count
6-
from enum import Enum, StrEnum, auto
74
import sys
85
from _remote_debugging import RemoteUnwinder, FrameInfo
6+
from collections import defaultdict
7+
from enum import Enum, StrEnum, auto
8+
from itertools import chain, count
99

1010

1111
class NodeType(Enum):
@@ -248,28 +248,36 @@ def display_awaited_by_tasks_table(pid, *, format=TaskTableOutputFormat.table):
248248
if format == TaskTableOutputFormat.table:
249249
_display_awaited_by_tasks_table(table)
250250
else:
251-
_display_awaited_by_tasks_csv(table, format)
251+
_display_awaited_by_tasks_csv(table, format=format)
252+
252253

254+
_row_header = ('tid', 'task id', 'task name', 'coroutine stack',
255+
'awaiter chain', 'awaiter name', 'awaiter id')
253256

254-
def _display_awaited_by_tasks_table(table) -> None:
255-
# Print the table in a simple tabular format
256-
print(
257-
f"{'tid':<10} {'task id':<20} {'task name':<20} {'coroutine stack':<50} {'awaiter chain':<50} {'awaiter name':<15} {'awaiter id':<15}"
258-
)
259-
print("-" * 180)
257+
258+
def _display_awaited_by_tasks_table(table):
259+
"""Print the table in a simple tabular format."""
260+
print(_fmt_table_row(*_row_header))
261+
print('-' * 180)
260262
for row in table:
261-
print(f"{row[0]:<10} {row[1]:<20} {row[2]:<20} {row[3]:<50} {row[4]:<50} {row[5]:<15} {row[6]:<15}")
263+
print(_fmt_table_row(*row))
264+
265+
266+
def _fmt_table_row(tid, task_id, task_name, coro_stack,
267+
awaiter_chain, awaiter_name, awaiter_id):
268+
# Format a single row for the table format
269+
return (f'{tid:<10} {task_id:<20} {task_name:<20} {coro_stack:<50} '
270+
f'{awaiter_chain:<50} {awaiter_name:<15} {awaiter_id:<15}')
262271

263272

264-
def _display_awaited_by_tasks_csv(table, format: TaskTableOutputFormat) -> None:
265-
csv_header = ('tid', 'task id', 'task name', 'coroutine stack',
266-
'awaiter chain', 'awaiter name', 'awaiter id')
273+
def _display_awaited_by_tasks_csv(table, *, format):
274+
"""Print the table in CSV format"""
267275
if format == TaskTableOutputFormat.csv:
268276
delimiter = ','
269277
else:
270278
raise ValueError(f"Unknown output format: {format}")
271279
csv_writer = csv.writer(sys.stdout, delimiter=delimiter)
272-
csv_writer.writerow(csv_header)
280+
csv_writer.writerow(_row_header)
273281
csv_writer.writerows(table)
274282

275283

0 commit comments

Comments
 (0)