From 2d05f831fed55911bb3dd4a5cf905aeee8cdcc88 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 24 Jan 2016 12:28:14 +0100 Subject: [PATCH] monkeypatch: unnest handling code this avoid python3 nested exceptions --- _pytest/monkeypatch.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/_pytest/monkeypatch.py b/_pytest/monkeypatch.py index 4431d25b3..d4c169d37 100644 --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -42,18 +42,23 @@ def resolve(name): try: found = getattr(found, part) except AttributeError: - try: - __import__(used) - except ImportError as ex: - # str is used for py2 vs py3 - expected = str(ex).split()[-1] - if expected == used: - raise - else: - raise ImportError( - 'import error in %s: %s' % (used, ex) - ) - found = annotated_getattr(found, part, used) + pass + else: + continue + # we use explicit un-nesting of the handling block in order + # to avoid nested exceptions on python 3 + try: + __import__(used) + except ImportError as ex: + # str is used for py2 vs py3 + expected = str(ex).split()[-1] + if expected == used: + raise + else: + raise ImportError( + 'import error in %s: %s' % (used, ex) + ) + found = annotated_getattr(found, part, used) return found