Skip to content

Commit c53d52c

Browse files
authored
Merge pull request #7174 from nicoddemus/backport-7168
2 parents 80936b6 + 3886c6d commit c53d52c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

changelog/7145.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Classes with broken ``__getattribute__`` methods are displayed correctly during failures.

src/_pytest/_io/saferepr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _format_repr_exception(exc: BaseException, obj: Any) -> str:
2020
except BaseException as exc:
2121
exc_info = "unpresentable exception ({})".format(_try_repr_or_str(exc))
2222
return "<[{} raised in repr()] {} object at 0x{:x}>".format(
23-
exc_info, obj.__class__.__name__, id(obj)
23+
exc_info, type(obj).__name__, id(obj)
2424
)
2525

2626

testing/io/test_saferepr.py

+17
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,20 @@ def test_pformat_dispatch():
154154
assert _pformat_dispatch("a") == "'a'"
155155
assert _pformat_dispatch("a" * 10, width=5) == "'aaaaaaaaaa'"
156156
assert _pformat_dispatch("foo bar", width=5) == "('foo '\n 'bar')"
157+
158+
159+
def test_broken_getattribute():
160+
"""saferepr() can create proper representations of classes with
161+
broken __getattribute__ (#7145)
162+
"""
163+
164+
class SomeClass:
165+
def __getattribute__(self, attr):
166+
raise RuntimeError
167+
168+
def __repr__(self):
169+
raise RuntimeError
170+
171+
assert saferepr(SomeClass()).startswith(
172+
"<[RuntimeError() raised in repr()] SomeClass object at 0x"
173+
)

0 commit comments

Comments
 (0)