python / python/cpython

Feature Request: Expose Handling Frame Information in Python's Traceback Module

Aperta
#126,935 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

Proposal:

Problem: When dealing with chained exceptions in Python, there is no straightforward way to identify the "outermost handling frame": the frame in the except/finally block that starts the part of the stacktrace that's unique to the "during the handling" exception.

This information is not visible in the textual stacktrace, nor is it accessible programmatically via Python's traceback module (or similar tools).

Since this "handling frame" represents half of the link between 2 exceptions when shown in "chained mode", the fact that this information is missing makes it very hard to see how 2 chained exceptions actually relate to each other.

Proposal: Extend Python's traceback-related modules (e.g., traceback, sys.exc_info) to expose handling frame information. This would allow developers and tools to programmatically determine where exceptions are caught and re-raised in the call stack.

(It would seem to me that just changing the underlying datastructures leads to the least disruption in peoples' workflow, while still allowing for some users to extract the information if they need it)

Current Behavior:

Using the following example:

class OriginalException(Exception):
    pass

class AnotherException(Exception):
    pass

def raise_another_exception():
    raise AnotherException()

def show_something():
    try:
        raise OriginalException()
    except OriginalException:
        raise_another_exception()

show_something()

Running this code produces:

Traceback (most recent call last):
  File "example.py", line 15, in show_something
    raise OriginalException()
__main__.OriginalException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "example.py", line 20, in <module>
    show_something()
  File "example.py", line 17, in show_something
    raise_another_exception()
  File "example.py", line 10, in raise_another_exception
    raise AnotherException()
__main__.AnotherException

In this traceback, line 17 (raise_another_exception()) is the handling frame, but there is no direct way to programmatically extract this information.

Proposed Improvement:

Enhance the traceback-related API(s) to expose handling frame information.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

I wrote extensively about this problem (as well as more generally about how confusing chained stacktraces can be) on my blog.

This is obviously not actually a discussion since it doesn't involve other people, but I do think it's useful context (and it's better to keep the longer story out of the feature-request)

Linked PRs
  • gh-127021

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo le API relative a traceback e sys.exc_info, usando l’esempio show_something e raise_another_exception come comportamento da comprendere. Esamina il PR collegato gh-127021 per il lavoro esistente. Il lavoro è completato quando l’API relativa a traceback espone il frame di gestione per le eccezioni concatenate in modo definito e utilizzabile.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend-api-design
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
15/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.