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
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]:
"""Return the index of the frame/TracebackEntry where recursion originates if
appropriate, None if no recursion occurred."""
@ -628,12 +617,15 @@ class ExceptionInfo(Generic[E]):
return isinstance(self.value, exc)
def _getreprcrash(self) -> Optional["ReprFileLocation"]:
exconly = self.exconly(tryshort=True)
entry = self.traceback.getcrashentry(self)
if entry is None:
return None
path, lineno = entry.frame.code.raw.co_filename, entry.lineno
return ReprFileLocation(path, lineno + 1, exconly)
# Find last non-hidden traceback entry that led to the exception of the
# traceback, or None if all hidden.
for i in range(-1, -len(self.traceback) - 1, -1):
entry = self.traceback[i]
if not entry.ishidden(self):
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(
self,

View File

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