From d6f5216d29f3ff812d119203ad3802239043edb5 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 9 Jan 2022 23:15:33 +0800 Subject: [PATCH] :bug: fix plugin load fail --- nonebot/plugin/manager.py | 4 +++- nonebot/plugin/plugin.py | 7 ++++++- tests/test_init.py | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/nonebot/plugin/manager.py b/nonebot/plugin/manager.py index 23597eb1..eb5bef0b 100644 --- a/nonebot/plugin/manager.py +++ b/nonebot/plugin/manager.py @@ -10,8 +10,8 @@ from typing import Set, Dict, List, Union, Iterable, Optional, Sequence from nonebot.log import logger from nonebot.utils import escape_tag -from .plugin import Plugin, _new_plugin from . import _managers, _current_plugin +from .plugin import Plugin, _new_plugin, _confirm_plugin class PluginManager: @@ -180,6 +180,8 @@ class PluginLoader(SourceFileLoader): # ) from e super().exec_module(module) + _confirm_plugin(plugin) + _current_plugin.reset(_plugin_token) return diff --git a/nonebot/plugin/plugin.py b/nonebot/plugin/plugin.py index 88ebd304..83d37c79 100644 --- a/nonebot/plugin/plugin.py +++ b/nonebot/plugin/plugin.py @@ -96,5 +96,10 @@ def _new_plugin(fullname: str, module: ModuleType, manager: "PluginManager") -> if name in plugins: raise RuntimeError("Plugin already exists! Check your plugin name.") plugin = Plugin(name, module, fullname, manager) - plugins[name] = 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 diff --git a/tests/test_init.py b/tests/test_init.py index 46911755..03da1755 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -94,3 +94,9 @@ async def test_load_plugin(load_plugin: Set["Plugin"]): assert False except RuntimeError: assert True + + try: + nonebot.load_plugin("some_plugin_no_exist") + assert False + except Exception: + assert nonebot.get_plugin("some_plugin_no_exist") is None