Improve the default excepthook formatting for multi-line and exception notes
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Proposal:
Currently, it is possible to add notes to exceptions, see BaseException.add_note, but this feature is underutilized.
One of the reasons for that, I believe, is the poor formatting in the default excepthook:
Current Output
(single note)
$ ./python
Python 3.14.0a2+ experimental free-threading build (heads/gh-127429-dirty:cc4f57a74d0, Dec 1 2024, 07:06:51) [GCC 14.2.1 20240910] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
... raise Exception('error')
... except Exception as e:
... e.add_note('some context')
... raise
...
Traceback (most recent call last):
File "<python-input-0>", line 2, in <module>
raise Exception('error')
Exception: error
some context
(with multiple notes)
$ ./python
Python 3.14.0a2+ experimental free-threading build (heads/gh-127429-dirty:cc4f57a74d0, Dec 1 2024, 07:06:51) [GCC 14.2.1 20240910] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
... raise Exception('error')
... except Exception as e:
... e.add_note('some context')
... e.add_note('some context 2')
... e.add_note('some context 3')
... raise
...
Traceback (most recent call last):
File "<python-input-0>", line 2, in <module>
raise Exception('error')
Exception: error
some context
some context 2
some context 3
I think if we formatted it better, making the output easier to understand, we could start seeing better adoption of this feature.
(with multiple notes)
$ ./python
Python 3.14.0a2+ experimental free-threading build (heads/gh-127429-dirty:cc4f57a74d0, Dec 1 2024, 07:06:51) [GCC 14.2.1 20240910] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
... raise Exception('error')
... except Exception as e:
... e.add_note('some context')
... e.add_note('some context 2')
... e.add_note('some context 3')
... raise
...
Traceback (most recent call last):
File "<python-input-0>", line 2, in <module>
raise Exception('error')
Exception: error
├── Note: some context
├── Note: some context 2
└── Note: some context 3
In addition to this, multiline exception messages could also be improved.
Current Output
(multi-line string, with no notes)
$./python
Python 3.14.0a2+ experimental free-threading build (heads/gh-127429-dirty:cc4f57a74d0, Dec 1 2024, 07:06:51) [GCC 14.2.1 20240910] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> raise Exception('some error\n\nsome context\nsome context (cont.)\n\nsome extra context')
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
raise Exception('some error\n\nsome context\nsome context (cont.)\n\nsome extra context')
Exception: some error
some context
some context (cont.)
some extra context
(multi-line string, with multi-line notes)
$ ./python
Python 3.14.0a2+ experimental free-threading build (heads/gh-127429-dirty:cc4f57a74d0, Dec 1 2024, 07:06:51) [GCC 14.2.1 20240910] on linux
>>> try:
... raise Exception('some error\n\nsome error text\nsome error text (cont.)\n\nsome more error text')
... except Exception as e:
... e.add_note('some context\nsome context (cont.)')
... e.add_note('some context 2\n\nsome extra context 2')
... e.add_note('some context 3\nsome context 3 (cont.)\n\nsome extra context 3')
... raise
...
Traceback (most recent call last):
File "<python-input-0>", line 2, in <module>
raise Exception('some error\n\nsome error text\nsome error text (cont.)\n\nsome more error text')
Exception: some error
some error text
some error text (cont.)
some more error text
some context
some context (cont.)
some context 2
some extra context 2
some context 3
some context 3 (cont.)
some extra context 3
The first example IMO isn't optimal, but it's okay enough, but the second example is very confusing. We could format it something like:
$ ./python
Python 3.14.0a2+ experimental free-threading build (heads/gh-127429-dirty:cc4f57a74d0, Dec 1 2024, 07:06:51) [GCC 14.2.1 20240910] on linux
>>> try:
... raise Exception('some error\n\nsome error text\nsome error text (cont.)\n\nsome more error text')
... except Exception as e:
... e.add_note('some context\nsome context (cont.)')
... e.add_note('some context 2\n\nsome extra context 2')
... e.add_note('some context 3\nsome context 3 (cont.)\n\nsome extra context 3')
... raise
...
Traceback (most recent call last):
File "<python-input-0>", line 2, in <module>
raise Exception('some error\n\nsome error text\nsome error text (cont.)\n\nsome more error text')
Exception:
│ │ some error
│ │
│ │ some error text
│ │ some error text (cont.)
│ │
│ └ some more error text
│
├── Note:
│ │ some context
│ └ some context (cont.)
│
├── Note:
│ │ some context 2
│ │
│ └ some extra context 2
│
└── Note:
│ some context 3
│ some context 3 (cont.)
│
└ some extra context 3
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 CPython 的默认 excepthook 和异常格式化入口开始;该 issue 没有指定文件或测试。将当前输出与提议的单行和多行 note 示例进行比较,并将实现预期的树状格式且不丢失消息内容视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100