[3.14] gh-141732: Fix ExceptionGroup repr changing when original exception sequence is mutated (GH-141736) by dr-carlos · Pull Request #144445 · python/cpython · GitHub
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 Doc/library/exceptions.rst
1 change: 1 addition & 0 deletions Include/cpython/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef struct {
PyException_HEAD
PyObject *msg;
PyObject *excs;
PyObject *excs_str;
} PyBaseExceptionGroupObject;

typedef struct {
Expand Down
73 changes: 72 additions & 1 deletion Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import collections.abc
import collections
import types
import unittest
from test.support import skip_emscripten_stack_overflow, skip_wasi_stack_overflow, exceeds_recursion_limit
Expand Down Expand Up @@ -193,6 +193,77 @@ class MyEG(ExceptionGroup):
"MyEG('flat', [ValueError(1), TypeError(2)]), "
"TypeError(2)])"))

def test_exceptions_mutation(self):
class MyEG(ExceptionGroup):
pass

excs = [ValueError(1), TypeError(2)]
eg = MyEG('test', excs)

self.assertEqual(repr(eg), "MyEG('test', [ValueError(1), TypeError(2)])")
excs.clear()

# Ensure that clearing the exceptions sequence doesn't change the repr.
self.assertEqual(repr(eg), "MyEG('test', [ValueError(1), TypeError(2)])")

# Ensure that the args are still as passed.
self.assertEqual(eg.args, ('test', []))

excs = (ValueError(1), KeyboardInterrupt(2))
eg = BaseExceptionGroup('test', excs)

# Ensure that immutable sequences still work fine.
self.assertEqual(
repr(eg),
"BaseExceptionGroup('test', (ValueError(1), KeyboardInterrupt(2)))"
)

# Test non-standard custom sequences.
excs = collections.deque([ValueError(1), TypeError(2)])
eg = ExceptionGroup('test', excs)

self.assertEqual(
repr(eg),
"ExceptionGroup('test', deque([ValueError(1), TypeError(2)]))"
)
excs.clear()

# Ensure that clearing the exceptions sequence doesn't change the repr.
self.assertEqual(
repr(eg),
"ExceptionGroup('test', deque([ValueError(1), TypeError(2)]))"
)

def test_repr_raises(self):
class MySeq(collections.abc.Sequence):
def __init__(self, raises):
self.raises = raises

def __len__(self):
return 1

def __getitem__(self, index):
if index == 0:
return ValueError(1)
raise IndexError

def __repr__(self):
if self.raises:
raise self.raises
return None

seq = MySeq(None)
with self.assertRaisesRegex(
TypeError,
r"__repr__ returned non-string \(type NoneType\)"
):
ExceptionGroup("test", seq)

seq = MySeq(ValueError)
with self.assertRaises(ValueError):
BaseExceptionGroup("test", seq)



def create_simple_eg():
excs = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure the :meth:`~object.__repr__` for :exc:`ExceptionGroup` and :exc:`BaseExceptionGroup` does
not change when the exception sequence that was original passed in to its constructor is subsequently mutated.
89 changes: 77 additions & 12 deletions Objects/exceptions.c
Loading