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

Commit ebbe91d

Browse files
authored
Merge pull request #16 from WindSoilder/suppress_copy
remember to copy __suppress_context__ for ExceptionGroup.
2 parents d680bfd + 1df86e8 commit ebbe91d

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

exceptiongroup/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ 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
55+
# last, after copying __cause__.
56+
new_group.__suppress_context__ = self.__suppress_context__
5357
return new_group
5458

5559
def __str__(self):

exceptiongroup/_tests/test_exceptiongroup.py

+35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import copy
12
import pytest
23

34
from exceptiongroup import ExceptionGroup
45

56

7+
def raise_group():
8+
try:
9+
1 / 0
10+
except Exception as e:
11+
raise ExceptionGroup("ManyError", [e], [str(e)]) from e
12+
13+
614
def test_exception_group_init():
715
memberA = ValueError("A")
816
memberB = RuntimeError("B")
@@ -47,3 +55,30 @@ def test_exception_group_str():
4755
assert "ExceptionGroup: " in repr(group)
4856
assert "memberA" in repr(group)
4957
assert "memberB" in repr(group)
58+
59+
60+
def test_exception_group_copy():
61+
try:
62+
raise_group() # the exception is raise by `raise...from..`
63+
except ExceptionGroup as e:
64+
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
79+
group.__suppress_context__ = False
80+
another_group = copy.copy(group)
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

exceptiongroup/_tests/test_tools.py

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ def test_split_and_check_attributes_same():
112112
assert matched.__traceback__ is new_group.__traceback__
113113
assert matched.__cause__ is new_group.__cause__
114114
assert matched.__context__ is new_group.__context__
115+
assert matched.__suppress_context__ is new_group.__suppress_context__
115116
assert unmatched.__traceback__ is new_group.__traceback__
116117
assert unmatched.__cause__ is new_group.__cause__
117118
assert unmatched.__context__ is new_group.__context__
119+
assert unmatched.__suppress_context__ is new_group.__suppress_context__

0 commit comments

Comments
 (0)