From b8370cb08332d70e6b1612c4b97b5859d1949fe1 Mon Sep 17 00:00:00 2001 From: Jose Castillo Date: Wed, 18 Oct 2023 10:07:54 +0100 Subject: [PATCH] [utilities|collect] Capture ImportError exceptions Capture two ImportError exceptions when calling __import__() Signed-off-by: Jose Castillo --- sos/collector/__init__.py | 7 ++++++- sos/utilities.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sos/collector/__init__.py b/sos/collector/__init__.py index 6c6280401f..ae316755eb 100644 --- a/sos/collector/__init__.py +++ b/sos/collector/__init__.py @@ -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'): diff --git a/sos/utilities.py b/sos/utilities.py index d56afe28cf..ce371b0a03 100644 --- a/sos/utilities.py +++ b/sos/utilities.py @@ -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]