@@ -115,7 +115,6 @@ class PluginManager:
115
115
def __init__ (self , project_name : str ) -> None :
116
116
self .project_name : "Final" = project_name
117
117
self ._name2plugin : "Final[Dict[str, _Plugin]]" = {}
118
- self ._plugin2hookcallers : "Final[Dict[_Plugin, List[_HookCaller]]]" = {}
119
118
self ._plugin_distinfo : "Final[List[Tuple[_Plugin, DistFacade]]]" = []
120
119
self .trace : "Final" = _tracing .TagTracer ().get ("pluginmanage" )
121
120
self .hook : "Final" = _HookRelay ()
@@ -138,11 +137,17 @@ def register(self, plugin: _Plugin, name: Optional[str] = None) -> Optional[str]
138
137
is already registered."""
139
138
plugin_name = name or self .get_canonical_name (plugin )
140
139
141
- if plugin_name in self ._name2plugin or plugin in self . _plugin2hookcallers :
140
+ if plugin_name in self ._name2plugin :
142
141
if self ._name2plugin .get (plugin_name , - 1 ) is None :
143
142
return None # blocked plugin, return None to indicate no registration
144
143
raise ValueError (
145
- "Plugin already registered: %s=%s\n %s"
144
+ "Plugin name already registered: %s=%s\n %s"
145
+ % (plugin_name , plugin , self ._name2plugin )
146
+ )
147
+
148
+ if plugin in self ._name2plugin .values ():
149
+ raise ValueError (
150
+ "Plugin already registered under a different name: %s=%s\n %s"
146
151
% (plugin_name , plugin , self ._name2plugin )
147
152
)
148
153
@@ -151,8 +156,6 @@ def register(self, plugin: _Plugin, name: Optional[str] = None) -> Optional[str]
151
156
self ._name2plugin [plugin_name ] = plugin
152
157
153
158
# register matching hook implementations of the plugin
154
- hookcallers : List [_HookCaller ] = []
155
- self ._plugin2hookcallers [plugin ] = hookcallers
156
159
for name in dir (plugin ):
157
160
hookimpl_opts = self .parse_hookimpl_opts (plugin , name )
158
161
if hookimpl_opts is not None :
@@ -168,7 +171,6 @@ def register(self, plugin: _Plugin, name: Optional[str] = None) -> Optional[str]
168
171
self ._verify_hook (hook , hookimpl )
169
172
hook ._maybe_apply_history (hookimpl )
170
173
hook ._add_hookimpl (hookimpl )
171
- hookcallers .append (hook )
172
174
return plugin_name
173
175
174
176
def parse_hookimpl_opts (
@@ -201,14 +203,16 @@ def unregister(
201
203
if plugin is None :
202
204
plugin = self .get_plugin (name )
203
205
206
+ hookcallers = self .get_hookcallers (plugin )
207
+ if hookcallers :
208
+ for hookcaller in hookcallers :
209
+ hookcaller ._remove_plugin (plugin )
210
+
204
211
# if self._name2plugin[name] == None registration was blocked: ignore
205
212
if self ._name2plugin .get (name ):
206
213
assert name is not None
207
214
del self ._name2plugin [name ]
208
215
209
- for hookcaller in self ._plugin2hookcallers .pop (plugin , []):
210
- hookcaller ._remove_plugin (plugin )
211
-
212
216
return plugin
213
217
214
218
def set_blocked (self , name : str ) -> None :
@@ -254,11 +258,11 @@ def parse_hookspec_opts(
254
258
255
259
def get_plugins (self ) -> Set [Any ]:
256
260
"""return the set of registered plugins."""
257
- return set (self ._plugin2hookcallers )
261
+ return set (self ._name2plugin . values () )
258
262
259
263
def is_registered (self , plugin : _Plugin ) -> bool :
260
264
"""Return ``True`` if the plugin is already registered."""
261
- return plugin in self ._plugin2hookcallers
265
+ return any ( plugin == val for val in self ._name2plugin . values ())
262
266
263
267
def get_canonical_name (self , plugin : _Plugin ) -> str :
264
268
"""Return canonical name for a plugin object. Note that a plugin
@@ -373,7 +377,14 @@ def list_name_plugin(self) -> List[Tuple[str, _Plugin]]:
373
377
374
378
def get_hookcallers (self , plugin : _Plugin ) -> Optional [List [_HookCaller ]]:
375
379
"""get all hook callers for the specified plugin."""
376
- return self ._plugin2hookcallers .get (plugin )
380
+ if self .get_name (plugin ) is None :
381
+ return None
382
+ hookcallers = []
383
+ for hookcaller in self .hook .__dict__ .values ():
384
+ for hookimpl in hookcaller .get_hookimpls ():
385
+ if hookimpl .plugin is plugin :
386
+ hookcallers .append (hookcaller )
387
+ return hookcallers
377
388
378
389
def add_hookcall_monitoring (
379
390
self , before : _BeforeTrace , after : _AfterTrace
0 commit comments