Skip to content

Commit

Permalink
fix pytest-dev#4386 - handle uninitialized exceptioninfo in repr/str
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Nov 22, 2018
1 parent 3eaa6d8 commit c17d3ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ def __init__(self, tup=None, exprinfo=None):
self.traceback = _pytest._code.Traceback(self.tb, excinfo=ref(self))

def __repr__(self):
return "<ExceptionInfo %s tblen=%d>" % (self.typename, len(self.traceback))
try:
return "<ExceptionInfo %s tblen=%d>" % (self.typename, len(self.traceback))
except AttributeError:
return "<ExceptionInfo uninitialized>"

def exconly(self, tryshort=False):
""" return the exception as a string
Expand Down Expand Up @@ -513,9 +516,13 @@ def getrepr(
return fmt.repr_excinfo(self)

def __str__(self):
entry = self.traceback[-1]
loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly())
return str(loc)
try:
entry = self.traceback[-1]
except AttributeError:
return repr(self)
else:
loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly())
return str(loc)

def __unicode__(self):
entry = self.traceback[-1]
Expand Down
9 changes: 9 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def __call__(self):
except pytest.raises.Exception:
pass

def test_raises_repr_inflight(self):
with pytest.raises(RuntimeError) as excinfo:
print(str(excinfo))
print(repr(excinfo))
import pprint

pprint.pprint(excinfo)
raise RuntimeError(1)

def test_raises_as_contextmanager(self, testdir):
testdir.makepyfile(
"""
Expand Down

0 comments on commit c17d3ab

Please sign in to comment.