[modutils] add pkgutil.extend_path support. Closes #8796

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changeset878ef72bc997
branchstable
phasepublic
hiddenno
parent revision#cd9817f175b4 [modutils] setuptools pkg_resources support. Closes #8796
child revision#ad5762700383 Update version number in spec file
files modified by this revision
modutils.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1371484984 -7200
# Mon Jun 17 18:03:04 2013 +0200
# Branch stable
# Node ID 878ef72bc997e3fb001b76eba1cd9fc311e91735
# Parent cd9817f175b45acf827df04cc14847b87356aafe
[modutils] add pkgutil.extend_path support. Closes #8796

diff --git a/modutils.py b/modutils.py
@@ -615,21 +615,22 @@
1          # __path__, get back information from there
2          module = sys.modules[modpath.pop(0)]
3          path = module.__path__
4      imported = []
5      while modpath:
6 +        modname = modpath[0]
7          # take care to changes in find_module implementation wrt builtin modules
8          #
9          # Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
10          # >>> imp.find_module('posix')
11          # (None, 'posix', ('', '', 6))
12          #
13          # Python 3.3.1 (default, Apr 26 2013, 12:08:46)
14          # >>> imp.find_module('posix')
15          # (None, None, ('', '', 6))
16          try:
17 -            _, mp_filename, mp_desc = find_module(modpath[0], path)
18 +            _, mp_filename, mp_desc = find_module(modname, path)
19          except ImportError:
20              if checkeggs:
21                  return _search_zip(modpath, pic)[:2]
22              raise
23          else:
@@ -651,11 +652,20 @@
24          mtype = mp_desc[2]
25          if modpath:
26              if mtype != PKG_DIRECTORY:
27                  raise ImportError('No module %s in %s' % ('.'.join(modpath),
28                                                            '.'.join(imported)))
29 -            path = [mp_filename]
30 +            # XXX guess if package is using pkgutil.extend_path by looking for
31 +            # those keywords in the first four Kbytes
32 +            data = open(join(mp_filename, '__init__.py')).read(4096)
33 +            if 'pkgutil' in data and 'extend_path' in data:
34 +                # extend_path is called, search sys.path for module/packages of this name
35 +                # see pkgutil.extend_path documentation
36 +                path = [join(p, modname) for p in sys.path
37 +                        if isdir(join(p, modname))]
38 +            else:
39 +                path = [mp_filename]
40      return mtype, mp_filename
41 
42  def _is_python_file(filename):
43      """return true if the given filename should be considered as a python file
44