From d26fe5b993393679069914eb2f632159555640a8 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 19 Jul 2018 15:17:09 -0400 Subject: [PATCH] issue #310: fix negative imports on Python 3.x. On 3.x, Importer() can still have its methods called even if load_module() raises ImportError. Closes #310. --- mitogen/core.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mitogen/core.py b/mitogen/core.py index c0b864f7..dd706311 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -815,10 +815,21 @@ class Importer(object): def get_filename(self, fullname): if fullname in self._cache: + path = self._cache[fullname][2] + if path is None: + # If find_loader() returns self but a subsequent master RPC + # reveals the module can't be loaded, and so load_module() + # throws ImportError, on Python 3.x it is still possible for + # the loader to be called to fetch metadata. + raise ImportError('master cannot serve %r' % (fullname,)) return u'master:' + self._cache[fullname][2] def get_source(self, fullname): if fullname in self._cache: + compressed = self._cache[fullname][3] + if compressed is None: + raise ImportError('master cannot serve %r' % (fullname,)) + source = zlib.decompress(self._cache[fullname][3]) if PY3: return to_text(source)