Remove duplicate code by making `traceback.print_list()` delegate to `format_list()`
还没有人认领这个 Issue。
评估
调研方向
从 Lib/traceback.py 开始,比较 print_list() 和 format_list() 这两个入口点及其当前的格式化路径。当 print_list() 委托给 format_list(),且不改变输出或文件处理时,即表示完成;该 issue 链接到了 PR gh-153783,因此请先检查这项工作,再继续进行。
由索引模型根据 Issue 内容生成。
描述
In Lib/traceback.py, the public helpers print_list() and format_list() independently contain the same formatting expression, so their outputs agree only by duplication. We propose routing print_list() through format_list() so that they share a single formatting path, with no change in behavior.
Problem
format_list() returns the formatted lines:
def format_list(extracted_list):
return StackSummary.from_list(extracted_list).format()
print_list() writes those same lines, but re-derives them with the identical expression instead of reusing format_list():
def print_list(extracted_list, file=None):
if file is None:
file = sys.stderr
for item in StackSummary.from_list(extracted_list).format():
print(item, file=file, end="")
The outputs match only because StackSummary.from_list(extracted_list).format() is duplicated in both. Any future change to how a frame list is formatted has to be applied in both places to prevent the two public helpers from silently diverging.
Solution
Have print_list() iterate format_list():
def print_list(extracted_list, file=None):
if file is None:
file = sys.stderr
for item in format_list(extracted_list):
print(item, file=file, end="")
This eliminates the possibility of drift and mirrors the delegation already used in this module; for example, print_tb() calls print_list() rather than re-deriving extract_tb(...).format().
Linked PRs
- gh-153783
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
docs pending
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 72/100
-
build type-bug
难度 2/5 1-3 小时 新手友好度 76/100
-
stdlib topic-email type-feature
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
fix: inaccuracy ⚠️
难度 2/5 1-3 小时 新手友好度 72/100
uabrc/uabrc.github.io#1255 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
ethereum-optimism/factory#64 ·
-
难度 2/5 1-3 小时 新手友好度 90/100
duckdb/duckdb-python#627 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
-
documentation
难度 1/5 1 小时以内 新手友好度 78/100
Qiskit/qiskit-addon-sqd#376 ·