@@ -41,6 +41,15 @@ def get_condition_filter(
41
41
42
42
raise TypeError ("expected a function, exception type or tuple of exception types" )
43
43
44
+ def _derive_and_copy_attributes (self , excs ):
45
+ eg = self .derive (excs )
46
+ eg .__cause__ = self .__cause__
47
+ eg .__context__ = self .__context__
48
+ eg .__traceback__ = self .__traceback__
49
+ if hasattr (self , "__notes__" ):
50
+ # Create a new list so that add_note() only affects one exceptiongroup
51
+ eg .__notes__ = list (self .__notes__ )
52
+ return eg
44
53
45
54
class BaseExceptionGroup (BaseException , Generic [_BaseExceptionT_co ]):
46
55
"""A combination of multiple unrelated exceptions."""
@@ -154,10 +163,7 @@ def subgroup(
154
163
if not modified :
155
164
return self
156
165
elif exceptions :
157
- group = self .derive (exceptions )
158
- group .__cause__ = self .__cause__
159
- group .__context__ = self .__context__
160
- group .__traceback__ = self .__traceback__
166
+ group = _derive_and_copy_attributes (self , exceptions )
161
167
return group
162
168
else :
163
169
return None
@@ -230,17 +236,11 @@ def split(
230
236
231
237
matching_group : _BaseExceptionGroupSelf | None = None
232
238
if matching_exceptions :
233
- matching_group = self .derive (matching_exceptions )
234
- matching_group .__cause__ = self .__cause__
235
- matching_group .__context__ = self .__context__
236
- matching_group .__traceback__ = self .__traceback__
239
+ matching_group = _derive_and_copy_attributes (self , matching_exceptions )
237
240
238
241
nonmatching_group : _BaseExceptionGroupSelf | None = None
239
242
if nonmatching_exceptions :
240
- nonmatching_group = self .derive (nonmatching_exceptions )
241
- nonmatching_group .__cause__ = self .__cause__
242
- nonmatching_group .__context__ = self .__context__
243
- nonmatching_group .__traceback__ = self .__traceback__
243
+ nonmatching_group = _derive_and_copy_attributes (self , nonmatching_exceptions )
244
244
245
245
return matching_group , nonmatching_group
246
246
@@ -257,12 +257,7 @@ def derive(
257
257
def derive (
258
258
self , __excs : Sequence [_BaseExceptionT ]
259
259
) -> BaseExceptionGroup [_BaseExceptionT ]:
260
- eg = BaseExceptionGroup (self .message , __excs )
261
- if hasattr (self , "__notes__" ):
262
- # Create a new list so that add_note() only affects one exceptiongroup
263
- eg .__notes__ = list (self .__notes__ )
264
-
265
- return eg
260
+ return BaseExceptionGroup (self .message , __excs )
266
261
267
262
def __str__ (self ) -> str :
268
263
suffix = "" if len (self ._exceptions ) == 1 else "s"
0 commit comments