From 6f12ea3e1d1d43b1cb721e1ab85708e5f645c6d3 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Mon, 29 Jun 2026 16:35:18 +0800 Subject: [PATCH 1/2] gh-152558: Avoid redundant quoting in :exc:`subprocess.TimeoutExpired` messages. --- Lib/subprocess.py | 2 +- Lib/test/test_subprocess.py | 13 +++++++++++++ .../2026-06-29-16-30-58.gh-issue-152558.CCfp-R.rst | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-29-16-30-58.gh-issue-152558.CCfp-R.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 6fe2ec98fb4088..43f75321ab75c3 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -180,7 +180,7 @@ def __init__(self, cmd, timeout, output=None, stderr=None): self.stderr = stderr def __str__(self): - return ("Command '%s' timed out after %s seconds" % + return ("Command %r timed out after %s seconds" % (self.cmd, self.timeout)) @property diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index d066ae85dfc51a..098af792bcc86d 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -2449,6 +2449,19 @@ def test_CalledProcessError_str(self): err = subprocess.CalledProcessError(-9876543, "fake cmd") self.assertEqual(str(err), "Command 'fake cmd' died with unknown signal 9876543.") + def test_TimeoutExpired_str(self): + # timeout command string + err = subprocess.TimeoutExpired("fake cmd", 1) + self.assertEqual(str(err), "Command 'fake cmd' timed out after 1 seconds") + + # timeout command string with a single-quote + err = subprocess.TimeoutExpired("fake ' cmd", 1) + self.assertEqual(str(err), 'Command "fake \' cmd" timed out after 1 seconds') + + # timeout command list + err = subprocess.TimeoutExpired(["fake", "cmd"], 1) + self.assertEqual(str(err), "Command ['fake', 'cmd'] timed out after 1 seconds") + def test_preexec(self): # DISCLAIMER: Setting environment variables is *not* a good use # of a preexec_fn. This is merely a test. diff --git a/Misc/NEWS.d/next/Library/2026-06-29-16-30-58.gh-issue-152558.CCfp-R.rst b/Misc/NEWS.d/next/Library/2026-06-29-16-30-58.gh-issue-152558.CCfp-R.rst new file mode 100644 index 00000000000000..84cca209ee4c6b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-29-16-30-58.gh-issue-152558.CCfp-R.rst @@ -0,0 +1 @@ +Avoid redundant quoting in :exc:`subprocess.TimeoutExpired` messages. From cffcc12f9bd06ae8c79abd75a9ad98ce0022a87a Mon Sep 17 00:00:00 2001 From: lipengyu Date: Mon, 29 Jun 2026 17:15:24 +0800 Subject: [PATCH 2/2] Use f-string --- Lib/subprocess.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 43f75321ab75c3..4da883b787898a 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -180,8 +180,7 @@ def __init__(self, cmd, timeout, output=None, stderr=None): self.stderr = stderr def __str__(self): - return ("Command %r timed out after %s seconds" % - (self.cmd, self.timeout)) + return f"Command {self.cmd!r} timed out after {self.timeout} seconds" @property def stdout(self):