Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[utilities|collect] Capture ImportError exceptions #3387

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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