Skip to content

gh-153970: Fix CalledProcessError.__str__ crash when returncode is None#153971

Open
fedonman wants to merge 5 commits into
python:mainfrom
fedonman:fix-gh-153970-calledprocesserror-none
Open

gh-153970: Fix CalledProcessError.__str__ crash when returncode is None#153971
fedonman wants to merge 5 commits into
python:mainfrom
fedonman:fix-gh-153970-calledprocesserror-none

Conversation

@fedonman

@fedonman fedonman commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Calling str() on a CalledProcessError raised TypeError when its returncode was None. The __str__ method fell through to a branch that formats the return code with %d, and %d can't format None, so building the error message crashed.

This adds a check for returncode is None that returns "Command ... returned an unknown exit status." instead. The other cases, negative return codes for signals and positive ones for exit codes, are unchanged.

I extended the existing test_CalledProcessError_str test with a None case.

Found as item 9 in devdanzin's standard library audit: https://gist.github.com/devdanzin/3198710e3c0128fda5e0a7b4e0768e5f

Fixes #153970.

… is None

CalledProcessError.__str__ fell through to a branch that formats the
return code with %d, which raises TypeError when returncode is None.
Handle the None case explicitly and return a message reporting an
unknown exit status.
@picnixz

picnixz commented Jul 18, 2026

Copy link
Copy Markdown
Member

See my reservations on the issue; I don't think the constructor is a public API and thus it's a case of GIGO (garbage-in, garbage-out)

@fedonman

Copy link
Copy Markdown
Contributor Author

See my reservations on the issue; I don't think the constructor is a public API and thus it's a case of GIGO (garbage-in, garbage-out)

Thank you for reviewing this. Answered on the Issue thread.

@vstinner

Copy link
Copy Markdown
Member

I don't think that there is a reason to format the returncode using %d. The issue can be addressed by using a f-string, without checking self.returncode:

diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 6fe2ec98fb4..df23fb18a51 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -151,8 +151,8 @@ def __str__(self):
                 return "Command %r died with unknown signal %d." % (
                         self.cmd, -self.returncode)
         else:
-            return "Command %r returned non-zero exit status %d." % (
-                    self.cmd, self.returncode)
+            return (f"Command {self.cmd!r} returned non-zero "
+                    f"exit status {self.returncode}.")
 
     @property
     def stdout(self):

@picnixz

picnixz commented Jul 18, 2026

Copy link
Copy Markdown
Member

I guess this patch would be acceptable indeed.

fedonman added 4 commits July 19, 2026 02:49
The implementation now formats a None returncode with an f-string
("returned non-zero exit status None.") rather than a dedicated branch,
so update the test assertion and the NEWS wording to match.
…com:fedonman/cpython into fix-pythongh-153970-calledprocesserror-none
@fedonman

Copy link
Copy Markdown
Contributor Author

Thank you. I implemented the requested changes.

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, after sleeping a bit, we would still have an issue if someone is using a returncode as a string for instance (just because self.returncode < 0 would raise here). Since we are considering only theoretical usages, I really wonderf whether we need the change.

This change would make None supported, but would still fail on truthy returncodes not comparable to 0.

So... I'd say:

  • either check returncode type in the constructor
  • do nothing but reword the docs a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subprocess: CalledProcessError.__str__ crashes when returncode is None

3 participants