Skip to content

Commit

Permalink
Delete lib from sys.modules when an exception happens during load
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuro-Rui authored Jan 6, 2023
1 parent a518c6c commit 5315a95
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions redbot/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,15 +1630,20 @@ 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
raise discord.ClientException(f"extension {name} does not have a setup function")

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
Expand Down

0 comments on commit 5315a95

Please sign in to comment.