Skip to content

Commit fcada1e

Browse files
committed
nodes: change _prunetraceback to return the new traceback instead of modifying excinfo
This makes it usable as a general function, and just more understandable in general.
1 parent 6f7f89f commit fcada1e

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/_pytest/nodes.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from _pytest._code import getfslineno
2323
from _pytest._code.code import ExceptionInfo
2424
from _pytest._code.code import TerminalRepr
25+
from _pytest._code.code import Traceback
2526
from _pytest.compat import cached_property
2627
from _pytest.compat import LEGACY_PATH
2728
from _pytest.config import Config
@@ -432,8 +433,8 @@ def getparent(self, cls: Type[_NodeType]) -> Optional[_NodeType]:
432433
assert current is None or isinstance(current, cls)
433434
return current
434435

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

438439
def _repr_failure_py(
439440
self,
@@ -452,7 +453,7 @@ def _repr_failure_py(
452453
if self.config.getoption("fulltrace", False):
453454
style = "long"
454455
else:
455-
self._prunetraceback(excinfo)
456+
excinfo.traceback = self._traceback_filter(excinfo)
456457
if style == "auto":
457458
style = "long"
458459
# XXX should excinfo.getrepr record all data and toterminal() process it?
@@ -554,13 +555,14 @@ def repr_failure( # type: ignore[override]
554555

555556
return self._repr_failure_py(excinfo, style=tbstyle)
556557

557-
def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
558+
def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
558559
if hasattr(self, "path"):
559560
traceback = excinfo.traceback
560561
ntraceback = traceback.cut(path=self.path)
561562
if ntraceback == traceback:
562563
ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
563-
excinfo.traceback = ntraceback.filter(excinfo)
564+
return excinfo.traceback.filter(excinfo)
565+
return excinfo.traceback
564566

565567

566568
def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[str]:

src/_pytest/python.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ def runtest(self) -> None:
18021802
def setup(self) -> None:
18031803
self._request._fillfixtures()
18041804

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

1818-
excinfo.traceback = ntraceback.filter(excinfo)
18191819
# issue364: mark all but first and last frames to
18201820
# only show a single-line message for each frame.
18211821
if self.config.getoption("tbstyle", "auto") == "auto":
1822-
if len(excinfo.traceback) > 2:
1823-
excinfo.traceback = Traceback(
1822+
if len(ntraceback) > 2:
1823+
ntraceback = Traceback(
18241824
entry
1825-
if i == 0 or i == len(excinfo.traceback) - 1
1825+
if i == 0 or i == len(ntraceback) - 1
18261826
else entry.with_repr_style("short")
1827-
for i, entry in enumerate(excinfo.traceback)
1827+
for i, entry in enumerate(ntraceback)
18281828
)
18291829

1830+
return ntraceback
1831+
return excinfo.traceback
1832+
18301833
# TODO: Type ignored -- breaks Liskov Substitution.
18311834
def repr_failure( # type: ignore[override]
18321835
self,

src/_pytest/unittest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,16 @@ def runtest(self) -> None:
334334
finally:
335335
delattr(self._testcase, self.name)
336336

337-
def _prunetraceback(
337+
def _traceback_filter(
338338
self, excinfo: _pytest._code.ExceptionInfo[BaseException]
339-
) -> None:
340-
super()._prunetraceback(excinfo)
341-
traceback = excinfo.traceback.filter(
339+
) -> _pytest._code.Traceback:
340+
traceback = super()._traceback_filter(excinfo)
341+
ntraceback = traceback.filter(
342342
lambda x: not x.frame.f_globals.get("__unittest"),
343343
)
344-
if traceback:
345-
excinfo.traceback = traceback
344+
if not ntraceback:
345+
ntraceback = traceback
346+
return ntraceback
346347

347348

348349
@hookimpl(tryfirst=True)

0 commit comments

Comments
 (0)