Skip to content

Commit

Permalink
nodes: apply same traceback filtering for chained exceptions as main …
Browse files Browse the repository at this point in the history
…exception

Fix pytest-dev#1904.
  • Loading branch information
bluetech committed Apr 16, 2023
1 parent 979ef5f commit 9de3601
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,9 @@ def getrepr(
showlocals: bool = False,
style: "_TracebackStyle" = "long",
abspath: bool = False,
tbfilter: bool = True,
tbfilter: Union[
bool, Callable[["ExceptionInfo[BaseException]"], Traceback]
] = True,
funcargs: bool = False,
truncate_locals: bool = True,
chain: bool = True,
Expand All @@ -652,8 +654,12 @@ def getrepr(
:param bool abspath:
If paths should be changed to absolute or left unchanged.
:param bool tbfilter:
Hide entries that contain a local variable ``__tracebackhide__==True``.
:param tbfilter:
A filter for traceback entries.
- If false, don't hide any entries.
- If true, hide internal entries and entries that contain a local
variable ``__tracebackhide__==True``.
- If a callable, delegates the filtering to the callable.
Ignored if ``style=="native"``.
:param bool funcargs:
Expand Down Expand Up @@ -719,7 +725,7 @@ class FormattedExcinfo:
showlocals: bool = False
style: "_TracebackStyle" = "long"
abspath: bool = True
tbfilter: bool = True
tbfilter: Union[bool, Callable[[ExceptionInfo[BaseException]], Traceback]] = True
funcargs: bool = False
truncate_locals: bool = True
chain: bool = True
Expand Down Expand Up @@ -881,7 +887,9 @@ def _makepath(self, path: Union[Path, str]) -> str:

def repr_traceback(self, excinfo: ExceptionInfo[BaseException]) -> "ReprTraceback":
traceback = excinfo.traceback
if self.tbfilter:
if callable(self.tbfilter):
traceback = self.tbfilter(excinfo)
elif self.tbfilter:
traceback = traceback.filter(excinfo)

if isinstance(excinfo.value, RecursionError):
Expand Down
7 changes: 5 additions & 2 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,13 @@ def _repr_failure_py(
style = "value"
if isinstance(excinfo.value, FixtureLookupError):
return excinfo.value.formatrepr()

tbfilter: Union[bool, Callable[[ExceptionInfo[BaseException]], Traceback]]
if self.config.getoption("fulltrace", False):
style = "long"
tbfilter = False
else:
excinfo.traceback = self._traceback_filter(excinfo)
tbfilter = self._traceback_filter
if style == "auto":
style = "long"
# XXX should excinfo.getrepr record all data and toterminal() process it?
Expand Down Expand Up @@ -484,7 +487,7 @@ def _repr_failure_py(
abspath=abspath,
showlocals=self.config.getoption("showlocals", False),
style=style,
tbfilter=False, # pruned already, or in --fulltrace mode.
tbfilter=tbfilter,
truncate_locals=truncate_locals,
)

Expand Down
45 changes: 45 additions & 0 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,3 +1603,48 @@ def test():
result.stdout.fnmatch_lines(["*ZeroDivisionError: division by zero"])
if tbstyle not in ("line", "native"):
result.stdout.fnmatch_lines(["All traceback entries are hidden.*"])


def test_hidden_entries_of_chained_exceptions_are_not_shown(pytester: Pytester) -> None:
"""Hidden entries of chained exceptions are not shown (#1904)."""
p = pytester.makepyfile(
"""
def g1():
__tracebackhide__ = True
str.does_not_exist
def f3():
__tracebackhide__ = True
1 / 0
def f2():
try:
f3()
except Exception:
g1()
def f1():
__tracebackhide__ = True
f2()
def test():
f1()
"""
)
result = pytester.runpytest(str(p), "--tb=short")
assert result.ret == 1
result.stdout.fnmatch_lines(
[
"*.py:11: in f2",
" f3()",
"E ZeroDivisionError: division by zero",
"",
"During handling of the above exception, another exception occurred:",
"*.py:20: in test",
" f1()",
"*.py:13: in f2",
" g1()",
"E AttributeError:*'does_not_exist'",
],
consecutive=True,
)

0 comments on commit 9de3601

Please sign in to comment.