Skip to content

Commit 5315a95

Browse files
authored
Delete lib from sys.modules when an exception happens during load
1 parent a518c6c commit 5315a95

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

redbot/core/bot.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,15 +1630,20 @@ async def load_extension(self, spec: ModuleSpec):
16301630

16311631
lib = module_from_spec(spec)
16321632
sys.modules[name] = lib
1633-
spec.loader.exec_module(lib)
1633+
try:
1634+
spec.loader.exec_module(lib)
1635+
except Exception:
1636+
del sys.modules[name]
1637+
raise
16341638

16351639
if not hasattr(lib, "setup"):
16361640
del lib
16371641
raise discord.ClientException(f"extension {name} does not have a setup function")
16381642

16391643
try:
16401644
await lib.setup(self)
1641-
except Exception as e:
1645+
except Exception:
1646+
del sys.modules[name]
16421647
await self._remove_module_references(lib.__name__)
16431648
await self._call_module_finalizers(lib, name)
16441649
raise

0 commit comments

Comments
 (0)