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)