From e88d1c2c7078f3e7367b6fb2828b5cea2954606a Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 2 Sep 2014 21:33:48 -0500 Subject: [PATCH] Fix lazy module behavior after failure. --- earwigbot/lazy.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/earwigbot/lazy.py b/earwigbot/lazy.py index f66fafe..e5c821f 100644 --- a/earwigbot/lazy.py +++ b/earwigbot/lazy.py @@ -36,11 +36,21 @@ __all__ = ["LazyImporter"] _real_get = ModuleType.__getattribute__ +def _create_failing_get(exc): + def _fail(self, attr): + raise exc + return _fail + def _mock_get(self, attr): with _real_get(self, "_lock"): if _real_get(self, "_unloaded"): type(self)._unloaded = False - reload(self) + try: + reload(self) + except ImportError as exc: + type(self).__getattribute__ = _create_failing_get(exc) + del type(self)._lock + raise type(self).__getattribute__ = _real_get del type(self)._lock return _real_get(self, attr)