python / python/cpython

Improve the default excepthook formatting for multi-line and exception notes

Aberta
#127,596 6 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

interpreter-core type-feature
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece pelos pontos de entrada padrão do CPython para excepthook e formatação de exceções; a issue não nomeia um arquivo nem um teste. Compare a saída atual com os exemplos propostos de notas de uma linha e de várias linhas e considere concluído quando a formatação em árvore pretendida for obtida sem perder o conteúdo da mensagem.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
backend
Tipo de issue
Funcionalidade
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.