mirror of
https://github.com/python/cpython.git
synced 2025-07-30 14:44:10 +00:00
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
just ImportError. This allows documentation tools to better skip unimportable packages.
This commit is contained in:
parent
ab26004923
commit
eb26ea3f83
1 changed files with 15 additions and 5 deletions
|
@ -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):
|
||||||
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 []
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue