monkeypatch: unnest handling code

this avoid python3 nested exceptions
This commit is contained in:
Ronny Pfannschmidt 2016-01-24 12:28:14 +01:00
parent cb6181255e
commit 2d05f831fe
1 changed files with 17 additions and 12 deletions

View File

@ -42,18 +42,23 @@ def resolve(name):
try: try:
found = getattr(found, part) found = getattr(found, part)
except AttributeError: except AttributeError:
try: pass
__import__(used) else:
except ImportError as ex: continue
# str is used for py2 vs py3 # we use explicit un-nesting of the handling block in order
expected = str(ex).split()[-1] # to avoid nested exceptions on python 3
if expected == used: try:
raise __import__(used)
else: except ImportError as ex:
raise ImportError( # str is used for py2 vs py3
'import error in %s: %s' % (used, ex) expected = str(ex).split()[-1]
) if expected == used:
found = annotated_getattr(found, part, used) raise
else:
raise ImportError(
'import error in %s: %s' % (used, ex)
)
found = annotated_getattr(found, part, used)
return found return found