python / python/cpython

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

オープン
#130,713 コメント 11 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue が関連するエントリポイントとして特定している CPython の _Py_HandleSystemExitAndKeyboardInterrupt から始め、単独の例外を発生させるスクリプトと、BaseExceptionGroup 内で例外を発生させるスクリプトを使って、2 つの終了コードの例を再現してください。SystemExit の出力をどのように扱うべきかを決める前に、リンクされた議論を読んでください。グループ化された KeyboardInterrupt と SystemExit が、意図された単独の場合の終了動作に従えば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
operating-systems
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。