9
9
from .model import Plugin , PluginMetadata , _current_plugin , _plugins
10
10
11
11
12
- class PluginFinder (MetaPathFinder ):
13
- def find_spec (
14
- self ,
15
- fullname : str ,
16
- path : Optional [Sequence [str ]],
17
- target : Optional [ModuleType ] = None ,
18
- ):
19
- module_spec = PathFinder .find_spec (fullname , path , target )
20
- if not module_spec :
21
- return
22
- module_origin = module_spec .origin
23
- if not module_origin :
24
- return
25
-
26
- module_spec .loader = PluginLoader (fullname , module_origin )
27
- return module_spec
28
-
29
-
30
12
class PluginLoader (SourceFileLoader ):
31
13
def __init__ (self , fullname : str , path : str ) -> None :
32
14
self .loaded = False
@@ -51,9 +33,9 @@ def exec_module(self, module: ModuleType) -> None:
51
33
52
34
try :
53
35
super ().exec_module (module )
54
- # except Exception:
55
- # # _revert_plugin( plugin)
56
- # raise
36
+ except Exception :
37
+ plugin . dispose ( )
38
+ raise
57
39
finally :
58
40
# leave plugin context
59
41
_current_plugin .reset (_plugin_token )
@@ -64,9 +46,6 @@ def exec_module(self, module: ModuleType) -> None:
64
46
return
65
47
66
48
67
- _finder = PluginFinder ()
68
-
69
-
70
49
def find_spec (name , package = None ):
71
50
fullname = resolve_name (name , package ) if name .startswith ("." ) else name
72
51
parent_name = fullname .rpartition ("." )[0 ]
@@ -81,7 +60,14 @@ def find_spec(name, package=None):
81
60
) from e
82
61
else :
83
62
parent_path = None
84
- return _finder .find_spec (fullname , parent_path )
63
+ module_spec = PathFinder .find_spec (fullname , parent_path , None )
64
+ if not module_spec :
65
+ return
66
+ module_origin = module_spec .origin
67
+ if not module_origin :
68
+ return
69
+ module_spec .loader = PluginLoader (fullname , module_origin )
70
+ return module_spec
85
71
86
72
87
73
def import_plugin (name , package = None ):
@@ -94,4 +80,23 @@ def import_plugin(name, package=None):
94
80
return
95
81
96
82
97
- sys .meta_path .insert (0 , _finder )
83
+ class _PluginFinder (MetaPathFinder ):
84
+ def find_spec (
85
+ self ,
86
+ fullname : str ,
87
+ path : Optional [Sequence [str ]],
88
+ target : Optional [ModuleType ] = None ,
89
+ ):
90
+ module_spec = PathFinder .find_spec (fullname , path , target )
91
+ if not module_spec :
92
+ return
93
+ module_origin = module_spec .origin
94
+ if not module_origin :
95
+ return
96
+ if module_spec .name in _plugins :
97
+ module_spec .loader = PluginLoader (fullname , module_origin )
98
+ return module_spec
99
+ return
100
+
101
+
102
+ sys .meta_path .insert (0 , _PluginFinder ())
0 commit comments