Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Version history

This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Fixed the ``repr()`` of exception groups being affected by mutation of the
original exception sequence after construction
(`#154 <https://github.com/agronholm/exceptiongroup/issues/154>`_)

**1.3.1**

- Fixed ``AttributeError: 'TracebackException' object has no attribute 'exceptions'``
Expand Down
4 changes: 3 additions & 1 deletion src/exceptiongroup/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ def __str__(self) -> str:
return f"{self.message} ({len(self._exceptions)} sub-exception{suffix})"

def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.args[0]!r}, {self.args[1]!r})"
return (
f"{self.__class__.__name__}({self.args[0]!r}, {list(self._exceptions)!r})"
)


class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception):
Expand Down
14 changes: 10 additions & 4 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,13 @@ def test_exceptions_mutate_original_sequence():

exceptions.append(KeyError("bar"))
assert excgrp.exceptions is exc_tuple
assert repr(excgrp) == (
"BaseExceptionGroup('foo', [ValueError(1), KeyboardInterrupt(), "
"KeyError('bar')])"
)
if sys.version_info < (3, 11):
# On < 3.11, the backport is active and stores exceptions as a tuple,
# so repr reflects the original (unmutated) exceptions.
# On >= 3.11, native BaseExceptionGroup is used. Whether the repr
# shows original or mutated exceptions depends on the CPython version
# (cpython#141736 fixes it for 3.13.12+, but not yet in 3.14).
# We only assert the backport behavior since we control it.
assert repr(excgrp) == (
"BaseExceptionGroup('foo', [ValueError(1), KeyboardInterrupt()])"
)