Skip to content

Commit 9f185e8

Browse files
committed
🐛 remove module cached file (.pyc)
1 parent ba7eed3 commit 9f185e8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

arclet/entari/plugin/model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from collections.abc import Awaitable
44
from contextvars import ContextVar
55
from dataclasses import dataclass, field
6+
from pathlib import Path
7+
import sys
68
from types import ModuleType
79
from typing import TYPE_CHECKING, Any, Callable, TypeVar
810
from weakref import finalize
@@ -152,6 +154,13 @@ def dispose(self):
152154
if self._is_disposed:
153155
return
154156
self._is_disposed = True
157+
if self.module.__spec__ and self.module.__spec__.cached:
158+
Path(self.module.__spec__.cached).unlink(missing_ok=True)
159+
sys.modules.pop(self.module.__name__, None)
160+
for submod in self.submodules.values():
161+
sys.modules.pop(submod.__name__, None)
162+
if submod.__spec__ and submod.__spec__.cached:
163+
Path(submod.__spec__.cached).unlink(missing_ok=True)
155164
self.submodules.clear()
156165
for disp in self.dispatchers.values():
157166
disp.dispose()

arclet/entari/plugin/module.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def package(*names: str):
1818

1919

2020
class PluginLoader(SourceFileLoader):
21-
def __init__(self, fullname: str, path: str) -> None:
21+
def __init__(self, fullname: str, path: str, parent_plugin_id: Optional[str] = None) -> None:
2222
self.loaded = False
23+
self.parent_plugin_id = parent_plugin_id
2324
super().__init__(fullname, path)
2425

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

3132
def exec_module(self, module: ModuleType) -> None:
32-
if plugin := _current_plugin.get(None):
33+
if plugin := _current_plugin.get(
34+
service.plugins.get(self.parent_plugin_id) if self.parent_plugin_id else None
35+
):
3336
if module.__name__ == plugin.module.__name__: # from . import xxxx
3437
return
3538
setattr(module, "__plugin__", plugin)
@@ -129,6 +132,10 @@ def find_spec(
129132
if module_spec.name in service.plugins:
130133
module_spec.loader = PluginLoader(fullname, module_origin)
131134
return module_spec
135+
for plug in service.plugins.values():
136+
if module_spec.name in plug.submodules:
137+
module_spec.loader = PluginLoader(fullname, module_origin, plug.id)
138+
return module_spec
132139
return
133140

134141

0 commit comments

Comments
 (0)