@@ -589,6 +589,31 @@ def exconly(self, tryshort: bool = False) -> str:
589
589
representation is returned (so 'AssertionError: ' is removed from
590
590
the beginning).
591
591
"""
592
+
593
+ def _get_single_subexc (
594
+ eg : BaseExceptionGroup [BaseException ],
595
+ ) -> BaseException | None :
596
+ res : BaseException | None = None
597
+ for subexc in eg .exceptions :
598
+ if res is not None :
599
+ return None
600
+
601
+ if isinstance (subexc , BaseExceptionGroup ):
602
+ res = _get_single_subexc (subexc )
603
+ if res is None :
604
+ # there were multiple exceptions in the subgroup
605
+ return None
606
+ else :
607
+ res = subexc
608
+ return res
609
+
610
+ if (
611
+ tryshort
612
+ and isinstance (self .value , BaseExceptionGroup )
613
+ and (subexc := _get_single_subexc (self .value )) is not None
614
+ ):
615
+ return f"[in { type (self .value ).__name__ } ] { subexc !r} "
616
+
592
617
lines = format_exception_only (self .type , self .value )
593
618
text = "" .join (lines )
594
619
text = text .rstrip ()
@@ -1033,24 +1058,6 @@ def _truncate_recursive_traceback(
1033
1058
1034
1059
def repr_excinfo (self , excinfo : ExceptionInfo [BaseException ]) -> ExceptionChainRepr :
1035
1060
repr_chain : list [tuple [ReprTraceback , ReprFileLocation | None , str | None ]] = []
1036
-
1037
- def _get_single_subexc (
1038
- eg : BaseExceptionGroup [BaseException ],
1039
- ) -> BaseException | None :
1040
- res : BaseException | None = None
1041
- for subexc in eg .exceptions :
1042
- if res is not None :
1043
- return None
1044
-
1045
- if isinstance (subexc , BaseExceptionGroup ):
1046
- res = _get_single_subexc (subexc )
1047
- if res is None :
1048
- # there were multiple exceptions in the subgroup
1049
- return None
1050
- else :
1051
- res = subexc
1052
- return res
1053
-
1054
1061
e : BaseException | None = excinfo .value
1055
1062
excinfo_ : ExceptionInfo [BaseException ] | None = excinfo
1056
1063
descr = None
@@ -1059,7 +1066,6 @@ def _get_single_subexc(
1059
1066
seen .add (id (e ))
1060
1067
1061
1068
if excinfo_ :
1062
- reprcrash = excinfo_ ._getreprcrash ()
1063
1069
# Fall back to native traceback as a temporary workaround until
1064
1070
# full support for exception groups added to ExceptionInfo.
1065
1071
# See https://github.com/pytest-dev/pytest/issues/9159
@@ -1073,13 +1079,9 @@ def _get_single_subexc(
1073
1079
)
1074
1080
)
1075
1081
)
1076
- if (
1077
- reprcrash is not None
1078
- and (subexc := _get_single_subexc (e )) is not None
1079
- ):
1080
- reprcrash .message = f"[in { type (e ).__name__ } ] { subexc !r} "
1081
1082
else :
1082
1083
reprtraceback = self .repr_traceback (excinfo_ )
1084
+ reprcrash = excinfo_ ._getreprcrash ()
1083
1085
else :
1084
1086
# Fallback to native repr if the exception doesn't have a traceback:
1085
1087
# ExceptionInfo objects require a full traceback to work.
0 commit comments