python / python/cpython

Treat `KeyboardInterrupt` or `SystemExit` the same on program exit even if they are inside exception groups

未关闭
#130,713 11 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

A KeyboardInterrupt or SystemExit inside a BaseExceptionGroup should be treated like a bare KeyboardInterrupt or SystemExit.

PS C:\...> python x.py
  + Exception Group Traceback (most recent call last):
  |   File "C:\...\x.py", line 1, in <module>
  |     raise BaseExceptionGroup("ki in an exception group", [KeyboardInterrupt()])
  | BaseExceptionGroup: ki in an exception group (1 sub-exception)
  +-+---------------- 1 ----------------
    | KeyboardInterrupt
    +------------------------------------
PS C:\...> $LastExitCode
1


PS C:\...> python x.py
Traceback (most recent call last):
  File "C:\...\x.py", line 1, in <module>
    raise KeyboardInterrupt()
KeyboardInterrupt
PS C:\...> $LastExitCode
-1073741510

Looking at the CPython source code, I believe _Py_HandleSystemExitAndKeyboardInterrupt is the relevant function.


Points for it:

  • less pitfalls for a 3rd party using exception groups
  • see any other failures (as exceptions) upon shutdown (ATM the workaround requires discarding the other exceptions)

Points against:

  • SystemExit in an exception group prints out the stack trace, unlike when it's not in an exception group
    • we could make them consistent (filter out SystemExit and print the exception group then)

I volunteer to implement this if this is fine.

Has this already been discussed elsewhere?

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

EDIT: I have now made a post. See https://discuss.python.org/t/keyboardinterrupt-and-systemexit-in-exception-groups-should-be-considered-for-pythons-exit-code/82816

Links to previous discussion of this feature:

I have tried some basic keyword searches as well as some digging in blame, but I can't find any previous discussion.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 CPython 的 _Py_HandleSystemExitAndKeyboardInterrupt 开始,issue 将其标识为相关入口点,并使用一个引发单独异常的脚本和一个在 BaseExceptionGroup 内引发异常的脚本重现两个退出代码示例。在决定应如何处理 SystemExit 输出之前,先阅读链接的讨论;当分组的 KeyboardInterrupt 和 SystemExit 遵循预期的独立退出行为时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。