diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index e37ad45..014d86b 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -142,7 +142,7 @@ def _iter_file_finder_modules(importer, prefix=''): if importer.path is None or not os.path.isdir(importer.path): return - yielded = {} + yielded = set() import inspect try: filenames = os.listdir(importer.path) @@ -152,31 +152,37 @@ def _iter_file_finder_modules(importer, prefix=''): 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 + modname = inspect.getmodulename(fn) - if not modname and os.path.isdir(path) and '.' not in fn: - modname = fn + if modname: + if modname == '__init__' or '.' in modname or modname in yielded: + continue + if os.path.isdir(path): + continue + ispkg = False + elif '.' not in fn and os.path.isdir(path): 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__': + for subfn in dircontents: + subname = inspect.getmodulename(subfn) + if subname == '__init__': # and os.path.isfile( + # os.path.join(path, subfn) + # ): + modname = fn ispkg = True break else: continue # not a package + else: + continue + + yielded.add(modname) + yield prefix + modname, ispkg - if modname and '.' not in modname: - yielded[modname] = 1 - yield prefix + modname, ispkg iter_importer_modules.register( importlib.machinery.FileFinder, _iter_file_finder_modules) @@ -222,44 +228,7 @@ class ImpImporter: return ImpLoader(fullname, file, filename, etc) def iter_modules(self, prefix=''): - if self.path is None or not os.path.isdir(self.path): - return - - yielded = {} - import inspect - try: - filenames = os.listdir(self.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(self.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 + yield from _iter_file_finder_modules(self, prefix) class ImpLoader: