From 3886c6d73579580df4f50b9f6f6667d422780c9d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 6 May 2020 17:57:38 -0300 Subject: [PATCH] Merge pull request #7168 from nicoddemus/saferepr-getattr-fail-7145 --- changelog/7145.bugfix.rst | 1 + src/_pytest/_io/saferepr.py | 2 +- testing/io/test_saferepr.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 changelog/7145.bugfix.rst diff --git a/changelog/7145.bugfix.rst b/changelog/7145.bugfix.rst new file mode 100644 index 00000000000..def237dc0c9 --- /dev/null +++ b/changelog/7145.bugfix.rst @@ -0,0 +1 @@ +Classes with broken ``__getattribute__`` methods are displayed correctly during failures. diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index 23af4d0bb70..47a00de6063 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -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) ) diff --git a/testing/io/test_saferepr.py b/testing/io/test_saferepr.py index 06084202eb7..f4ced8facdf 100644 --- a/testing/io/test_saferepr.py +++ b/testing/io/test_saferepr.py @@ -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" + )