Catch exceptions for direct events.

Events triggered with direct=True will have exceptions caught.

Note that all event handlers in a direct event will currently run
in the same thread.
This commit is contained in:
Lance Stout 2010-11-03 12:38:13 -04:00
parent ffc6f031d9
commit 0214db7545

View file

@ -535,13 +535,22 @@ class XMLStream(object):
name -- The name of the event to trigger. name -- The name of the event to trigger.
data -- Data that will be passed to each event handler. data -- Data that will be passed to each event handler.
Defaults to an empty dictionary. Defaults to an empty dictionary.
direct -- Runs the event directly if True. direct -- Runs the event directly if True, skipping the
event queue. All event handlers will run in the
same thread.
""" """
for handler in self.__event_handlers.get(name, []): for handler in self.__event_handlers.get(name, []):
if direct: if direct:
handler[0](copy.copy(data)) try:
handler[0](copy.copy(data))
except Exception as e:
error_msg = 'Error processing event handler: %s'
logging.exception(error_msg % str(handler[0]))
if hasattr(data, 'exception'):
data.exception(e)
else: else:
self.event_queue.put(('event', handler, copy.copy(data))) self.event_queue.put(('event', handler, copy.copy(data)))
if handler[2]: if handler[2]:
# If the handler is disposable, we will go ahead and # If the handler is disposable, we will go ahead and
# remove it now instead of waiting for it to be # remove it now instead of waiting for it to be
@ -807,11 +816,6 @@ class XMLStream(object):
""" """
try: try:
func(*args) func(*args)
except Exception as e:
error_msg = 'Error processing event handler: %s'
logging.exception(error_msg % str(func))
if hasattr(args[0], 'exception'):
args[0].exception(e)
def _event_runner(self): def _event_runner(self):
""" """