summaryrefslogtreecommitdiff
path: root/modutils.py
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2013-06-17 18:03:04 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2013-06-17 18:03:04 +0200
commitfb8c4ee84abfc8f73ede8460cf718b90fb2b8ef9 (patch)
tree6f608b471366c742cd7c274db648d14e56d0450b /modutils.py
parent1e207fd9fa6d8c6c56e7706a781652fb8b6ad109 (diff)
downloadlogilab-common-fb8c4ee84abfc8f73ede8460cf718b90fb2b8ef9.tar.gz
[modutils] add pkgutil.extend_path support. Closes #8796
Diffstat (limited to 'modutils.py')
-rw-r--r--modutils.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/modutils.py b/modutils.py
index 834b5d3..9d0bb49 100644
--- a/modutils.py
+++ b/modutils.py
@@ -617,6 +617,7 @@ def _module_file(modpath, path=None):
path = module.__path__
imported = []
while modpath:
+ modname = modpath[0]
# take care to changes in find_module implementation wrt builtin modules
#
# Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
@@ -627,7 +628,7 @@ def _module_file(modpath, path=None):
# >>> imp.find_module('posix')
# (None, None, ('', '', 6))
try:
- _, mp_filename, mp_desc = find_module(modpath[0], path)
+ _, mp_filename, mp_desc = find_module(modname, path)
except ImportError:
if checkeggs:
return _search_zip(modpath, pic)[:2]
@@ -653,7 +654,16 @@ def _module_file(modpath, path=None):
if mtype != PKG_DIRECTORY:
raise ImportError('No module %s in %s' % ('.'.join(modpath),
'.'.join(imported)))
- path = [mp_filename]
+ # XXX guess if package is using pkgutil.extend_path by looking for
+ # those keywords in the first four Kbytes
+ data = open(join(mp_filename, '__init__.py')).read(4096)
+ if 'pkgutil' in data and 'extend_path' in data:
+ # extend_path is called, search sys.path for module/packages of this name
+ # see pkgutil.extend_path documentation
+ path = [join(p, modname) for p in sys.path
+ if isdir(join(p, modname))]
+ else:
+ path = [mp_filename]
return mtype, mp_filename
def _is_python_file(filename):