subprocess: `CalledProcessError.__str__` crashes when returncode is None
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 平均合併
- 1 天 9 小時
- 30 天內合併 PR
- 558
描述
Calling str() on a CalledProcessError crashes if its returncode is None.
The __str__ method uses if self.returncode and self.returncode < 0 for the "died with a signal" case, and otherwise formats the return code with %d. When returncode is None, that first check is falsy, so it falls through to the %d branch, and %d can't format None, so it raises TypeError. Having the exception's own string representation blow up is a pretty bad way to fail.
import subprocess
err = subprocess.CalledProcessError(None, "cmd")
str(err) # TypeError: %d format: a real number is required, not NoneType
The fix is to check for returncode is None before the %d branch and return a plain message instead.
Found while going through devdanzin's audit of the standard library, item 9: https://gist.github.com/devdanzin/3198710e3c0128fda5e0a7b4e0768e5f
Linked PRs
- gh-153971
- gh-155923
- gh-155924
- gh-155931
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 subprocess.CalledProcessError.str 開始,使用 None returncode 重現 issue 中的範例。為失敗的表示新增涵蓋範圍,並確認 str(err) 會傳回一則純文字訊息而不引發例外;開始前請查看已連結的 PRs,因為相關工作已列在此 issue 下。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- operating-systems
- Issue 類型
- 缺陷
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 30/100