Skip to content

gh-160659: Test except* handlers raising a non-Exception BaseException - #160660

Open
agronholm wants to merge 1 commit into
python:mainfrom
agronholm:gh-160659-except-star-base-exception-tests
Open

agronholm wants to merge 1 commit into
python:mainfrom
agronholm:gh-160659-except-star-base-exception-tests

Conversation

@agronholm

@agronholm agronholm commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Adds tests for except* handlers that raise a BaseException that isn't an Exception. Until now, every handler in test_except_star.py raised an Exception subclass. This changes tests only; CPython already behaves correctly.

Why

PyPy runs CPython's test_except_star.py and passes all of it, but gets this case wrong (pypy/pypy#5689). Its except* re-raise logic asserts that every exception raised in a handler is an Exception, so raising e.g. KeyboardInterrupt from a handler produces an internal AssertionError. Fixing that alone exposes a second bug: it builds an ExceptionGroup where CPython builds a BaseExceptionGroup, which fails with TypeError: Cannot nest BaseExceptions in an ExceptionGroup. Neither bug was noticed because no test covered this case. It only came up through a test in the exceptiongroup backport (agronholm/exceptiongroup#163), which now skips that test on PyPy.

New tests in TestExceptStarRaise

They follow the existing test_raise_* tests, but raise a local BaseException subclass. I used a local subclass rather than KeyboardInterrupt so that a broken implementation can't stop the test run by letting a KeyboardInterrupt escape.

  • test_raise_base_exception: part of the group is handled, so the result must be a BaseExceptionGroup holding the raised exception and the unhandled rest.
  • test_raise_handle_all_raise_one_base_exception: the whole group is handled, so the raised exception propagates on its own.
  • test_raise_handle_all_raise_base_exception_and_exception: two handlers raise a BaseException and an Exception, so the result must be a BaseExceptionGroup.

The two group tests check the group's type and members directly. assertExceptionIsLike only looks inside ExceptionGroup instances; given a BaseExceptionGroup, it compares just the class and message.

Testing

No NEWS entry, since this only adds tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants