diff --git a/redbot/core/bot.py b/redbot/core/bot.py index 89f711ef009..4c666802034 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -12,6 +12,7 @@ from collections import namedtuple, OrderedDict from datetime import datetime from importlib.machinery import ModuleSpec +from importlib.util import module_from_spec from pathlib import Path from typing import ( Optional, @@ -1680,16 +1681,22 @@ async def load_extension(self, spec: ModuleSpec): name = spec.name.split(".")[-1] if name in self.extensions: raise errors.PackageAlreadyLoaded(spec) - - lib = spec.loader.load_module() + lib = module_from_spec(spec) + sys.modules[spec.name] = lib + try: + spec.loader.exec_module(lib) + except Exception: + del sys.modules[spec.name] + raise if not hasattr(lib, "setup"): del lib - raise discord.ClientException(f"extension {name} does not have a setup function") + raise discord.ClientException(f"Extension {name} does not have a setup function.") try: await lib.setup(self) await self.tree.red_check_enabled() - except Exception as e: + except Exception: + del sys.modules[spec.name] await self._remove_module_references(lib.__name__) await self._call_module_finalizers(lib, name) raise