From 9fb2371d64d63dafca0a0f663e42c3e8318375cd Mon Sep 17 00:00:00 2001 From: David Wilson Date: Tue, 26 Jun 2018 03:22:44 +0100 Subject: [PATCH] importer: reorder/tweak find_module() tests to cope with six.moves The old hack on the master side we had is broken for some reason on 3.x. Instead tweak the client to be more selective: if a request is for a module within a package, the package must be loaded (in sys.modules), and its __loader__ must be us. Previously if the module didn't exist in sys.modules, we'd still try to fetch from the master, which doesn't appear to ever make sense. --- mitogen/core.py | 28 +++++++++++++--------------- mitogen/master.py | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index db3d4be6..af7bf28e 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -682,27 +682,25 @@ class Importer(object): _tls.running = True try: - pkgname, dot, _ = fullname.rpartition('.') - _vv and IOLOG.debug('%r.find_module(%r)', self, fullname) - suffix = fullname[len(pkgname+dot):] - if suffix not in self._present.get(pkgname, (suffix,)): - _v and LOG.debug('%r: master doesn\'t know %r', self, fullname) - return None - - pkg = sys.modules.get(pkgname) - if pkg and getattr(pkg, '__loader__', None) is not self: - _vv and IOLOG.debug( - '%r: %r is submodule of a package we did not load', - self, fullname - ) - return None - + _v and LOG.debug('%r.find_module(%r)', self, fullname) # #114: explicitly whitelisted prefixes override any # system-installed package. if self.whitelist != ['']: if any(fullname.startswith(s) for s in self.whitelist): return self + pkgname, dot, _ = fullname.rpartition('.') + pkg = sys.modules.get(pkgname) + if pkgname and getattr(pkg, '__loader__', None) is not self: + LOG.debug('%r: %r is submodule of a package we did not load', + self, fullname) + return None + + suffix = fullname[len(pkgname+dot):] + if pkgname and suffix not in self._present.get(pkgname, ()): + _v and LOG.debug('%r: master doesn\'t know %r', self, fullname) + return None + try: self.builtin_find_module(fullname) _vv and IOLOG.debug('%r: %r is available locally', diff --git a/mitogen/master.py b/mitogen/master.py index 25bf8e83..46b1e84b 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -471,7 +471,7 @@ class ModuleFinder(object): for name in maybe_names if sys.modules.get(name) is not None and not is_stdlib_name(name) - and 'six.moves' not in name # TODO: crap + and u'six.moves' not in name # TODO: crap ) ))