Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ExceptionInfo.__str__, falling back to __repr__ #5413

Merged
merged 1 commit into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/5412.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``ExceptionInfo`` objects (returned by ``pytest.raises``) now have the same ``str`` representation as ``repr``, which
avoids some confusion when users use ``print(e)`` to inspect the object.
7 changes: 0 additions & 7 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,6 @@ def getrepr(
)
return fmt.repr_excinfo(self)

def __str__(self):
if self._excinfo is None:
return repr(self)
entry = self.traceback[-1]
loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly())
return str(loc)

def match(self, regexp):
"""
Check whether the regular expression 'regexp' is found in the string
Expand Down
14 changes: 3 additions & 11 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,10 @@ def test_excinfo_exconly():
assert msg.endswith("world")


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


def test_excinfo_str():
excinfo = pytest.raises(ValueError, h)
s = str(excinfo)
assert s.startswith(__file__[:-9]) # pyc file and $py.class
assert s.endswith("ValueError")
assert len(s.split(":")) >= 3 # on windows it's 4
assert repr(excinfo) == "<ExceptionInfo ValueError tblen=4>"
assert str(excinfo) == "<ExceptionInfo ValueError tblen=4>"


def test_excinfo_for_later():
Expand Down