Skip to content

Commit fa6a232

Browse files
authored
don't crash if exceptiongroup has only hidden tb frames (#14025)
* don't crash if exceptiongroup has only hidden tb frames
1 parent 7eb7b3e commit fa6a232

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

changelog/13734.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed crash when a test raises an exceptiongroup with ``__tracebackhide__ = True``.

src/_pytest/_code/code.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,9 +1193,15 @@ def repr_excinfo(self, excinfo: ExceptionInfo[BaseException]) -> ExceptionChainR
11931193
format_exception(
11941194
type(excinfo.value),
11951195
excinfo.value,
1196-
traceback[0]._rawentry,
1196+
traceback[0]._rawentry if traceback else None,
11971197
)
11981198
)
1199+
if not traceback:
1200+
reprtraceback.extraline = (
1201+
"All traceback entries are hidden. "
1202+
"Pass `--full-trace` to see hidden and internal frames."
1203+
)
1204+
11991205
else:
12001206
reprtraceback = self.repr_traceback(excinfo_)
12011207
reprcrash = excinfo_._getreprcrash()

testing/code/test_excinfo.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,19 +1897,23 @@ def test_nested_multiple() -> None:
18971897

18981898

18991899
@pytest.mark.parametrize("tbstyle", ("long", "short", "auto", "line", "native"))
1900-
def test_all_entries_hidden(pytester: Pytester, tbstyle: str) -> None:
1900+
@pytest.mark.parametrize("group", (True, False), ids=("group", "bare"))
1901+
def test_all_entries_hidden(pytester: Pytester, tbstyle: str, group: bool) -> None:
19011902
"""Regression test for #10903."""
19021903
pytester.makepyfile(
1903-
"""
1904+
f"""
1905+
import sys
1906+
if sys.version_info < (3, 11):
1907+
from exceptiongroup import ExceptionGroup
19041908
def test():
19051909
__tracebackhide__ = True
1906-
1 / 0
1910+
raise {'ExceptionGroup("", [ValueError("bar")])' if group else 'ValueError("bar")'}
19071911
"""
19081912
)
19091913
result = pytester.runpytest("--tb", tbstyle)
19101914
assert result.ret == 1
19111915
if tbstyle != "line":
1912-
result.stdout.fnmatch_lines(["*ZeroDivisionError: division by zero"])
1916+
result.stdout.fnmatch_lines(["*ValueError: bar"])
19131917
if tbstyle not in ("line", "native"):
19141918
result.stdout.fnmatch_lines(["All traceback entries are hidden.*"])
19151919

0 commit comments

Comments
 (0)