'saferepr' handles classes with broken __getattribute__

Fix #7145
This commit is contained in:
Bruno Oliveira 2020-05-05 19:20:34 -03:00
parent 4787fd64a4
commit d0022b5a13
3 changed files with 19 additions and 1 deletions

View File

@ -0,0 +1 @@
Classes with broken ``__getattribute__`` methods are displayed correctly during failures.

View File

@ -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)
)

View File

@ -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"
)