module = None
try:
spec = util.spec_from_file_location(_MS_EXTENSION_NAME, library_path)
if spec is None or spec.loader is None:
raise ImportError(f"Cannot create an extension loader for {library_path}")
module = util.module_from_spec(spec)
sys.modules[_MS_EXTENSION_NAME] = module
spec.loader.exec_module(module)
return module
except (ImportError, OSError, RuntimeError) as error:
if module is not None and sys.modules.get(_MS_EXTENSION_NAME) is module:
sys.modules.pop(_MS_EXTENSION_NAME, None)
load_errors.append(f"{library_path}: {error}")