Feature Request: Expose Handling Frame Information in Python's Traceback Module
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
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:
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先閱讀與 traceback 相關的 API 和 sys.exc_info,並以 show_something 和 raise_another_exception 範例作為需要理解的行為。查看連結的 PR gh-127021,了解現有工作。完成的標準是,與 traceback 相關的 API 以明確且可用的方式公開鏈式例外的處理 frame。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend-api-design
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 15/100