Skip to content

Commit

Permalink
Improve ExceptionInfo.__repr__
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Oct 9, 2019
1 parent 5c92a0f commit 6a8080d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ def traceback(self, value: Traceback) -> None:
def __repr__(self) -> str:
if self._excinfo is None:
return "<ExceptionInfo for raises contextmanager>"
return "<ExceptionInfo %s tblen=%d>" % (self.typename, len(self.traceback))
return "<{} {} tblen={}>".format(
self.__class__.__name__, saferepr(self._excinfo[1]), len(self.traceback)
)

def exconly(self, tryshort: bool = False) -> str:
""" return the exception as a string
Expand Down
15 changes: 13 additions & 2 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,19 @@ def test_excinfo_exconly():

def test_excinfo_repr_str():
excinfo = pytest.raises(ValueError, h)
assert repr(excinfo) == "<ExceptionInfo ValueError tblen=4>"
assert str(excinfo) == "<ExceptionInfo ValueError tblen=4>"
assert repr(excinfo) == "<ExceptionInfo ValueError() tblen=4>"
assert str(excinfo) == "<ExceptionInfo ValueError() tblen=4>"

class CustomException(Exception):
def __repr__(self):
return "custom_repr"

def raises():
raise CustomException()

excinfo = pytest.raises(CustomException, raises)
assert repr(excinfo) == "<ExceptionInfo custom_repr tblen=2>"
assert str(excinfo) == "<ExceptionInfo custom_repr tblen=2>"


def test_excinfo_for_later():
Expand Down

0 comments on commit 6a8080d

Please sign in to comment.