From e4ed7eee885e44a69c98a18575ec7c7f57e2ad00 Mon Sep 17 00:00:00 2001 From: Vyron Vasileiadis Date: Fri, 7 Aug 2026 15:19:07 +0300 Subject: [PATCH] [3.14] gh-153970: Fix str() of CalledProcessError when returncode is not an integer (GH-153971) CalledProcessError.__str__() fell through to a branch that formats the return code with %d, which raises TypeError when returncode is None. (cherry picked from commit 7c653e2540cbe4180efb3bd83b63865a1f675650) Co-authored-by: Vyron Vasileiadis --- Doc/library/subprocess.rst | 4 ++-- Lib/subprocess.py | 6 +++--- Lib/test/test_subprocess.py | 10 ++++++++++ .../2026-07-22-12-00-00.gh-issue-153970.Kq7Wn3.rst | 3 +++ 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-22-12-00-00.gh-issue-153970.Kq7Wn3.rst diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst index 66a3d6a484a8a8..4be045d50d54b4 100644 --- a/Doc/library/subprocess.rst +++ b/Doc/library/subprocess.rst @@ -239,8 +239,8 @@ underlying :class:`Popen` interface can be used directly. .. attribute:: returncode - Exit status of the child process. If the process exited due to a - signal, this will be the negative signal number. + Exit status of the child process, an integer. If the process + exited due to a signal, this will be the negative signal number. .. attribute:: cmd diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 52b7b7117703d7..1ff47af3dde753 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -143,7 +143,7 @@ def __init__(self, returncode, cmd, output=None, stderr=None): self.stderr = stderr def __str__(self): - if self.returncode and self.returncode < 0: + if isinstance(self.returncode, int) and self.returncode < 0: try: return "Command '%s' died with %r." % ( self.cmd, signal.Signals(-self.returncode)) @@ -151,8 +151,8 @@ def __str__(self): return "Command '%s' died with unknown signal %d." % ( self.cmd, -self.returncode) else: - return "Command '%s' returned non-zero exit status %d." % ( - self.cmd, self.returncode) + return (f"Command '{self.cmd}' returned non-zero " + f"exit status {self.returncode}.") @property def stdout(self): diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index d13f0ec2522abd..65a3d321f44d11 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -2415,6 +2415,16 @@ def test_CalledProcessError_str_non_zero(self): error_string = str(err) self.assertIn("non-zero exit status 2.", error_string) + # returncode which is not an integer, which happens for example when + # Popen is mocked: str() must not fail + for returncode in (None, "2", 2.5, [2]): + with self.subTest(returncode=returncode): + err = subprocess.CalledProcessError(returncode, "fake cmd") + self.assertEqual( + str(err), + f"Command 'fake cmd' returned non-zero " + f"exit status {returncode}.") + 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-07-22-12-00-00.gh-issue-153970.Kq7Wn3.rst b/Misc/NEWS.d/next/Library/2026-07-22-12-00-00.gh-issue-153970.Kq7Wn3.rst new file mode 100644 index 00000000000000..def943b46b5942 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-22-12-00-00.gh-issue-153970.Kq7Wn3.rst @@ -0,0 +1,3 @@ +Calling :func:`str` on a :exc:`subprocess.CalledProcessError` no longer +raises :exc:`TypeError` when its :attr:`!returncode` is not an integer, such +as ``None``.