diff options
author | mathieui <mathieui@mathieui.net> | 2015-02-22 14:17:17 +0100 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2015-02-22 14:17:17 +0100 |
commit | 2b3b86e281da03dfe4b2444be5b62811b4dcfe8d (patch) | |
tree | 10638578db29b92a20cb0760e50d4c2c73120a92 /slixmpp/xmlstream/xmlstream.py | |
parent | 92e4bc752a7da814b5b7c19b0f5d2ee95f715f7e (diff) | |
download | slixmpp-2b3b86e281da03dfe4b2444be5b62811b4dcfe8d.tar.gz slixmpp-2b3b86e281da03dfe4b2444be5b62811b4dcfe8d.tar.bz2 slixmpp-2b3b86e281da03dfe4b2444be5b62811b4dcfe8d.tar.xz slixmpp-2b3b86e281da03dfe4b2444be5b62811b4dcfe8d.zip |
Allow event handlers to be coroutine functions
And do not copy data when running events with XMLStream.event()
Diffstat (limited to 'slixmpp/xmlstream/xmlstream.py')
-rw-r--r-- | slixmpp/xmlstream/xmlstream.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index 2e109792..573ca829 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -13,7 +13,6 @@ """ import functools -import copy import logging import socket as Socket import ssl @@ -705,18 +704,22 @@ class XMLStream(asyncio.BaseProtocol): handlers = self.__event_handlers.get(name, []) for handler in handlers: - #TODO: Data should not be copied, but should be read only, - # but this might break current code so it's left for future. handler_callback, disposable = handler - out_data = copy.copy(data) if len(handlers) > 1 else data - old_exception = getattr(data, 'exception', None) - try: - handler_callback(out_data) - except Exception as e: - if old_exception: - old_exception(e) - else: + # If the callback is a coroutine, schedule it instead of + # running it directly + if asyncio.iscoroutinefunction(handler_callback): + @asyncio.coroutine + def handler_callback_routine(cb): + try: + yield from cb(data) + except Exception as e: + self.exception(e) + asyncio.async(handler_callback_routine(handler_callback)) + else: + try: + handler_callback(data) + except Exception as e: self.exception(e) if disposable: # If the handler is disposable, we will go ahead and |