log: Don't print backtrace on ^c/KeyboardInterrupt (#8259)

This commit is contained in:
alexzorin 2020-09-04 20:57:46 +10:00 committed by GitHub
parent 3ce87d1fcb
commit 98615564ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View file

@ -319,6 +319,9 @@ def post_arg_parse_except_hook(exc_type, exc_value, trace, debug, log_path):
# logger.DEBUG should be used
if debug or not issubclass(exc_type, Exception):
assert constants.QUIET_LOGGING_LEVEL <= logging.ERROR
if exc_type is KeyboardInterrupt:
logger.error('Exiting due to user request.')
sys.exit(1)
logger.error('Exiting abnormally:', exc_info=exc_info)
else:
logger.debug('Exiting abnormally:', exc_info=exc_info)

View file

@ -306,7 +306,7 @@ class PostArgParseExceptHookTest(unittest.TestCase):
self.log_path = 'foo.log'
def test_base_exception(self):
exc_type = KeyboardInterrupt
exc_type = BaseException
mock_logger, output = self._test_common(exc_type, debug=False)
self._assert_exception_logged(mock_logger.error, exc_type)
self._assert_logfile_output(output)
@ -342,6 +342,11 @@ class PostArgParseExceptHookTest(unittest.TestCase):
self._assert_exception_logged(mock_logger.debug, exc_type)
self._assert_quiet_output(mock_logger, output)
def test_keyboardinterrupt(self):
exc_type = KeyboardInterrupt
mock_logger, output = self._test_common(exc_type, debug=False)
mock_logger.error.assert_called_once_with('Exiting due to user request.')
def _test_common(self, error_type, debug):
"""Returns the mocked logger and stderr output."""
mock_err = six.StringIO()