add option to allow errors to be raised when emitting signals

This commit is contained in:
Izalia Mae 2024-04-21 12:20:25 -04:00
parent 68d813c842
commit 548a7902eb

View file

@ -120,12 +120,13 @@ class Signal(list[SignalCallback]):
print(f"WARNING: '{cbname}' was not connted to signal '{signame}'")
async def handle_emit(self, *args: Any, **kwargs: Any) -> None:
async def handle_emit(self, *args: Any, catch_errors: bool = True, **kwargs: Any) -> None:
"""
This gets called by :meth:`Signal.emit` as an :class:`asyncio.Task`.
:param args: Positional arguments to pass to all of the callbacks
:param kwargs: Keyword arguments to pass to all of the callbacks
:param catch_errors: Whether or not to handle exceptions raised from callbacks
"""
if not self.callback:
@ -133,10 +134,25 @@ class Signal(list[SignalCallback]):
return
for callback in self:
if await self.handle_callback(callback, *args, **kwargs):
try:
if await self.handle_callback(callback, *args, **kwargs):
break
except Exception:
if not catch_errors:
raise
traceback.print_exc()
break
await self.handle_callback(self.callback, *args, **kwargs)
try:
await self.handle_callback(self.callback, *args, **kwargs)
except Exception:
if not catch_errors:
raise
traceback.print_exc()
async def handle_callback(self,
@ -157,10 +173,6 @@ class Signal(list[SignalCallback]):
print(f"Callback '{callback.__name__}' timed out")
return True
except Exception:
traceback.print_exc()
return True
class Object: