python / python/cpython

traceback: add `recent_first` and `show_lines` parameters

Offen
#135,751 4 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@methane arbeitet bereits daran.

Seit 20.6.2025.

stdlib type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.