Skip to content
This repository was archived by the owner on Nov 2, 2022. It is now read-only.

Commit b1b3096

Browse files
committed
add comment about why we need to copy __suppress_context__ attribute, and enhance test case.
1 parent ffe3bf1 commit b1b3096

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

exceptiongroup/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def __copy__(self):
5050
new_group.__traceback__ = self.__traceback__
5151
new_group.__context__ = self.__context__
5252
new_group.__cause__ = self.__cause__
53+
# Setting __cause__ also implicitly sets the __suppress_context__
54+
# attribute to True. So we should copy __suppress_context__ attribute.
5355
new_group.__suppress_context__ = self.__suppress_context__
5456
return new_group
5557

exceptiongroup/_tests/test_exceptiongroup.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,26 @@ def test_exception_group_str():
5959

6060
def test_exception_group_copy():
6161
try:
62-
raise_group()
63-
except BaseException as e:
62+
raise_group() # the exception is raise by `raise...from..`
63+
except ExceptionGroup as e:
6464
group = e
65+
66+
another_group = copy.copy(group)
67+
assert another_group.message == group.message
68+
assert another_group.exceptions == group.exceptions
69+
assert another_group.sources == group.sources
70+
assert another_group.__traceback__ is group.__traceback__
71+
assert another_group.__cause__ is group.__cause__
72+
assert another_group.__context__ is group.__context__
73+
assert another_group.__suppress_context__ is group.__suppress_context__
74+
assert another_group.__cause__ is not None
75+
assert another_group.__context__ is not None
76+
assert another_group.__suppress_context__ is True
77+
78+
# doing copy when __suppress_context__ is False
6579
group.__suppress_context__ = False
6680
another_group = copy.copy(group)
67-
assert group.message == another_group.message
68-
assert group.exceptions == another_group.exceptions
69-
assert group.sources == another_group.sources
70-
assert group.__traceback__ is another_group.__traceback__
71-
assert group.__context__ is another_group.__context__
72-
assert group.__cause__ is another_group.__cause__
73-
assert group.__suppress_context__ == another_group.__suppress_context__
81+
assert another_group.__cause__ is group.__cause__
82+
assert another_group.__context__ is group.__context__
83+
assert another_group.__suppress_context__ is group.__suppress_context__
84+
assert another_group.__suppress_context__ is False

0 commit comments

Comments
 (0)