Add call_unsafe functions to EventManager.EventHook, to not swallow errors on
specific function calls (e.g. preprocess.command)
This commit is contained in:
parent
6af738107b
commit
7eb31820d7
3 changed files with 27 additions and 12 deletions
|
@ -96,8 +96,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
stdout, stderr = StdOut(module_name, target), StdErr(module_name,
|
stdout, stderr = StdOut(module_name, target), StdErr(module_name,
|
||||||
target)
|
target)
|
||||||
|
|
||||||
returns = self.events.on("preprocess.command"
|
returns = self.events.on("preprocess.command").unsafe_call(
|
||||||
).call(hook=hook, user=event["user"], server=event["server"],
|
hook=hook, user=event["user"], server=event["server"],
|
||||||
target=target, is_channel=is_channel, tags=event["tags"])
|
target=target, is_channel=is_channel, tags=event["tags"])
|
||||||
for returned in returns:
|
for returned in returns:
|
||||||
if returned:
|
if returned:
|
||||||
|
|
|
@ -85,6 +85,13 @@ class EventHookContext(object):
|
||||||
def call_limited(self, maximum, **kwargs):
|
def call_limited(self, maximum, **kwargs):
|
||||||
return self._parent.call_limited(maximum, **kwargs)
|
return self._parent.call_limited(maximum, **kwargs)
|
||||||
|
|
||||||
|
def call_unsafe_for_result(self, default=None, **kwargs):
|
||||||
|
return self._parent.call_unsafe_for_result(default, **kwargs)
|
||||||
|
def call_unsafe(self, **kwargs):
|
||||||
|
return self._parent.call_unsafe(**kwargs)
|
||||||
|
def call_unsafe_limited(self, maximum, **kwargs):
|
||||||
|
return self._parent.call_unsafe_limited(maximum, **kwargs)
|
||||||
|
|
||||||
def get_hooks(self):
|
def get_hooks(self):
|
||||||
return self._parent.get_hooks()
|
return self._parent.get_hooks()
|
||||||
def get_children(self):
|
def get_children(self):
|
||||||
|
@ -187,18 +194,25 @@ class EventHook(object):
|
||||||
return child
|
return child
|
||||||
|
|
||||||
def call_for_result(self, default=None, **kwargs):
|
def call_for_result(self, default=None, **kwargs):
|
||||||
results = self.call_limited(1, **kwargs)
|
return (self.call_limited(1, **kwargs) or [default])[0]
|
||||||
return default if not len(results) else results[0]
|
|
||||||
def assure_call(self, **kwargs):
|
def assure_call(self, **kwargs):
|
||||||
if not self._stored_events == None:
|
if not self._stored_events == None:
|
||||||
self._stored_events.append(kwargs)
|
self._stored_events.append(kwargs)
|
||||||
else:
|
else:
|
||||||
self._call(kwargs)
|
self._call(kwargs, True, None)
|
||||||
def call(self, **kwargs):
|
def call(self, **kwargs):
|
||||||
return self._call(kwargs)
|
return self._call(kwargs, True, None)
|
||||||
def call_limited(self, maximum, **kwargs):
|
def call_limited(self, maximum, **kwargs):
|
||||||
return self._call(kwargs, maximum=maximum)
|
return self._call(kwargs, True, None)
|
||||||
def _call(self, kwargs, maximum=None):
|
|
||||||
|
def call_unsafe_for_result(self, default=None, **kwargs):
|
||||||
|
return (self.call_unsafe_limited(1, **kwargs) or [default])[0]
|
||||||
|
def call_unsafe(self, **kwargs):
|
||||||
|
return self._call(kwargs, False)
|
||||||
|
def call_unsafe_limited(self, maximum, **kwargs):
|
||||||
|
return self._call(kwargs, False, maximum)
|
||||||
|
|
||||||
|
def _call(self, kwargs, safe, maximum):
|
||||||
event_path = self._get_path()
|
event_path = self._get_path()
|
||||||
self.log.trace("calling event: \"%s\" (params: %s)",
|
self.log.trace("calling event: \"%s\" (params: %s)",
|
||||||
[event_path, kwargs])
|
[event_path, kwargs])
|
||||||
|
@ -212,9 +226,10 @@ class EventHook(object):
|
||||||
try:
|
try:
|
||||||
returns.append(hook.call(event))
|
returns.append(hook.call(event))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
traceback.print_exc()
|
self.log.error("failed to call event \"%s\"",
|
||||||
self.log.error("failed to call event \"%s\"", [
|
[self._get_path()], exc_info=True)
|
||||||
event_path], exc_info=True)
|
if not safe:
|
||||||
|
raise
|
||||||
|
|
||||||
total_milliseconds = (time.monotonic() - start) * 1000
|
total_milliseconds = (time.monotonic() - start) * 1000
|
||||||
self.log.trace("event \"%s\" called in %fms", [
|
self.log.trace("event \"%s\" called in %fms", [
|
||||||
|
|
|
@ -252,7 +252,7 @@ class Server(IRCObject.Object):
|
||||||
return self.until_read_timeout == 0
|
return self.until_read_timeout == 0
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
returned = self.events.on("preprocess.send").call_for_result(
|
returned = self.events.on("preprocess.send").call_unsafe_for_result(
|
||||||
server=self, line=data)
|
server=self, line=data)
|
||||||
line = returned or data
|
line = returned or data
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue