Repository navigation
Conversation
The other outcome exceptions (`skip`, `fail`, `xfail`) derive from `BaseException`, so that code catching `Exception` doesn't swallow them. `Exit` derived from `Exception` though, so a test or fixture with an `except Exception:` around code that calls `pytest.exit()` swallowed it, and the run just continued. pytest itself had to work around this in `pytest.warns` and in `assertrepr_compare`, which are now simplified. `Exit` used to derive from `KeyboardInterrupt`, and later from `SystemExit`, but was changed to `Exception` because pdb catches those two itself. Deriving from `BaseException` directly avoids both problems.
6290cc9 to
e915ad2
Compare
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
History: Exit went KeyboardInterrupt -> SystemExit in #4292 (pdb swallowed KeyboardInterrupt), then -> Exception in #4280 with no recorded reason. BaseException is the right base.
Two places still assume Exit is an Exception:
SetupState.teardown_exactcatches onlyTEST_OUTCOME.pytest.exit()in a fixture teardown now aborts the loop and the node's other finalizers never run. Please collectExitthere too and re-raise after the loop.pytest.exit()in an initial conftest or-pplugin used to become an import error; now it escapesconfig.main()as a raw traceback, rc 1, returncode ignored.main()should handleExit.
Tests for both would be good. As except Exception / pytest.raises(Exception) no longer catch it, a breaking changelog entry fits better than improvement.
Generated by Claude Code
9904e29 to
db88f81
Compare
Now that `Exit` is a `BaseException`, two places that used to catch it as an `Exception` no longer did: `SetupState.teardown_exact` only catches `TEST_OUTCOME`, so a `pytest.exit()` in a fixture teardown stopped the loop and the other finalizers never ran. It now runs them all and re-raises the `Exit` afterwards. It is kept out of the collected teardown errors, because wrapped in an exception group it would not stop the session anymore. `pytest.exit()` in an initial conftest or a `-p` plugin escaped `config.main()` as a raw traceback with exit code 1. It is now reported like an `Exit` during the session, and its returncode is used. (Before `Exit` became a `BaseException`, this was reported as an import error, also ignoring the returncode.)
db88f81 to
ac02cc0
Compare
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Both fixes work here, and coverage is complete now.
One thing left for later, not this PR: when one finalizer calls pytest.exit() and another raises, the other error is dropped. That is the same problem as #13650 (Exit inside an exception group); once #13736 lands, teardown_exact can collect Exit into the group and keep both.
Generated by Claude Code
pytest.exit()raisesExit, which derives fromException, unlike the other outcome exceptions (skip,fail,xfail) which derive fromBaseExceptionsince #2490. So anyexcept Exception:in a test or fixture swallows it, and the run continues. pytest itself already had to work around this inassertrepr_compare(#4978) and inpytest.warns(#11920).This makes
Exitderive fromBaseExceptiondirectly. It used to derive fromKeyboardInterruptand later fromSystemExit(#4292), but pdb catches both of those itself, which is why it was changed toException(#4280).