Skip to content

Commit 3311c18

Browse files
committed
move logic to ExceptionInfo
1 parent f59a8d1 commit 3311c18

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/_pytest/_code/code.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,31 @@ def exconly(self, tryshort: bool = False) -> str:
589589
representation is returned (so 'AssertionError: ' is removed from
590590
the beginning).
591591
"""
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+
592617
lines = format_exception_only(self.type, self.value)
593618
text = "".join(lines)
594619
text = text.rstrip()
@@ -1033,24 +1058,6 @@ def _truncate_recursive_traceback(
10331058

10341059
def repr_excinfo(self, excinfo: ExceptionInfo[BaseException]) -> ExceptionChainRepr:
10351060
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-
10541061
e: BaseException | None = excinfo.value
10551062
excinfo_: ExceptionInfo[BaseException] | None = excinfo
10561063
descr = None
@@ -1059,7 +1066,6 @@ def _get_single_subexc(
10591066
seen.add(id(e))
10601067

10611068
if excinfo_:
1062-
reprcrash = excinfo_._getreprcrash()
10631069
# Fall back to native traceback as a temporary workaround until
10641070
# full support for exception groups added to ExceptionInfo.
10651071
# See https://github.com/pytest-dev/pytest/issues/9159
@@ -1073,13 +1079,9 @@ def _get_single_subexc(
10731079
)
10741080
)
10751081
)
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}"
10811082
else:
10821083
reprtraceback = self.repr_traceback(excinfo_)
1084+
reprcrash = excinfo_._getreprcrash()
10831085
else:
10841086
# Fallback to native repr if the exception doesn't have a traceback:
10851087
# ExceptionInfo objects require a full traceback to work.

0 commit comments

Comments
 (0)