Skip to content

Commit 2d690b8

Browse files
committed
ExceptionInfo.from_current: assert current exception
1 parent 15d6088 commit 2d690b8

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/_pytest/_code/code.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ def from_current(cls, exprinfo=None):
413413
to the exception message/``__str__()``
414414
"""
415415
tup = sys.exc_info()
416+
assert tup[0] is not None, "no current exception"
416417
_striptext = ""
417418
if exprinfo is None and isinstance(tup[1], AssertionError):
418419
exprinfo = getattr(tup[1], "msg", None)

testing/code/test_code.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ def test_bad_getsource(self):
172172
exci = _pytest._code.ExceptionInfo.from_current()
173173
assert exci.getrepr()
174174

175+
def test_from_current_with_missing(self):
176+
with pytest.raises(AssertionError, match="no current exception"):
177+
_pytest._code.ExceptionInfo.from_current()
178+
175179

176180
class TestTracebackEntry(object):
177181
def test_getsource(self):

0 commit comments

Comments
 (0)