1
0
mirror of https://github.com/ARM-software/devlib.git synced 2024-10-06 10:50:51 +01:00

utils/misc: walk_modules always raises HostError

Previously, if an error had occured while attempting to load a module
during walk_modules, it was propagated to the loading code. This
meant that walk_modules could, in theory, raise any exception at all,
and that it was not always possible to tell exactly where the exception
originated.

Now, all encounted errors are re-raised as HostError, which preserves
the original exception in an attribute and sets "modpath" attribute to
point to the module that originated it.
This commit is contained in:
Sergei Trofimov 2017-02-08 11:21:06 +00:00
parent a9265031ba
commit 28891a822b

View File

@ -36,7 +36,7 @@ from functools import partial
import wrapt
from devlib.exception import TimeoutError
from devlib.exception import HostError, TimeoutError
# ABI --> architectures list
@ -172,15 +172,31 @@ def walk_modules(path):
Given package name, return a list of all modules (including submodules, etc)
in that package.
:raises HostError: if an exception is raised while trying to import one of the
modules under ``path``. The exception will have addtional
attributes set: ``module`` will be set to the qualified name
of the originating module, and ``orig_exc`` will contain
the original exception.
"""
root_mod = __import__(path, {}, {}, [''])
def __try_import(path):
try:
return __import__(path, {}, {}, [''])
except Exception as e:
he = HostError('Could not load {}: {}'.format(path, str(e)))
he.module = path
he.orig_exc = e
raise he
root_mod = __try_import(path)
mods = [root_mod]
for _, name, ispkg in pkgutil.iter_modules(root_mod.__path__):
submod_path = '.'.join([path, name])
if ispkg:
mods.extend(walk_modules(submod_path))
else:
submod = __import__(submod_path, {}, {}, [''])
submod = __try_import(submod_path)
mods.append(submod)
return mods