add checks to make sure crash entries are not none where they should not be

This commit is contained in:
Felix Hofstätter 2023-03-12 16:39:26 +01:00
parent e6e36c2dee
commit 7f95c9c3d7
3 changed files with 11 additions and 1 deletions

View File

@ -650,13 +650,18 @@ class ExceptionInfo(Generic[E]):
Added the ``chain`` parameter. Added the ``chain`` parameter.
""" """
if style == "native": if style == "native":
r = self._getreprcrash()
if r is None:
raise ValueError(
"There should always be a non-hidden traceback entry for the top level function."
)
return ReprExceptionInfo( return ReprExceptionInfo(
reprtraceback=ReprTracebackNative( reprtraceback=ReprTracebackNative(
traceback.format_exception( traceback.format_exception(
self.type, self.value, self.traceback[0]._rawentry self.type, self.value, self.traceback[0]._rawentry
) )
), ),
reprcrash=self._getreprcrash(), reprcrash=r,
) )
fmt = FormattedExcinfo( fmt = FormattedExcinfo(

View File

@ -347,6 +347,10 @@ class TestReport(BaseReport):
elif isinstance(excinfo.value, skip.Exception): elif isinstance(excinfo.value, skip.Exception):
outcome = "skipped" outcome = "skipped"
r = excinfo._getreprcrash() r = excinfo._getreprcrash()
if r is None:
raise ValueError(
"There should always be a traceback entry for skipping a test."
)
if excinfo.value._use_item_location: if excinfo.value._use_item_location:
path, line = item.reportinfo()[:2] path, line = item.reportinfo()[:2]
assert line is not None assert line is not None

View File

@ -294,6 +294,7 @@ class TestTraceback_f_g_h:
excinfo = pytest.raises(ValueError, f) excinfo = pytest.raises(ValueError, f)
tb = excinfo.traceback tb = excinfo.traceback
entry = tb.getcrashentry() entry = tb.getcrashentry()
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 entry.frame.code.path == co.path
assert entry.lineno == co.firstlineno + 1 assert entry.lineno == co.firstlineno + 1