Adapt the plugin safety metaclass for async funcs

This commit is contained in:
mathieui 2019-08-22 16:22:57 +02:00
parent 22ab7622f6
commit 5312382f8a
No known key found for this signature in database
GPG key ID: C59F84CEEFD616E3

View file

@ -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):