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
issue510
David Wilson 6 years ago
parent 6af1a64cce
commit 06415bb720

@ -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

@ -1,7 +1,11 @@
# coding=utf-8
import sys
# £
class EvilObject(object):
"""
Wild cackles! I have come to confuse perplex your importer with rainbows!

@ -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)

Loading…
Cancel
Save