summaryrefslogtreecommitdiff
path: root/Lib/pkgutil.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-07-15 21:19:18 +1000
committerNick Coghlan <ncoghlan@gmail.com>2012-07-15 21:19:18 +1000
commit8ecf50474ce3d0ef34fbcce940566c70370e57ad (patch)
tree51f2817b0238f406b5e673e703996ec7bc2a8c8b /Lib/pkgutil.py
parent3f94cbf9eba7adef027cfc5d087b3660800df9d7 (diff)
downloadcpython-git-8ecf50474ce3d0ef34fbcce940566c70370e57ad.tar.gz
Issue #15343: Handle importlib.machinery.FileFinder instances in pkgutil.walk_packages (et al)
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r--Lib/pkgutil.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index 487c8cd8a9..8407b6d0b3 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -157,6 +157,49 @@ def iter_importer_modules(importer, prefix=''):
iter_importer_modules = simplegeneric(iter_importer_modules)
+# Implement a file walker for the normal importlib path hook
+def _iter_file_finder_modules(importer, prefix=''):
+ if importer.path is None or not os.path.isdir(importer.path):
+ return
+
+ yielded = {}
+ import inspect
+ try:
+ filenames = os.listdir(importer.path)
+ except OSError:
+ # ignore unreadable directories like import does
+ filenames = []
+ filenames.sort() # handle packages before same-named modules
+
+ for fn in filenames:
+ modname = inspect.getmodulename(fn)
+ if modname=='__init__' or modname in yielded:
+ continue
+
+ path = os.path.join(importer.path, fn)
+ ispkg = False
+
+ if not modname and os.path.isdir(path) and '.' not in fn:
+ modname = fn
+ try:
+ dircontents = os.listdir(path)
+ except OSError:
+ # ignore unreadable directories like import does
+ dircontents = []
+ for fn in dircontents:
+ subname = inspect.getmodulename(fn)
+ if subname=='__init__':
+ ispkg = True
+ break
+ else:
+ continue # not a package
+
+ if modname and '.' not in modname:
+ yielded[modname] = 1
+ yield prefix + modname, ispkg
+
+iter_importer_modules.register(
+ importlib.machinery.FileFinder, _iter_file_finder_modules)
class ImpImporter:
"""PEP 302 Importer that wraps Python's "classic" import algorithm