Skip to content

Commit

Permalink
nodes: change _prunetraceback to return the new traceback instead of …
Browse files Browse the repository at this point in the history
…modifying excinfo

This makes it usable as a general function, and just more understandable
in general.
  • Loading branch information
bluetech committed Apr 16, 2023
1 parent 6c4d7ba commit 979ef5f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from _pytest._code import getfslineno
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import TerminalRepr
from _pytest._code.code import Traceback
from _pytest.compat import cached_property
from _pytest.compat import LEGACY_PATH
from _pytest.config import Config
Expand Down Expand Up @@ -432,8 +433,8 @@ def getparent(self, cls: Type[_NodeType]) -> Optional[_NodeType]:
assert current is None or isinstance(current, cls)
return current

def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
pass
def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
return excinfo.traceback

def _repr_failure_py(
self,
Expand All @@ -452,7 +453,7 @@ def _repr_failure_py(
if self.config.getoption("fulltrace", False):
style = "long"
else:
self._prunetraceback(excinfo)
excinfo.traceback = self._traceback_filter(excinfo)
if style == "auto":
style = "long"
# XXX should excinfo.getrepr record all data and toterminal() process it?
Expand Down Expand Up @@ -554,13 +555,14 @@ def repr_failure( # type: ignore[override]

return self._repr_failure_py(excinfo, style=tbstyle)

def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
if hasattr(self, "path"):
traceback = excinfo.traceback
ntraceback = traceback.cut(path=self.path)
if ntraceback == traceback:
ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
excinfo.traceback = ntraceback.filter(excinfo)
return excinfo.traceback.filter(excinfo)
return excinfo.traceback


def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[str]:
Expand Down
15 changes: 9 additions & 6 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ def runtest(self) -> None:
def setup(self) -> None:
self._request._fillfixtures()

def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
if hasattr(self, "_obj") and not self.config.getoption("fulltrace", False):
code = _pytest._code.Code.from_function(get_real_func(self.obj))
path, firstlineno = code.path, code.firstlineno
Expand All @@ -1814,19 +1814,22 @@ def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
ntraceback = ntraceback.filter(filter_traceback)
if not ntraceback:
ntraceback = traceback
ntraceback = ntraceback.filter(excinfo)

excinfo.traceback = ntraceback.filter(excinfo)
# issue364: mark all but first and last frames to
# only show a single-line message for each frame.
if self.config.getoption("tbstyle", "auto") == "auto":
if len(excinfo.traceback) > 2:
excinfo.traceback = Traceback(
if len(ntraceback) > 2:
ntraceback = Traceback(
entry
if i == 0 or i == len(excinfo.traceback) - 1
if i == 0 or i == len(ntraceback) - 1
else entry.with_repr_style("short")
for i, entry in enumerate(excinfo.traceback)
for i, entry in enumerate(ntraceback)
)

return ntraceback
return excinfo.traceback

# TODO: Type ignored -- breaks Liskov Substitution.
def repr_failure( # type: ignore[override]
self,
Expand Down
13 changes: 7 additions & 6 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,16 @@ def runtest(self) -> None:
finally:
delattr(self._testcase, self.name)

def _prunetraceback(
def _traceback_filter(
self, excinfo: _pytest._code.ExceptionInfo[BaseException]
) -> None:
super()._prunetraceback(excinfo)
traceback = excinfo.traceback.filter(
) -> _pytest._code.Traceback:
traceback = super()._traceback_filter(excinfo)
ntraceback = traceback.filter(
lambda x: not x.frame.f_globals.get("__unittest"),
)
if traceback:
excinfo.traceback = traceback
if not ntraceback:
ntraceback = traceback
return ntraceback


@hookimpl(tryfirst=True)
Expand Down

0 comments on commit 979ef5f

Please sign in to comment.