From 5312382f8abf228a9dcd83aec260ae2d939fc200 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 22 Aug 2019 16:22:57 +0200 Subject: Adapt the plugin safety metaclass for async funcs --- poezio/plugin.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'poezio/plugin.py') diff --git a/poezio/plugin.py b/poezio/plugin.py index 0275e2f9..1995d7cd 100644 --- a/poezio/plugin.py +++ b/poezio/plugin.py @@ -3,6 +3,7 @@ Define the PluginConfig and Plugin classes, plus the SafetyMetaclass. These are used in the plugin system added in poezio 0.7.5 (see plugin_manager.py) """ +from asyncio import iscoroutinefunction from functools import partial from configparser import RawConfigParser from poezio.timed_events import TimedEvent, DelayedEvent @@ -84,9 +85,22 @@ class SafetyMetaclass(type): SafetyMetaclass.core.information(traceback.format_exc(), 'Error') return None - + async def async_helper(*args, **kwargs): + try: + return await f(*args, **kwargs) + except: + if inspect.stack()[1][1] == inspect.getfile(f): + raise + elif SafetyMetaclass.core: + log.error('Error in a plugin', exc_info=True) + SafetyMetaclass.core.information(traceback.format_exc(), + 'Error') + return None + if iscoroutinefunction(f): + return async_helper return helper + def __new__(meta, name, bases, class_dict): for k, v in class_dict.items(): if inspect.isfunction(v): -- cgit v1.2.3