traceback: add `recent_first` and `show_lines` parameters
未关闭
@methane 已经在做这个了。
开始于 2025年6月20日。
stdlib
type-feature
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
traceback.print_exception() and other APIs showing traceback is optimized for developers looking console:
- It uses "most recent call last" order and print exception later.
- It shows source code lines. It makes the traceback huge when using large library like sqlalchemy.
On the other hand, Python applications would send traceback to observability backends like DataDog, New Relic, Grafana Loki, etc...
In such cases, "most recent first" order and shorter traceback without source are preferred because most other languages uses "most recent first" order and no source.
stdlib traceback should provide shorter and "most recent first" traceback format.
source:
import traceback
def f1():
return 1 / 0
def f2():
return f1()
try:
f2()
except ZeroDivisionError as e:
print("# default format")
traceback.print_exception(e)
print()
print("# show_lines=False")
traceback.print_exception(e, show_lines=False)
print()
print("# show_lines=False, recent_first=True")
traceback.print_exception(e, show_lines=False, recent_first=True)
output:
# default format
Traceback (most recent call last):
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 10, in <module>
f2()
~~^^
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 7, in f2
return f1()
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 4, in f1
return 1 / 0
~~^~~
ZeroDivisionError: division by zero
# show_lines=False
Traceback (most recent call last):
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 10, in <module>
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 7, in f2
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 4, in f1
ZeroDivisionError: division by zero
# show_lines=False, recent_first=True
ZeroDivisionError: division by zero
Traceback (most recent call first):
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 4, in f1
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 7, in f2
File "/Users/inada-n/work/python/cpython/tb_demo.py", line 10, in <module>
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/shorter-and-reversed-format-for-traceback/51139
Linked PRs
- gh-135752
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
评估
这个 Issue 还没有评估数据。