Skip to content

Commit

Permalink
Graceful exit on KeyboardInterrupt (#10725)
Browse files Browse the repository at this point in the history
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 <jelle.zijlstra@gmail.com>
  • Loading branch information
cdce8p and JelleZijlstra committed Nov 19, 2021
1 parent 89bb94a commit d7c4e69
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions mypy/__main__.py
@@ -1,9 +1,10 @@
"""Mypy type checker command line tool."""

import sys
import os
import sys
import traceback

from mypy.main import main
from mypy.main import main, process_options
from mypy.util import FancyFormatter


def console_entry() -> None:
Expand All @@ -17,6 +18,16 @@ def console_entry() -> None:
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(2)
except KeyboardInterrupt:
_, options = process_options(args=sys.argv[1:])
if options.show_traceback:
sys.stdout.write(traceback.format_exc())
formatter = FancyFormatter(sys.stdout, sys.stderr, False)
msg = "Interrupted\n"
sys.stdout.write(formatter.style(msg, color="red", bold=True))
sys.stdout.flush()
sys.stderr.flush()
sys.exit(2)


if __name__ == '__main__':
Expand Down

0 comments on commit d7c4e69

Please sign in to comment.