-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-153970: Fix str() of CalledProcessError when returncode is not an integer #153971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fedonman
wants to merge
12
commits into
python:main
Choose a base branch
from
fedonman:fix-gh-153970-calledprocesserror-none
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+18
−5
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e4ebd4f
gh-153970: Fix CalledProcessError.__str__ crash when returncode is None
fedonman 7162066
change implementation per comment by vstinner
fedonman a6d21b9
Merge branch 'main' into fix-gh-153970-calledprocesserror-none
fedonman fa1a5d6
gh-153970: Align test and NEWS with the f-string message
fedonman da53d9b
Merge branch 'fix-gh-153970-calledprocesserror-none' of github.com:fe…
fedonman 8dbfbe3
Merge branch 'main' into fix-gh-153970-calledprocesserror-none
fedonman f3bd991
gh-153970: Document that CalledProcessError.returncode is an integer
fedonman 5dffa29
Merge branch 'main' into fix-gh-153970-calledprocesserror-none
fedonman 19a86c2
gh-153970: Validate the returncode type in CalledProcessError
fedonman 46d6a68
Merge branch 'main' into fix-gh-153970-calledprocesserror-none
fedonman 0aea374
Merge branch 'main' into fix-gh-153970-calledprocesserror-none
fedonman fdad6bd
gh-153970: Make str() of CalledProcessError work for any returncode
fedonman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-07-22-12-00-00.gh-issue-153970.Kq7Wn3.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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``. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This two lines changes is the only needed change IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then we are still having the possibility of the TypeError. In this case I would rather just document that we need an error code that is an integer and leave it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind to elaborate which operation would raise TypeError? The issue is about
str()and the change above does fixstr().With the change above,
CalledProcessErrorcan be called with anyreturncode, it just works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue with <0 branch. If you pass [1] you get a type error there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I'm sorry, I completely missed the error on
self.returncode < 0.Well, in that case, I suggest replacing:
if self.returncode and self.returncode < 0:with:
if isinstance(self.returncode, int) and self.returncode < 0:IMO we should accept any returncode value in the CalledProcessError constructor and
str()should not fail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's slightly awkward for me to accept arbitrary return codes. I would assume them to be integers, and not None or other things, and would rather change the docs. In the first place, the class signature is not exposed so I don't consider it as part of the public API, whether subclassing or not.
But, if you really want to support arbitrary types, I'm ok with this change (+ the
__repr__one), but I really think it's a bit too niche.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can get a type other than int if there is a bug in subprocess under specific conditions. In that case, I would prefer to be able to create CalledProcessError which stores the full process state (returncode, stdout, stderr), rather than raising a new exception (ex: TypeError) which removes all subprocess context. Running a subprocess is an expensive operation, and you might not able to get the same stdout/stderr if you run the same command twice which would make debugging way harder.
You can also get a non-int returncode when mocking the Popen object. Example:
Output:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, the mocking argument convinces me.