utils: add direct fallback to load_system_module

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-06-07 19:45:39 -04:00
parent d6231634db
commit 1a5cf56532
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 8 additions and 2 deletions

View File

@ -200,6 +200,12 @@ def verify_source(
return checksum, checksum == orig_chksum
def load_system_module(name: str) -> ModuleType:
if not SYS_MOD_PATHS:
# no dist path detected, fall back to direct import attempt
try:
return importlib.import_module(name)
except ImportError as e:
raise ServerError(f"Unable to import module {name}") from e
for module_path in SYS_MOD_PATHS:
sys.path.insert(0, module_path)
try:
@ -207,10 +213,10 @@ def load_system_module(name: str) -> ModuleType:
except ImportError as e:
if not isinstance(e, ModuleNotFoundError):
logging.exception(f"Failed to load {name} module")
sys.path.pop(0)
else:
sys.path.pop(0)
break
finally:
sys.path.pop(0)
else:
raise ServerError(f"Unable to import module {name}")
return module