From 5315a95c1d3a755147159abfd62847aefe6b83bb Mon Sep 17 00:00:00 2001 From: Louis Dominic <83048873+Kuro-Rui@users.noreply.github.com> Date: Fri, 6 Jan 2023 22:36:20 +0700 Subject: [PATCH] Delete lib from sys.modules when an exception happens during load --- redbot/core/bot.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/redbot/core/bot.py b/redbot/core/bot.py index f94324783b5c..a3a7205cfd0d 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -1630,7 +1630,11 @@ async def load_extension(self, spec: ModuleSpec): lib = module_from_spec(spec) sys.modules[name] = lib - spec.loader.exec_module(lib) + try: + spec.loader.exec_module(lib) + except Exception: + del sys.modules[name] + raise if not hasattr(lib, "setup"): del lib @@ -1638,7 +1642,8 @@ async def load_extension(self, spec: ModuleSpec): try: await lib.setup(self) - except Exception as e: + except Exception: + del sys.modules[name] await self._remove_module_references(lib.__name__) await self._call_module_finalizers(lib, name) raise