From 106905fd003f2aa970bb86a91fb178501fef3ea1 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 27 Mar 2022 20:42:34 +0300 Subject: [PATCH] Update path cache when replacing plugins. Fixes #156 --- maubot/loader/abc.py | 4 ---- maubot/loader/zip.py | 13 +++++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/maubot/loader/abc.py b/maubot/loader/abc.py index c669398..c2c71b2 100644 --- a/maubot/loader/abc.py +++ b/maubot/loader/abc.py @@ -93,10 +93,6 @@ class PluginLoader(BasePluginLoader, ABC): async def reload(self) -> type[PluginClass]: pass - @abstractmethod - async def unload(self) -> None: - pass - @abstractmethod async def delete(self) -> None: pass diff --git a/maubot/loader/zip.py b/maubot/loader/zip.py index f2e27f0..323b58a 100644 --- a/maubot/loader/zip.py +++ b/maubot/loader/zip.py @@ -233,12 +233,17 @@ class ZippedPluginLoader(PluginLoader): return plugin async def reload(self, new_path: str | None = None) -> type[PluginClass]: - await self.unload() - if new_path is not None: + self._unload() + if new_path is not None and new_path != self.path: + try: + del self.path_cache[self.path] + except KeyError: + pass self.path = new_path + self.path_cache[self.path] = self return await self.load(reset_cache=True) - async def unload(self) -> None: + def _unload(self) -> None: for name, mod in list(sys.modules.items()): if (getattr(mod, "__file__", "") or "").startswith(self.path): del sys.modules[name] @@ -246,7 +251,7 @@ class ZippedPluginLoader(PluginLoader): self.log.debug(f"Unloaded plugin {self.meta.id} at {self.path}") async def delete(self) -> None: - await self.unload() + self._unload() try: del self.path_cache[self.path] except KeyError: