Skip to content

Mutations to the original exception sequence argument affects the exception group #143

Closed
@bswck

Description

@bswck

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Exceptiongroup version

1.2.2

Python version

3.9, 3.11

What happened?

The CPython implementation of PEP 654 transforms the ExceptionGroup's exception sequence argument to a tuple during initialization and stores just that, effectively disallowing the original sequence mutations to affect the created group—contrary to the exceptiongroup implementation, which reflects all, even incorrect, mutations.

This is not a violation of PEP 654. It's an inconsistency with the upstream CPython implementation of PEP 654.

Therefore (despite I personally think that this is worth fixing) if it's not considered a bug in this package, please close this issue.

How can we reproduce the bug?

from exceptiongroup import ExceptionGroup
excs = [ValueError()]
group = ExceptionGroup("", excs)
print(group.exceptions)
excs.clear()
print(group.exceptions)

outputs:

  • for Python 3.9.19 (main, Aug 14 2024, 05:11:09) [Clang 18.1.8 ] on linux:
    (ValueError(),)
    ()
  • for Python 3.11.9 (main, Aug 14 2024, 05:07:28) [Clang 18.1.8 ] on linux:
    (ValueError(),)
    (ValueError(),)

Note that in the backport implementation exception groups, after clearing the stored sequence, it is still possible to raise such exception groups with (0 sub-exceptions)—despite the constructor disallowing that state initially.

Suggested resolution

Explicitly casting __exceptions to a tuple in

instance._exceptions = __exceptions

No longer casting self._exceptions to a tuple in the exceptions property

@property
def exceptions(
self,
) -> tuple[_BaseExceptionT_co | BaseExceptionGroup[_BaseExceptionT_co], ...]:
return tuple(self._exceptions)

and other necessary work for future maintenance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions