Refactor try-except clause

This commit is contained in:
Leo Huckvale 2016-07-15 09:21:48 +01:00
parent fa89582d57
commit 725599fa96

View File

@ -111,16 +111,18 @@ def safe_getattr(obj, name, *defargs):
# sometimes accessing a property raises an exception (e.g.
# NotImplementedError), so let's try to read the attribute directly
# We should also be aware that if `__dict__` has been overridden,
# this may also raise an exception.
try:
# In case the object does weird things with attribute access
# such that accessing `obj.__dict__` may raise an exception
obj_dict = obj.__dict__
except Exception:
obj_dict = {}
if name in obj_dict:
return obj.__dict__[name]
# This is a broad `except` clause, but we're being specific about
# where we catch it
pass
else:
# We don't want to catch any exceptions here
if name in obj_dict:
return obj_dict[name]
# this is a catch-all for all the weird things that some modules do
# with attribute access