Skip to content

Commit d7c4e69

Browse files
Graceful exit on KeyboardInterrupt (#10725)
Catch KeyboardInterrupt to allow graceful exit instead of printing the traceback. Adds options parsing to the KeyboardInterrupt handler. At the moment the traceback will only be printed on --show-traceback but can easily extend it to -v as well. Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 89bb94a commit d7c4e69

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

mypy/__main__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Mypy type checker command line tool."""
2-
3-
import sys
42
import os
3+
import sys
4+
import traceback
55

6-
from mypy.main import main
6+
from mypy.main import main, process_options
7+
from mypy.util import FancyFormatter
78

89

910
def console_entry() -> None:
@@ -17,6 +18,16 @@ def console_entry() -> None:
1718
devnull = os.open(os.devnull, os.O_WRONLY)
1819
os.dup2(devnull, sys.stdout.fileno())
1920
sys.exit(2)
21+
except KeyboardInterrupt:
22+
_, options = process_options(args=sys.argv[1:])
23+
if options.show_traceback:
24+
sys.stdout.write(traceback.format_exc())
25+
formatter = FancyFormatter(sys.stdout, sys.stderr, False)
26+
msg = "Interrupted\n"
27+
sys.stdout.write(formatter.style(msg, color="red", bold=True))
28+
sys.stdout.flush()
29+
sys.stderr.flush()
30+
sys.exit(2)
2031

2132

2233
if __name__ == '__main__':

0 commit comments

Comments
 (0)