From 828f60351b7e2fd0e5c69c43955285a51fb91c2e Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 8 Oct 2017 16:31:38 +0530 Subject: [PATCH] importer: Delete _get_module_via_parent entirely Can't figure out what it's supposed to do any more, and can't find a version of Ansible before August 2016 (when I wrote that code) that seems to need it. Add some more mitigations to avoid sending dylibs. --- mitogen/master.py | 36 +++++++++--------------------------- tests/module_finder_test.py | 23 ----------------------- 2 files changed, 9 insertions(+), 50 deletions(-) diff --git a/mitogen/master.py b/mitogen/master.py index c1af3eb9..ccefbda1 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -401,6 +401,11 @@ class ModuleFinder(object): return False + def _py_filename(self, path): + path = path.rstrip('co') + if path.endswith('.py'): + return path + def _get_module_via_pkgutil(self, fullname): """Attempt to fetch source code via pkgutil. In an ideal world, this would be the only required implementation of get_module().""" @@ -410,7 +415,7 @@ class ModuleFinder(object): return try: - path = loader.get_filename(fullname) + path = self._py_filename(loader.get_filename(fullname)) source = loader.get_source(fullname) if path is not None and source is not None: return path, source, loader.is_package(fullname) @@ -426,9 +431,8 @@ class ModuleFinder(object): fullname) return - modpath = getattr(module, '__file__', '') - if not modpath.rstrip('co').endswith('.py'): - # Probably a native module. + modpath = self._py_filename(getattr(module, '__file__', '')) + if not modpath: return is_pkg = hasattr(module, '__path__') @@ -444,30 +448,8 @@ class ModuleFinder(object): source, hasattr(module, '__path__')) - def _get_module_via_parent(self, fullname): - """Attempt to fetch source code by examining the module's (hopefully - less insane) parent package. Required for ansible.compat.six.""" - # Need to find the ancient version of Ansible with the ancient - # non-package version of six that required this method to exist. - # Currently it doesn't seem to be needed at all, and it's broken for - # packages. - pkgname, _, modname = fullname.rpartition('.') - pkg = sys.modules.get(pkgname) - if not (isinstance(pkg, types.ModuleType) and hasattr(pkg, '__file__')): - return - - pkg_path = os.path.dirname(pkg.__file__) - try: - fp, path, ext = imp.find_module(modname, [pkg_path]) - if ext and ext[-1] == imp.PKG_DIRECTORY: - assert 0, "TODO" - return path, fp.read(), False - except ImportError, e: - LOG.debug('imp.find_module(%r, %r) -> %s', modname, [pkg_path], e) - get_module_methods = [_get_module_via_pkgutil, - _get_module_via_sys_modules, - _get_module_via_parent] + _get_module_via_sys_modules] def get_module_source(self, fullname): """Given the name of a loaded module `fullname`, attempt to find its diff --git a/tests/module_finder_test.py b/tests/module_finder_test.py index 6dff80c5..9cc6c58a 100644 --- a/tests/module_finder_test.py +++ b/tests/module_finder_test.py @@ -103,29 +103,6 @@ class GetModuleViaSysModulesTest(testlib.TestCase): self.assertEquals(None, tup) -class GetModuleViaParentEnumerationTest(testlib.TestCase): - klass = mitogen.master.ModuleFinder - - def call(self, fullname): - return self.klass()._get_module_via_parent(fullname) - - def test_simple_module(self): - import email.utils - path, src, is_pkg = self.call('email.utils') - self.assertEquals(path, email.utils.__file__.rstrip('co')) - self.assertEquals(src, file(email.utils.__file__.rstrip('co')).read()) - self.assertFalse(is_pkg) - - def test_ansible_compat_six(self): - # See comment in _get_module_via_parent - raise unittest.SkipTest() - import ansible.compat.six - path, src, is_pkg = self.call('ansible.compat.six') - self.assertEquals(path, __main__.__file__) - self.assertEquals(src, file(path).read()) - self.assertFalse(is_pkg) - - class ResolveRelPathTest(testlib.TestCase): klass = mitogen.master.ModuleFinder