code: inline `Traceback.getcrashentry` into `ExceptionInfo._getreprcrash`

Since `Traceback.getcrashentry` takes the `ExceptionInfo`, it is not
really independent of it and is in the wrong layer. Prevent nonsensical
mistakes by inlining it.
This commit is contained in:
Ran Benita 2023-04-12 23:43:00 +03:00
parent cc23ec91d0
commit 0a20452f78
2 changed files with 17 additions and 27 deletions

View File

@ -414,17 +414,6 @@ class Traceback(List[TracebackEntry]):
fn = _excinfo_or_fn fn = _excinfo_or_fn
return Traceback(filter(fn, self)) return Traceback(filter(fn, self))
def getcrashentry(
self, excinfo: Optional["ExceptionInfo[BaseException]"]
) -> Optional[TracebackEntry]:
"""Return last non-hidden traceback entry that lead to the exception of
a traceback, or None if all hidden."""
for i in range(-1, -len(self) - 1, -1):
entry = self[i]
if not entry.ishidden(excinfo):
return entry
return None
def recursionindex(self) -> Optional[int]: def recursionindex(self) -> Optional[int]:
"""Return the index of the frame/TracebackEntry where recursion originates if """Return the index of the frame/TracebackEntry where recursion originates if
appropriate, None if no recursion occurred.""" appropriate, None if no recursion occurred."""
@ -628,12 +617,15 @@ class ExceptionInfo(Generic[E]):
return isinstance(self.value, exc) return isinstance(self.value, exc)
def _getreprcrash(self) -> Optional["ReprFileLocation"]: def _getreprcrash(self) -> Optional["ReprFileLocation"]:
exconly = self.exconly(tryshort=True) # Find last non-hidden traceback entry that led to the exception of the
entry = self.traceback.getcrashentry(self) # traceback, or None if all hidden.
if entry is None: for i in range(-1, -len(self.traceback) - 1, -1):
return None entry = self.traceback[i]
path, lineno = entry.frame.code.raw.co_filename, entry.lineno if not entry.ishidden(self):
return ReprFileLocation(path, lineno + 1, exconly) path, lineno = entry.frame.code.raw.co_filename, entry.lineno
exconly = self.exconly(tryshort=True)
return ReprFileLocation(path, lineno + 1, exconly)
return None
def getrepr( def getrepr(
self, self,

View File

@ -11,7 +11,7 @@ from typing import Tuple
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Union from typing import Union
import _pytest import _pytest._code
import pytest import pytest
from _pytest._code.code import ExceptionChainRepr from _pytest._code.code import ExceptionChainRepr
from _pytest._code.code import ExceptionInfo from _pytest._code.code import ExceptionInfo
@ -290,7 +290,7 @@ class TestTraceback_f_g_h:
excinfo = pytest.raises(ValueError, fail) excinfo = pytest.raises(ValueError, fail)
assert excinfo.traceback.recursionindex() is None assert excinfo.traceback.recursionindex() is None
def test_traceback_getcrashentry(self): def test_getreprcrash(self):
def i(): def i():
__tracebackhide__ = True __tracebackhide__ = True
raise ValueError raise ValueError
@ -306,15 +306,13 @@ class TestTraceback_f_g_h:
g() g()
excinfo = pytest.raises(ValueError, f) excinfo = pytest.raises(ValueError, f)
tb = excinfo.traceback reprcrash = excinfo._getreprcrash()
entry = tb.getcrashentry(excinfo) assert reprcrash is not None
assert entry is not None
co = _pytest._code.Code.from_function(h) co = _pytest._code.Code.from_function(h)
assert entry.frame.code.path == co.path assert reprcrash.path == str(co.path)
assert entry.lineno == co.firstlineno + 1 assert reprcrash.lineno == co.firstlineno + 1 + 1
assert entry.frame.code.name == "h"
def test_traceback_getcrashentry_empty(self): def test_getreprcrash_empty(self):
def g(): def g():
__tracebackhide__ = True __tracebackhide__ = True
raise ValueError raise ValueError
@ -324,7 +322,7 @@ class TestTraceback_f_g_h:
g() g()
excinfo = pytest.raises(ValueError, f) excinfo = pytest.raises(ValueError, f)
assert excinfo.traceback.getcrashentry(excinfo) is None assert excinfo._getreprcrash() is None
def test_excinfo_exconly(): def test_excinfo_exconly():