python / python/cpython

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

Open
#130,713 11 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with CPython's _Py_HandleSystemExitAndKeyboardInterrupt, which the issue identifies as the relevant entry point, and reproduce the two exit-code examples using a script that raises a bare exception and one inside a BaseExceptionGroup. Read the linked discussion before deciding how SystemExit output should be handled; done means grouped KeyboardInterrupt and SystemExit follow the intended standalone exit behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.