diff --git a/AUTHORS.rst b/AUTHORS.rst index 6d7199a..67425a8 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -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 diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ed95b83..68191ab 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,11 @@ Changelog ========= +Unreleased +~~~~~~~~~~ + +* Fixed unpickling of ``BaseExceptionGroup`` instances containing exceptions such as ``KeyboardInterrupt``. + 3.2.2 (2025-11-12) ~~~~~~~~~~~~~~~~~~ diff --git a/src/tblib/pickling_support.py b/src/tblib/pickling_support.py index 2f57449..806232d 100644 --- a/src/tblib/pickling_support.py +++ b/src/tblib/pickling_support.py @@ -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): @@ -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 ( diff --git a/tests/test_pickle_exception.py b/tests/test_pickle_exception.py index 0990d9d..f6487c9 100644 --- a/tests/test_pickle_exception.py +++ b/tests/test_pickle_exception.py @@ -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',)