Skip to content

Commit

Permalink
🐛 remove module cached file (.pyc)
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Sep 28, 2024
1 parent ba7eed3 commit 9f185e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 9 additions & 0 deletions arclet/entari/plugin/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from collections.abc import Awaitable
from contextvars import ContextVar
from dataclasses import dataclass, field
from pathlib import Path
import sys
from types import ModuleType
from typing import TYPE_CHECKING, Any, Callable, TypeVar
from weakref import finalize
Expand Down Expand Up @@ -152,6 +154,13 @@ def dispose(self):
if self._is_disposed:
return
self._is_disposed = True
if self.module.__spec__ and self.module.__spec__.cached:
Path(self.module.__spec__.cached).unlink(missing_ok=True)
sys.modules.pop(self.module.__name__, None)
for submod in self.submodules.values():
sys.modules.pop(submod.__name__, None)
if submod.__spec__ and submod.__spec__.cached:
Path(submod.__spec__.cached).unlink(missing_ok=True)
self.submodules.clear()
for disp in self.dispatchers.values():
disp.dispose()
Expand Down
11 changes: 9 additions & 2 deletions arclet/entari/plugin/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def package(*names: str):


class PluginLoader(SourceFileLoader):
def __init__(self, fullname: str, path: str) -> None:
def __init__(self, fullname: str, path: str, parent_plugin_id: Optional[str] = None) -> None:
self.loaded = False
self.parent_plugin_id = parent_plugin_id
super().__init__(fullname, path)

def create_module(self, spec) -> Optional[ModuleType]:
Expand All @@ -29,7 +30,9 @@ def create_module(self, spec) -> Optional[ModuleType]:
return super().create_module(spec)

def exec_module(self, module: ModuleType) -> None:
if plugin := _current_plugin.get(None):
if plugin := _current_plugin.get(
service.plugins.get(self.parent_plugin_id) if self.parent_plugin_id else None
):
if module.__name__ == plugin.module.__name__: # from . import xxxx
return
setattr(module, "__plugin__", plugin)
Expand Down Expand Up @@ -129,6 +132,10 @@ def find_spec(
if module_spec.name in service.plugins:
module_spec.loader = PluginLoader(fullname, module_origin)
return module_spec
for plug in service.plugins.values():
if module_spec.name in plug.submodules:
module_spec.loader = PluginLoader(fullname, module_origin, plug.id)
return module_spec
return


Expand Down

0 comments on commit 9f185e8

Please sign in to comment.