Skip to content

Commit

Permalink
[utilities|collect] Capture ImportError exceptions
Browse files Browse the repository at this point in the history
Capture two ImportError exceptions when
calling __import__()

Signed-off-by: Jose Castillo <[email protected]>
  • Loading branch information
jcastill authored and TurboTurtle committed Oct 27, 2023
1 parent 1fb401d commit 509b460
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion sos/collector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ def _find_modules_in_path(cls, path, modulename):
def _import_modules(self, modname):
"""Import and return all found classes in a module"""
mod_short_name = modname.split('.')[2]
module = __import__(modname, globals(), locals(), [mod_short_name])
try:
module = __import__(modname, globals(), locals(), [mod_short_name])
except ImportError as e:
print(f'Error while trying to load module {modname}: '
f' {e.__class__.__name__}')
raise e
modules = inspect.getmembers(module, inspect.isclass)
for mod in modules:
if mod[0] in ('SosHost', 'Cluster'):
Expand Down
7 changes: 6 additions & 1 deletion sos/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,12 @@ def import_module(module_fqname, superclasses=None):
be subclasses of the specified superclass or superclasses. If superclasses
is plural it must be a tuple of classes."""
module_name = module_fqname.rpartition(".")[-1]
module = __import__(module_fqname, globals(), locals(), [module_name])
try:
module = __import__(module_fqname, globals(), locals(), [module_name])
except ImportError as e:
print(f'Error while trying to load module {module_fqname}: '
f' {e.__class__.__name__}')
raise e
modules = [class_ for cname, class_ in
inspect.getmembers(module, inspect.isclass)
if class_.__module__ == module_fqname]
Expand Down

0 comments on commit 509b460

Please sign in to comment.