summaryrefslogtreecommitdiffstats
path: root/Lib/pkgutil.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2006-07-25 10:22:34 (GMT)
committerGeorg Brandl <georg@python.org>2006-07-25 10:22:34 (GMT)
commit69b9b677b091fe770d73a22e0d4a11a15364bc4a (patch)
tree1e6f889a3ca2637f72e1bfcf86b71e13974bdc8c /Lib/pkgutil.py
parent2b2d2974e760d06048666794711a12e7dbe28720 (diff)
downloadcpython-69b9b677b091fe770d73a22e0d4a11a15364bc4a.zip
cpython-69b9b677b091fe770d73a22e0d4a11a15364bc4a.tar.gz
cpython-69b9b677b091fe770d73a22e0d4a11a15364bc4a.tar.bz2
Patch #1525766: correctly pass onerror arg to recursive calls
of pkg.walk_packages. Also improve the docstrings.
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r--Lib/pkgutil.py38
1 files changed, 34 insertions, 4 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index 1683fae..3b004e9 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -69,7 +69,28 @@ def simplegeneric(func):
def walk_packages(path=None, prefix='', onerror=None):
- """Yield submodule names+loaders recursively, for path or sys.path"""
+ """Yields (module_loader, name, ispkg) 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.
+
+ 'prefix' is a string to output on the front of every module name
+ on output.
+
+ Note that this function must import all *packages* (NOT all
+ modules!) on the given path, in order to access the __path__
+ attribute to find submodules.
+
+ 'onerror' is a function which gets called with one argument (the
+ name of the package which was being imported) if an ImportError
+ occurs trying to import a package. By default the ImportError is
+ caught and ignored.
+
+ Examples:
+ walk_packages() : list all modules python can access
+ walk_packages(ctypes.__path__, ctypes.__name__+'.') : list all submodules of ctypes
+ """
def seen(p, m={}):
if p in m:
@@ -84,19 +105,28 @@ def walk_packages(path=None, prefix='', onerror=None):
__import__(name)
except ImportError:
if onerror is not None:
- onerror()
+ onerror(name)
else:
path = getattr(sys.modules[name], '__path__', None) or []
# don't traverse path items we've seen before
path = [p for p in path if not seen(p)]
- for item in walk_packages(path, name+'.'):
+ for item in walk_packages(path, name+'.', onerror):
yield item
def iter_modules(path=None, prefix=''):
- """Yield submodule names+loaders for path or sys.path"""
+ """Yields (module_loader, name, ispkg) 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.
+
+ 'prefix' is a string to output on the front of every module name
+ on output.
+ """
+
if path is None:
importers = iter_importers()
else: