Skip to content

Commit 3a060b7

Browse files
Zac-HDnicoddemus
andauthored
Revert change to traceback repr (#7535)
* Revert change to traceback repr * Add test and changelog entry * Restore *exact* prev output Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 7ec6401 commit 3a060b7

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

changelog/7534.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restored the previous formatting of ``TracebackEntry.__str__`` which was changed by accident.

src/_pytest/_code/code.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,15 @@ def __str__(self) -> str:
262262
raise
263263
except BaseException:
264264
line = "???"
265-
return " File %r:%d in %s\n %s\n" % (self.path, self.lineno + 1, name, line)
265+
# This output does not quite match Python's repr for traceback entries,
266+
# but changing it to do so would break certain plugins. See
267+
# https://github.com/pytest-dev/pytest/pull/7535/ for details.
268+
return " File %r:%d in %s\n %s\n" % (
269+
str(self.path),
270+
self.lineno + 1,
271+
name,
272+
line,
273+
)
266274

267275
@property
268276
def name(self) -> str:

testing/code/test_code.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import sys
23
from types import FrameType
34
from unittest import mock
@@ -170,6 +171,15 @@ def test_getsource(self) -> None:
170171
assert len(source) == 6
171172
assert "assert False" in source[5]
172173

174+
def test_tb_entry_str(self):
175+
try:
176+
assert False
177+
except AssertionError:
178+
exci = ExceptionInfo.from_current()
179+
pattern = r" File '.*test_code.py':\d+ in test_tb_entry_str\n assert False"
180+
entry = str(exci.traceback[0])
181+
assert re.match(pattern, entry)
182+
173183

174184
class TestReprFuncArgs:
175185
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:

0 commit comments

Comments
 (0)