From 280d5e47c85f7fc365e4ce6faf708eb1ceb7f4b9 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 27 Aug 2025 17:46:36 +0530 Subject: [PATCH 1/2] =?UTF-8?q?Revert=20"gh-134861:=20Add=20=F0=9F=8D=8CSV?= =?UTF-8?q?=20output=20format=20to=20``python=20-m=20asyncio=20ps``=20(#13?= =?UTF-8?q?7486)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ee72c95aa947e5a87308e3657b6b3983805a086e. --- Lib/asyncio/__main__.py | 5 +---- Lib/asyncio/tools.py | 5 ----- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py index 5ef2e1f9efc9ed..4b43ba5dd70514 100644 --- a/Lib/asyncio/__main__.py +++ b/Lib/asyncio/__main__.py @@ -156,10 +156,7 @@ def interrupt(self) -> None: ) ps.add_argument("pid", type=int, help="Process ID to inspect") formats = [fmt.value for fmt in TaskTableOutputFormat] - formats_to_show = [fmt for fmt in formats - if fmt != TaskTableOutputFormat.bsv.value] - ps.add_argument("--format", choices=formats, default="table", - metavar=f"{{{','.join(formats_to_show)}}}") + ps.add_argument("--format", choices=formats, default="table") pstree = subparsers.add_parser( "pstree", help="Display a tree of all pending tasks in a process" ) diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index 3887fb8ab5bf52..efa8e1844cf3d2 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -236,9 +236,6 @@ def _get_awaited_by_tasks(pid: int) -> list: class TaskTableOutputFormat(StrEnum): table = auto() csv = auto() - bsv = auto() - # 🍌SV is not just a format. It's a lifestyle. A philosophy. - # https://www.youtube.com/watch?v=RrsVi1P6n0w def display_awaited_by_tasks_table(pid, *, format=TaskTableOutputFormat.table): @@ -276,8 +273,6 @@ def _display_awaited_by_tasks_csv(table, *, format): """Print the table in CSV format""" if format == TaskTableOutputFormat.csv: delimiter = ',' - elif format == TaskTableOutputFormat.bsv: - delimiter = '\N{BANANA}' else: raise ValueError(f"Unknown output format: {format}") csv_writer = csv.writer(sys.stdout, delimiter=delimiter) From 1f0010045d857d762b9246b2b2280a69ba42eef8 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 27 Aug 2025 17:47:12 +0530 Subject: [PATCH 2/2] Revert "gh-134861: Add CSV output format to ``python -m asyncio ps`` (#134862)" This reverts commit 470cbe97a528c5f31823a0cd4e283bf02d5d11e7. --- Lib/asyncio/__main__.py | 10 ++-- Lib/asyncio/tools.py | 52 ++++--------------- ...-05-29-19-00-37.gh-issue-134861.y2-fu-.rst | 1 - 3 files changed, 12 insertions(+), 51 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2025-05-29-19-00-37.gh-issue-134861.y2-fu-.rst diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py index 4b43ba5dd70514..ff3a69d1e17297 100644 --- a/Lib/asyncio/__main__.py +++ b/Lib/asyncio/__main__.py @@ -1,6 +1,7 @@ import argparse import ast import asyncio +import asyncio.tools import concurrent.futures import contextvars import inspect @@ -10,9 +11,6 @@ import threading import types import warnings -from asyncio.tools import (TaskTableOutputFormat, - display_awaited_by_tasks_table, - display_awaited_by_tasks_tree) from _colorize import get_theme from _pyrepl.console import InteractiveColoredConsole @@ -155,8 +153,6 @@ def interrupt(self) -> None: "ps", help="Display a table of all pending tasks in a process" ) ps.add_argument("pid", type=int, help="Process ID to inspect") - formats = [fmt.value for fmt in TaskTableOutputFormat] - ps.add_argument("--format", choices=formats, default="table") pstree = subparsers.add_parser( "pstree", help="Display a tree of all pending tasks in a process" ) @@ -164,10 +160,10 @@ def interrupt(self) -> None: args = parser.parse_args() match args.command: case "ps": - display_awaited_by_tasks_table(args.pid, format=args.format) + asyncio.tools.display_awaited_by_tasks_table(args.pid) sys.exit(0) case "pstree": - display_awaited_by_tasks_tree(args.pid) + asyncio.tools.display_awaited_by_tasks_tree(args.pid) sys.exit(0) case None: pass # continue to the interactive shell diff --git a/Lib/asyncio/tools.py b/Lib/asyncio/tools.py index efa8e1844cf3d2..2683f34cc7113b 100644 --- a/Lib/asyncio/tools.py +++ b/Lib/asyncio/tools.py @@ -1,9 +1,8 @@ """Tools to analyze tasks running in asyncio programs.""" -from collections import defaultdict -import csv +from collections import defaultdict, namedtuple from itertools import count -from enum import Enum, StrEnum, auto +from enum import Enum import sys from _remote_debugging import RemoteUnwinder, FrameInfo @@ -233,51 +232,18 @@ def _get_awaited_by_tasks(pid: int) -> list: sys.exit(1) -class TaskTableOutputFormat(StrEnum): - table = auto() - csv = auto() - - -def display_awaited_by_tasks_table(pid, *, format=TaskTableOutputFormat.table): +def display_awaited_by_tasks_table(pid: int) -> None: """Build and print a table of all pending tasks under `pid`.""" tasks = _get_awaited_by_tasks(pid) table = build_task_table(tasks) - format = TaskTableOutputFormat(format) - if format == TaskTableOutputFormat.table: - _display_awaited_by_tasks_table(table) - else: - _display_awaited_by_tasks_csv(table, format=format) - - -_row_header = ('tid', 'task id', 'task name', 'coroutine stack', - 'awaiter chain', 'awaiter name', 'awaiter id') - - -def _display_awaited_by_tasks_table(table): - """Print the table in a simple tabular format.""" - print(_fmt_table_row(*_row_header)) - print('-' * 180) + # Print the table in a simple tabular format + print( + f"{'tid':<10} {'task id':<20} {'task name':<20} {'coroutine stack':<50} {'awaiter chain':<50} {'awaiter name':<15} {'awaiter id':<15}" + ) + print("-" * 180) for row in table: - print(_fmt_table_row(*row)) - - -def _fmt_table_row(tid, task_id, task_name, coro_stack, - awaiter_chain, awaiter_name, awaiter_id): - # Format a single row for the table format - return (f'{tid:<10} {task_id:<20} {task_name:<20} {coro_stack:<50} ' - f'{awaiter_chain:<50} {awaiter_name:<15} {awaiter_id:<15}') - - -def _display_awaited_by_tasks_csv(table, *, format): - """Print the table in CSV format""" - if format == TaskTableOutputFormat.csv: - delimiter = ',' - else: - raise ValueError(f"Unknown output format: {format}") - csv_writer = csv.writer(sys.stdout, delimiter=delimiter) - csv_writer.writerow(_row_header) - csv_writer.writerows(table) + print(f"{row[0]:<10} {row[1]:<20} {row[2]:<20} {row[3]:<50} {row[4]:<50} {row[5]:<15} {row[6]:<15}") def display_awaited_by_tasks_tree(pid: int) -> None: diff --git a/Misc/NEWS.d/next/Library/2025-05-29-19-00-37.gh-issue-134861.y2-fu-.rst b/Misc/NEWS.d/next/Library/2025-05-29-19-00-37.gh-issue-134861.y2-fu-.rst deleted file mode 100644 index 07e4c61b404ba4..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-05-29-19-00-37.gh-issue-134861.y2-fu-.rst +++ /dev/null @@ -1 +0,0 @@ -Add CSV as an output format for :program:`python -m asyncio ps`.