This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add a command line flag to suppress default signal handlers
Type: enhancement Stage:
Components: Interpreter Core Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: abacabadabacaba, martin.panter, r.david.murray
Priority: normal Keywords:

Created on 2015-05-21 22:14 by abacabadabacaba, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg243795 - (view) Author: Evgeny Kapun (abacabadabacaba) Date: 2015-05-21 22:14
Currently, Python always changes handling of certain signals on startup: SIGPIPE is ignored, and SIGINT is handled by a function that raises KeyboardInterrupt exception. As a result, if the user presses Ctrl-C, a backtrace is printed to stderr.

Some program may want to change this behavior. For example, they may want to print a user-friendly message, or to terminate without printing anything. They may do that by two means: either by installing their own handler for SIGINT, or by using a try..except block to handle the KeyboardInterrupt exception. However, they both have a flaw: if SIGINT is received during startup, before the program begins execution, the ugly KeyboardInterrupt backtrace is printed nevertheless. So, I propose to add a way to stop Python from installing this default SIGINT handler. I think that the most obvious way is to add a command line switch.

This switch should prevent Python from installing its SIGINT handler. It may also prevent it from ignoring SIGPIPE, so that all signal actions would stay the same as when the program was executed. After that, programs may use signal module to set up their own handlers, if that is desired.
msg243796 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-21 23:34
See also issue 14228.
msg243798 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-05-22 01:25
I have often wished for Python to have two modes:

1. Programming development mode: something like “python -b -Wdefault”, enabling warnings, printing full trackbacks for SIGINT and EPIPE errors, etc. Also possibly catching SystemExit() when raised inside the interactive interpreter.

2. End-user mode: hide deprecation warnings, no traceback for SIGINT and EPIPE errors (just set an exit status or maybe reraise using the default OS signal handler to set the right exit signal flag to the parent process).

Among other things, this would allow easily switching between reporting KeyboardInterrupt tracebacks (whether triggered at startup or by the main code), and hiding them.
History
Date User Action Args
2022-04-11 14:58:17adminsetgithub: 68449
2015-05-22 01:25:15martin.pantersetnosy: + martin.panter
messages: + msg243798
2015-05-21 23:34:42r.david.murraysetnosy: + r.david.murray
messages: + msg243796
2015-05-21 22:14:39abacabadabacabacreate