Don't fail if hasattr fails when rewriting assertions

When rewriting assertions, pytest makes a call to
`__name__` on each object in a comparision. If one of
the objects has reimplemented `__getattr__`, they could
fail trying to fetch `__name__` with an error other than
`AttributeError`, which is what `hasattr` catches.

In this case, the stack trace for the failed `__getattr__`
call will show up in the pytest output, even though
it isn't related to the test failing.

This change fixes that by catching exceptions
that `hasattr` throws.
This commit is contained in:
Anton Lodder 2019-01-10 14:13:17 -05:00 committed by AnjoMan
parent acb3e8e8a7
commit 3241fc3103
1 changed files with 7 additions and 1 deletions

View File

@ -525,7 +525,13 @@ def _format_assertmsg(obj):
def _should_repr_global_name(obj):
return not hasattr(obj, "__name__") and not callable(obj)
if callable(obj):
return False
try:
return not hasattr(obj, "__name__")
except Exception:
return True
def _format_boolop(explanations, is_or):