diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2012-07-15 11:19:18 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2012-07-15 11:19:18 (GMT) |
commit | 8ecf50474ce3d0ef34fbcce940566c70370e57ad (patch) | |
tree | 51f2817b0238f406b5e673e703996ec7bc2a8c8b /Lib/pkgutil.py | |
parent | 3f94cbf9eba7adef027cfc5d087b3660800df9d7 (diff) | |
download | cpython-8ecf50474ce3d0ef34fbcce940566c70370e57ad.zip cpython-8ecf50474ce3d0ef34fbcce940566c70370e57ad.tar.gz cpython-8ecf50474ce3d0ef34fbcce940566c70370e57ad.tar.bz2 |
Issue #15343: Handle importlib.machinery.FileFinder instances in pkgutil.walk_packages (et al)
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r-- | Lib/pkgutil.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index 487c8cd..8407b6d 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 |