Update path cache when replacing plugins. Fixes #156

This commit is contained in:
Tulir Asokan 2022-03-27 20:42:34 +03:00
parent 7532f4523a
commit 106905fd00
2 changed files with 9 additions and 8 deletions

View File

@ -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

View File

@ -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: