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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

'saferepr' handles classes with broken __getattribute__ #7168

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7145.bugfix.rst
@@ -0,0 +1 @@
Classes with broken ``__getattribute__`` methods are displayed correctly during failures.
2 changes: 1 addition & 1 deletion src/_pytest/_io/saferepr.py
Expand Up @@ -20,7 +20,7 @@ def _format_repr_exception(exc: BaseException, obj: Any) -> str:
except BaseException as exc:
exc_info = "unpresentable exception ({})".format(_try_repr_or_str(exc))
return "<[{} raised in repr()] {} object at 0x{:x}>".format(
exc_info, obj.__class__.__name__, id(obj)
exc_info, type(obj).__name__, id(obj)
)


Expand Down
17 changes: 17 additions & 0 deletions testing/io/test_saferepr.py
Expand Up @@ -154,3 +154,20 @@ def test_pformat_dispatch():
assert _pformat_dispatch("a") == "'a'"
assert _pformat_dispatch("a" * 10, width=5) == "'aaaaaaaaaa'"
assert _pformat_dispatch("foo bar", width=5) == "('foo '\n 'bar')"


def test_broken_getattribute():
"""saferepr() can create proper representations of classes with
broken __getattribute__ (#7145)
"""

class SomeClass:
def __getattribute__(self, attr):
raise RuntimeError

def __repr__(self):
raise RuntimeError

assert saferepr(SomeClass()).startswith(
"<[RuntimeError() raised in repr()] SomeClass object at 0x"
)