mirror of
				https://github.com/ARM-software/devlib.git
				synced 2025-11-03 23:41:21 +00: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:
		@@ -36,7 +36,7 @@ from functools import partial
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import wrapt
 | 
					import wrapt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from devlib.exception import TimeoutError
 | 
					from devlib.exception import HostError, TimeoutError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# ABI --> architectures list
 | 
					# ABI --> architectures list
 | 
				
			||||||
@@ -172,15 +172,31 @@ def walk_modules(path):
 | 
				
			|||||||
    Given package name, return a list of all modules (including submodules, etc)
 | 
					    Given package name, return a list of all modules (including submodules, etc)
 | 
				
			||||||
    in that package.
 | 
					    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]
 | 
					    mods = [root_mod]
 | 
				
			||||||
    for _, name, ispkg in pkgutil.iter_modules(root_mod.__path__):
 | 
					    for _, name, ispkg in pkgutil.iter_modules(root_mod.__path__):
 | 
				
			||||||
        submod_path = '.'.join([path, name])
 | 
					        submod_path = '.'.join([path, name])
 | 
				
			||||||
        if ispkg:
 | 
					        if ispkg:
 | 
				
			||||||
            mods.extend(walk_modules(submod_path))
 | 
					            mods.extend(walk_modules(submod_path))
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            submod = __import__(submod_path, {}, {}, [''])
 | 
					            submod = __try_import(submod_path)
 | 
				
			||||||
            mods.append(submod)
 | 
					            mods.append(submod)
 | 
				
			||||||
    return mods
 | 
					    return mods
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user