python / python/cpython

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

Abierto
#126,935 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza leyendo las API relacionadas con traceback y sys.exc_info, usando el ejemplo show_something y raise_another_exception como el comportamiento que se debe comprender. Revisa el PR enlazado gh-127021 para consultar el trabajo existente. Se considera terminado cuando la API relacionada con traceback expone el frame de manejo para excepciones encadenadas de una forma definida y utilizable.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend-api-design
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
15/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.