diff --git a/AUTHORS b/AUTHORS index 14c465571..6e2f472fe 100644 --- a/AUTHORS +++ b/AUTHORS @@ -165,6 +165,7 @@ Marcelo Duarte Trevisani Marcin Bachry Marco Gorelli Mark Abramowitz +Mark Dickinson Markus Unterwaditzer Martijn Faassen Martin Altmayer diff --git a/changelog/6255.bugfix.rst b/changelog/6255.bugfix.rst new file mode 100644 index 000000000..831187feb --- /dev/null +++ b/changelog/6255.bugfix.rst @@ -0,0 +1,3 @@ +Clear the ``sys.last_traceback``, ``sys.last_type`` and ``sys.last_value`` +attributes by deleting them instead of setting them to ``None``. This better +matches the behaviour of the Python standard library. diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index c383146c3..67e28e905 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -121,7 +121,12 @@ def pytest_runtest_setup(item): def pytest_runtest_call(item): _update_current_test_var(item, "call") - sys.last_type, sys.last_value, sys.last_traceback = (None, None, None) + try: + del sys.last_type + del sys.last_value + del sys.last_traceback + except AttributeError: + pass try: item.runtest() except Exception: diff --git a/testing/test_runner.py b/testing/test_runner.py index 86e9bddff..301e11898 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -900,9 +900,9 @@ def test_store_except_info_on_error(): # The next run should clear the exception info stored by the previous run ItemMightRaise.raise_error = False runner.pytest_runtest_call(ItemMightRaise()) - assert sys.last_type is None - assert sys.last_value is None - assert sys.last_traceback is None + assert not hasattr(sys, "last_type") + assert not hasattr(sys, "last_value") + assert not hasattr(sys, "last_traceback") def test_current_test_env_var(testdir, monkeypatch):