Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Authors
* Oldřich Jedlička - https://github.com/oldium
* Colin Watson - https://github.com/cjwatson
* Jacob Tomlinson - https://github.com/jacobtomlinson
* yqia03 - https://github.com/yqia03
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

Unreleased
~~~~~~~~~~

* Fixed unpickling of ``BaseExceptionGroup`` instances containing exceptions such as ``KeyboardInterrupt``.

3.2.2 (2025-11-12)
~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 2 additions & 2 deletions src/tblib/pickling_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import Traceback

if sys.version_info < (3, 11):
ExceptionGroup = None
BaseExceptionGroup = None


def unpickle_traceback(tb_frame, tb_lineno, tb_next):
Expand Down Expand Up @@ -82,7 +82,7 @@ def pickle_exception(
attrs['filename'] = obj.filename
if obj.filename2 is not None:
attrs['filename2'] = obj.filename2
if ExceptionGroup is not None and isinstance(obj, ExceptionGroup):
if BaseExceptionGroup is not None and isinstance(obj, BaseExceptionGroup):
args = (obj.message, obj.exceptions)

return (
Expand Down
40 changes: 40 additions & 0 deletions tests/test_pickle_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,43 @@ def test_exception_group():
assert isinstance(exc.exceptions[8], OSError)
assert exc.exceptions[8].errno == 2
assert str(exc.exceptions[8]) == real_oserror_str


@pytest.mark.skipif(not has_python311, reason='BaseExceptionGroup needs Python 3.11')
@pytest.mark.parametrize('how', ['global', 'instance', 'class'])
@pytest.mark.parametrize('protocol', [0, pickle.HIGHEST_PROTOCOL])
def test_base_exception_group(clear_dispatch_table, how, protocol):
try:
raise KeyboardInterrupt('stop')
except KeyboardInterrupt as error:
interrupt = error

nested = ExceptionGroup('nested', [ValueError('task failed')]) # noqa: F821
group = BaseExceptionGroup('interrupted tasks', [interrupt, nested]) # noqa: F821
group.add_note('group note')
try:
raise group
except BaseException as error:
exc = error

assert type(exc) is BaseExceptionGroup # noqa: F821
if how == 'global':
tblib.pickling_support.install()
elif how == 'instance':
tblib.pickling_support.install(exc)
else:
tblib.pickling_support.install(BaseExceptionGroup, KeyboardInterrupt, ExceptionGroup, ValueError) # noqa: F821

restored = pickle.loads(pickle.dumps(exc, protocol=protocol))

assert type(restored) is BaseExceptionGroup # noqa: F821
assert restored.message == 'interrupted tasks'
assert restored.__traceback__ is not None
assert restored.__notes__ == ['group note']
assert isinstance(restored.exceptions[0], KeyboardInterrupt)
assert restored.exceptions[0].args == ('stop',)
assert restored.exceptions[0].__traceback__ is not None
assert type(restored.exceptions[1]) is ExceptionGroup # noqa: F821
assert restored.exceptions[1].message == 'nested'
assert isinstance(restored.exceptions[1].exceptions[0], ValueError)
assert restored.exceptions[1].exceptions[0].args == ('task failed',)