Merge pull request #4292 from blueyed/exit-outcome

Derive outcomes.exit.Exit from SystemExit instead of KeyboardInterrupt
This commit is contained in:
Bruno Oliveira 2018-12-13 13:43:39 -02:00 committed by GitHub
commit b390c66dc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 8 deletions

View File

@ -0,0 +1 @@
``pytest.outcomes.Exit`` is derived from ``SystemExit`` instead of ``KeyboardInterrupt``.

View File

@ -205,7 +205,7 @@ def wrap_session(config, doit):
raise raise
except Failed: except Failed:
session.exitstatus = EXIT_TESTSFAILED session.exitstatus = EXIT_TESTSFAILED
except KeyboardInterrupt: except (KeyboardInterrupt, exit.Exception):
excinfo = _pytest._code.ExceptionInfo.from_current() excinfo = _pytest._code.ExceptionInfo.from_current()
exitstatus = EXIT_INTERRUPTED exitstatus = EXIT_INTERRUPTED
if initstate <= 2 and isinstance(excinfo.value, exit.Exception): if initstate <= 2 and isinstance(excinfo.value, exit.Exception):

View File

@ -49,13 +49,13 @@ class Failed(OutcomeException):
__module__ = "builtins" __module__ = "builtins"
class Exit(KeyboardInterrupt): class Exit(SystemExit):
""" raised for immediate program exits (no tracebacks/summaries)""" """ raised for immediate program exits (no tracebacks/summaries)"""
def __init__(self, msg="unknown reason", returncode=None): def __init__(self, msg="unknown reason", returncode=None):
self.msg = msg self.msg = msg
self.returncode = returncode self.returncode = returncode
KeyboardInterrupt.__init__(self, msg) SystemExit.__init__(self, msg)
# exposed helper methods # exposed helper methods
@ -63,7 +63,7 @@ class Exit(KeyboardInterrupt):
def exit(msg, returncode=None): def exit(msg, returncode=None):
""" """
Exit testing process as if KeyboardInterrupt was triggered. Exit testing process as if SystemExit was triggered.
:param str msg: message to display upon exit. :param str msg: message to display upon exit.
:param int returncode: return code to be used when exiting pytest. :param int returncode: return code to be used when exiting pytest.

View File

@ -15,6 +15,7 @@ from .reports import CollectErrorRepr
from .reports import CollectReport from .reports import CollectReport
from .reports import TestReport from .reports import TestReport
from _pytest._code.code import ExceptionInfo from _pytest._code.code import ExceptionInfo
from _pytest.outcomes import Exit
from _pytest.outcomes import skip from _pytest.outcomes import skip
from _pytest.outcomes import Skipped from _pytest.outcomes import Skipped
from _pytest.outcomes import TEST_OUTCOME from _pytest.outcomes import TEST_OUTCOME
@ -190,10 +191,11 @@ def check_interactive_exception(call, report):
def call_runtest_hook(item, when, **kwds): def call_runtest_hook(item, when, **kwds):
hookname = "pytest_runtest_" + when hookname = "pytest_runtest_" + when
ihook = getattr(item.ihook, hookname) ihook = getattr(item.ihook, hookname)
reraise = (Exit,)
if not item.config.getvalue("usepdb"):
reraise += (KeyboardInterrupt,)
return CallInfo.from_call( return CallInfo.from_call(
lambda: ihook(item=item, **kwds), lambda: ihook(item=item, **kwds), when=when, reraise=reraise
when=when,
reraise=KeyboardInterrupt if not item.config.getvalue("usepdb") else (),
) )

View File

@ -553,7 +553,7 @@ def test_outcomeexception_passes_except_Exception():
def test_pytest_exit(): def test_pytest_exit():
with pytest.raises(pytest.exit.Exception) as excinfo: with pytest.raises(pytest.exit.Exception) as excinfo:
pytest.exit("hello") pytest.exit("hello")
assert excinfo.errisinstance(KeyboardInterrupt) assert excinfo.errisinstance(pytest.exit.Exception)
def test_pytest_fail(): def test_pytest_fail():