Kaydet (Commit) eb26ea3f authored tarafından Phillip J. Eby's avatar Phillip J. Eby

Allow the 'onerror' argument to walk_packages() to catch any Exception, not

just ImportError.  This allows documentation tools to better skip unimportable
packages.
üst ab260049
...@@ -83,13 +83,18 @@ def walk_packages(path=None, prefix='', onerror=None): ...@@ -83,13 +83,18 @@ def walk_packages(path=None, prefix='', onerror=None):
attribute to find submodules. attribute to find submodules.
'onerror' is a function which gets called with one argument (the 'onerror' is a function which gets called with one argument (the
name of the package which was being imported) if an ImportError name of the package which was being imported) if any exception
occurs trying to import a package. By default the ImportError is occurs while trying to import a package. If no onerror function is
caught and ignored. supplied, ImportErrors are caught and ignored, while all other
exceptions are propagated, terminating the search.
Examples: Examples:
walk_packages() : list all modules python can access
walk_packages(ctypes.__path__, ctypes.__name__+'.') : list all submodules of ctypes # list all modules python can access
walk_packages()
# list all submodules of ctypes
walk_packages(ctypes.__path__, ctypes.__name__+'.')
""" """
def seen(p, m={}): def seen(p, m={}):
...@@ -106,6 +111,11 @@ def walk_packages(path=None, prefix='', onerror=None): ...@@ -106,6 +111,11 @@ def walk_packages(path=None, prefix='', onerror=None):
except ImportError: except ImportError:
if onerror is not None: if onerror is not None:
onerror(name) onerror(name)
except Exception:
if onerror is not None:
onerror(name)
else:
raise
else: else:
path = getattr(sys.modules[name], '__path__', None) or [] path = getattr(sys.modules[name], '__path__', None) or []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment