Improve ExceptionInfo.__repr__
This commit is contained in:
parent
5c92a0f695
commit
2a2fe7d3db
|
@ -0,0 +1 @@
|
||||||
|
``repr`` of ``ExceptionInfo`` objects has been improved to honor the ``__repr__`` method of the underlying exception.
|
|
@ -507,7 +507,9 @@ class ExceptionInfo(Generic[_E]):
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
if self._excinfo is None:
|
if self._excinfo is None:
|
||||||
return "<ExceptionInfo for raises contextmanager>"
|
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:
|
def exconly(self, tryshort: bool = False) -> str:
|
||||||
""" return the exception as a string
|
""" return the exception as a string
|
||||||
|
|
|
@ -316,8 +316,19 @@ def test_excinfo_exconly():
|
||||||
|
|
||||||
def test_excinfo_repr_str():
|
def test_excinfo_repr_str():
|
||||||
excinfo = pytest.raises(ValueError, h)
|
excinfo = pytest.raises(ValueError, h)
|
||||||
assert repr(excinfo) == "<ExceptionInfo ValueError tblen=4>"
|
assert repr(excinfo) == "<ExceptionInfo ValueError() tblen=4>"
|
||||||
assert str(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():
|
def test_excinfo_for_later():
|
||||||
|
|
Loading…
Reference in New Issue