diff -r 168efd87e051 Doc/library/pkgutil.rst --- a/Doc/library/pkgutil.rst Fri Feb 15 23:38:23 2013 +0200 +++ b/Doc/library/pkgutil.rst Sat Feb 16 20:39:48 2013 +0530 @@ -11,6 +11,8 @@ This module provides utilities for the import system, in particular package support. +.. class:: ModuleInfo + A namedtuple consisting of ``(module_finder, name, ispkg)``. .. function:: extend_path(path, name) @@ -138,7 +140,7 @@ .. function:: iter_modules(path=None, prefix='') - Yields ``(module_finder, name, ispkg)`` for all submodules on *path*, or, if + Yields a ``ModuleInfo`` for all submodules on *path*, or, if path is ``None``, all top-level modules on ``sys.path``. *path* should be either ``None`` or a list of paths to look for modules in. @@ -158,7 +160,7 @@ .. function:: walk_packages(path=None, prefix='', onerror=None) - Yields ``(module_finder, name, ispkg)`` for all modules recursively on + Yields a ``ModuleInfo`` for all modules recursively on *path*, or, if path is ``None``, all accessible modules. *path* should be either ``None`` or a list of paths to look for modules in. diff -r 168efd87e051 Lib/pkgutil.py --- a/Lib/pkgutil.py Fri Feb 15 23:38:23 2013 +0200 +++ b/Lib/pkgutil.py Sat Feb 16 20:39:48 2013 +0530 @@ -7,13 +7,16 @@ import os.path from warnings import warn from types import ModuleType +from collections import namedtuple __all__ = [ 'get_importer', 'iter_importers', 'get_loader', 'find_loader', 'walk_packages', 'iter_modules', 'get_data', - 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path', + 'ImpImporter', 'ImpLoader', 'ModuleInfo', 'read_code', 'extend_path', ] +ModuleInfo = namedtuple("ModuleInfo", "module_loader name ispkg") + def read_code(stream): # This helper is needed in order for the PEP 302 emulation to # correctly handle compiled files @@ -68,7 +71,7 @@ def walk_packages(path=None, prefix='', onerror=None): - """Yields (module_loader, name, ispkg) for all modules recursively + """Yields a ModuleInfo for all modules recursively on path, or, if path is None, all accessible modules. 'path' should be either None or a list of paths to look for @@ -102,7 +105,7 @@ m[p] = True for importer, name, ispkg in iter_modules(path, prefix): - yield importer, name, ispkg + yield ModuleInfo(importer, name, ispkg) if ispkg: try: @@ -125,7 +128,7 @@ def iter_modules(path=None, prefix=''): - """Yields (module_loader, name, ispkg) for all submodules on path, + """Yields a ModuleInfo for all submodules on path, or, if path is None, all top-level modules on sys.path. 'path' should be either None or a list of paths to look for @@ -134,7 +137,6 @@ 'prefix' is a string to output on the front of every module name on output. """ - if path is None: importers = iter_importers() else: @@ -145,7 +147,7 @@ for name, ispkg in iter_importer_modules(i, prefix): if name not in yielded: yielded[name] = 1 - yield i, name, ispkg + yield ModuleInfo(i, name, ispkg) #@simplegeneric diff -r 168efd87e051 Lib/test/test_pkgutil.py --- a/Lib/test/test_pkgutil.py Fri Feb 15 23:38:23 2013 +0200 +++ b/Lib/test/test_pkgutil.py Sat Feb 16 20:39:48 2013 +0530 @@ -80,8 +80,9 @@ self.assertEqual(res2, RESOURCE_DATA) names = [] - for loader, name, ispkg in pkgutil.iter_modules([zip_file]): - names.append(name) + for moduleinfo in pkgutil.iter_modules([zip_file]): + self.assertInstance(moduleinfo, pkgutil.ModuleInfo) + names.append(moduleinfo.name) self.assertEqual(names, ['test_getdata_zipfile']) del sys.path[0] diff -r 168efd87e051 Lib/test/test_runpy.py --- a/Lib/test/test_runpy.py Fri Feb 15 23:38:23 2013 +0200 +++ b/Lib/test/test_runpy.py Sat Feb 16 20:39:48 2013 +0530 @@ -439,7 +439,9 @@ self.addCleanup(self._del_pkg, pkg_dir, max_depth, mod_name) for depth in range(2, max_depth+1): self._add_relative_modules(pkg_dir, "", depth) - for finder, mod_name, ispkg in pkgutil.walk_packages([pkg_dir]): + for moduleinfo in pkgutil.walk_packages([pkg_dir]): + self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo) + find, mod_name, ispkg = moduleinfo self.assertIsInstance(finder, importlib.machinery.FileFinder) if ispkg: