From 06415bb720fb7bd3d04a9d326efe7d8cbca347a9 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 20 Jan 2019 15:53:11 +0000 Subject: [PATCH] issue #310: fix test failures, teach old import method new tricks - don't try anything unless something really lives in sys.modules by that name - non-ASCII files are possible - the unimportable thing might be an extension module, we don't want that --- mitogen/master.py | 22 +++++++++++++++++++++- tests/data/pkg_like_plumbum/colors.py | 4 ++++ tests/module_finder_test.py | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/mitogen/master.py b/mitogen/master.py index a1ed2dfb..1b6aaa61 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -459,6 +459,11 @@ class ModuleFinder(object): insane) parent package. Required for older versions of ansible.compat.six and plumbum.colors. """ + if fullname not in sys.modules: + # Don't attempt this unless a module really exists in sys.modules, + # else we could return junk. + return + pkgname, _, modname = fullname.rpartition('.') pkg = sys.modules.get(pkgname) if pkg is None or not hasattr(pkg, '__file__'): @@ -467,7 +472,21 @@ class ModuleFinder(object): pkg_path = os.path.dirname(pkg.__file__) try: fp, path, ext = imp.find_module(modname, [pkg_path]) - return path, fp.read().encode('utf-8'), False + try: + path = self._py_filename(path) + if not path: + fp.close() + return + + source = fp.read() + finally: + fp.close() + + if isinstance(source, mitogen.core.UnicodeType): + # get_source() returns "string" according to PEP-302, which was + # reinterpreted for Python 3 to mean a Unicode string. + source = source.encode('utf-8') + return path, source, False except ImportError: e = sys.exc_info()[1] LOG.debug('imp.find_module(%r, %r) -> %s', modname, [pkg_path], e) @@ -491,6 +510,7 @@ class ModuleFinder(object): for method in self.get_module_methods: tup = method(self, fullname) if tup: + #LOG.debug('%r returned %r', method, tup) break else: tup = None, None, None diff --git a/tests/data/pkg_like_plumbum/colors.py b/tests/data/pkg_like_plumbum/colors.py index b0fc4ce6..bff19555 100644 --- a/tests/data/pkg_like_plumbum/colors.py +++ b/tests/data/pkg_like_plumbum/colors.py @@ -1,7 +1,11 @@ +# coding=utf-8 + import sys +# £ + class EvilObject(object): """ Wild cackles! I have come to confuse perplex your importer with rainbows! diff --git a/tests/module_finder_test.py b/tests/module_finder_test.py index efc60af0..f452a38c 100644 --- a/tests/module_finder_test.py +++ b/tests/module_finder_test.py @@ -133,8 +133,8 @@ class GetModuleViaParentEnumerationTest(testlib.TestCase): self.assertEquals(path, testlib.data_path('pkg_like_plumbum/colors.py')) - s = open(testlib.data_path('pkg_like_plumbum/colors.py')).read() - self.assertEquals(mitogen.core.to_text(src), s) + s = open(testlib.data_path('pkg_like_plumbum/colors.py'), 'rb').read() + self.assertEquals(src, s) self.assertFalse(is_pkg)