🐛 fix plugin load fail

This commit is contained in:
yanyongyu 2022-01-09 23:15:33 +08:00
parent d549087db2
commit d6f5216d29
3 changed files with 15 additions and 2 deletions

View File

@ -10,8 +10,8 @@ from typing import Set, Dict, List, Union, Iterable, Optional, Sequence
from nonebot.log import logger from nonebot.log import logger
from nonebot.utils import escape_tag from nonebot.utils import escape_tag
from .plugin import Plugin, _new_plugin
from . import _managers, _current_plugin from . import _managers, _current_plugin
from .plugin import Plugin, _new_plugin, _confirm_plugin
class PluginManager: class PluginManager:
@ -180,6 +180,8 @@ class PluginLoader(SourceFileLoader):
# ) from e # ) from e
super().exec_module(module) super().exec_module(module)
_confirm_plugin(plugin)
_current_plugin.reset(_plugin_token) _current_plugin.reset(_plugin_token)
return return

View File

@ -96,5 +96,10 @@ def _new_plugin(fullname: str, module: ModuleType, manager: "PluginManager") ->
if name in plugins: if name in plugins:
raise RuntimeError("Plugin already exists! Check your plugin name.") raise RuntimeError("Plugin already exists! Check your plugin name.")
plugin = Plugin(name, module, fullname, manager) plugin = Plugin(name, module, fullname, manager)
plugins[name] = plugin
return plugin return plugin
def _confirm_plugin(plugin: Plugin) -> None:
if plugin.name in plugins:
raise RuntimeError("Plugin already exists! Check your plugin name.")
plugins[plugin.name] = plugin

View File

@ -94,3 +94,9 @@ async def test_load_plugin(load_plugin: Set["Plugin"]):
assert False assert False
except RuntimeError: except RuntimeError:
assert True assert True
try:
nonebot.load_plugin("some_plugin_no_exist")
assert False
except Exception:
assert nonebot.get_plugin("some_plugin_no_exist") is None