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

Commit eef4d66

Browse files
committed
1. The __copy__ method should also copy attribute. 2. Update test code to cover this.
1 parent ee3e826 commit eef4d66

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

exceptiongroup/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ def __init__(self, message, exceptions, sources):
4343
)
4444

4545
# copy.copy doesn't work for ExceptionGroup, because BaseException have
46-
# rewrite __reduce_ex__ method. So we need to add __copy__ method to
46+
# rewrite __reduce_ex__ method. We need to add __copy__ method to
4747
# make it can be copied.
4848
def __copy__(self):
4949
new_group = self.__class__(self.message, self.exceptions, self.sources)
5050
new_group.__traceback__ = self.__traceback__
5151
new_group.__context__ = self.__context__
52+
new_group.__cause__ = self.__cause__
5253
return new_group
5354

5455

exceptiongroup/_tests/test_tools.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ def raise_error(err):
55
raise err
66

77

8+
def raise_error_from_another(out_err, another_err):
9+
# use try..except approache so out_error have meaningful
10+
# __context__, __cause__ attribute.
11+
try:
12+
raise another_err
13+
except Exception as e:
14+
raise out_err from e
15+
16+
817
def test_split_for_none_exception():
918
matched, unmatched = split(RuntimeError, None)
1019
assert matched is None
@@ -79,7 +88,7 @@ def test_split_with_single_exception():
7988
assert unmatched is err
8089

8190

82-
def test_split_with_same_traceback():
91+
def test_split_and_check_attributes_same():
8392
try:
8493
raise_error(RuntimeError("RuntimeError"))
8594
except Exception as e:
@@ -93,11 +102,16 @@ def test_split_with_same_traceback():
93102
group = ExceptionGroup(
94103
"ErrorGroup", [run_error, val_error], ["RuntimeError", "ValueError"]
95104
)
105+
# go and check __traceback__, __cause__ attributes
96106
try:
97-
raise_error(group)
107+
raise_error_from_another(group, RuntimeError("Cause"))
98108
except BaseException as e:
99109
new_group = e
100110

101111
matched, unmatched = split(RuntimeError, group)
102112
assert matched.__traceback__ is new_group.__traceback__
113+
assert matched.__cause__ is new_group.__cause__
114+
assert matched.__context__ is new_group.__context__
103115
assert unmatched.__traceback__ is new_group.__traceback__
116+
assert unmatched.__cause__ is new_group.__cause__
117+
assert unmatched.__context__ is new_group.__context__

0 commit comments

Comments
 (0)