From 2a2ac73845ffc8695e2bc55746f45e1a18d55e6c Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 1 Jul 2011 15:15:13 -0700 Subject: So using sys.excepthook to catch errors only works once. The error bubbles through the event processing loop, breaking it and hanging the application. Instead, there is now a .exception(e) method on XMLStream which may be overridden or reassigned that will receive all unhandled exceptions (read: not XMPPError) from event and stream handlers. --- sleekxmpp/stanza/rootstanza.py | 5 +- sleekxmpp/xmlstream/xmlstream.py | 29 +++--------- tests/test_stream_exceptions.py | 99 ++++++++++++++++++++++++++++++++++++++-- tests/test_stream_xep_0030.py | 14 +++--- tests/test_stream_xep_0128.py | 1 - tests/test_stream_xep_0249.py | 1 - 6 files changed, 111 insertions(+), 38 deletions(-) diff --git a/sleekxmpp/stanza/rootstanza.py b/sleekxmpp/stanza/rootstanza.py index bc11476e..9e1d1cfa 100644 --- a/sleekxmpp/stanza/rootstanza.py +++ b/sleekxmpp/stanza/rootstanza.py @@ -64,8 +64,7 @@ class RootStanza(StanzaBase): # log the error log.exception('Error handling {%s}%s stanza' % (self.namespace, self.name)) - # Finally raise the exception, so it can be handled (or not) - # at a higher level by using sys.excepthook. - raise e + # Finally raise the exception to a global exception handler + self.stream.exception(e) register_stanza_plugin(RootStanza, Error) diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 6282c8d0..c7d0d3a8 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -764,7 +764,6 @@ class XMLStream(object): Event handlers and the send queue will be threaded regardless of this parameter's value. """ - self._thread_excepthook() self.scheduler.process(threaded=True) def start_thread(name, target): @@ -1052,30 +1051,16 @@ class XMLStream(object): self.event_queue.put(('quit', None, None)) return - def _thread_excepthook(self): + def exception(self, exception): """ - If a threaded event handler raises an exception, there is no way to - catch it except with an excepthook. Currently, each thread has its own - excepthook, but ideally we could use the main sys.excepthook. + Process an unknown exception. - Modifies threading.Thread to use sys.excepthook when an exception - is not caught. - """ - init_old = threading.Thread.__init__ - - def init(self, *args, **kwargs): - init_old(self, *args, **kwargs) - run_old = self.run + Meant to be overridden. - def run_with_except_hook(*args, **kw): - try: - run_old(*args, **kw) - except (KeyboardInterrupt, SystemExit): - raise - except: - sys.excepthook(*sys.exc_info()) - self.run = run_with_except_hook - threading.Thread.__init__ = init + Arguments: + exception -- An unhandled exception object. + """ + pass # To comply with PEP8, method names now use underscores. diff --git a/tests/test_stream_exceptions.py b/tests/test_stream_exceptions.py index 1143ce28..c41edbb2 100644 --- a/tests/test_stream_exceptions.py +++ b/tests/test_stream_exceptions.py @@ -12,7 +12,6 @@ class TestStreamExceptions(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() def testExceptionReply(self): @@ -23,8 +22,33 @@ class TestStreamExceptions(SleekTest): msg['body'] = 'Body changed' raise XMPPError(clear=False) + self.stream_start() + self.xmpp.add_event_handler('message', message) + + self.recv(""" + + This is going to cause an error. + + """) + + self.send(""" + + This is going to cause an error. + + + + + """) + + def testExceptionContinueWorking(self): + """Test that Sleek continues to respond after an XMPPError is raised.""" + + def message(msg): + msg.reply() + msg['body'] = 'Body changed' + raise XMPPError(clear=False) - sys.excepthook = lambda *args, **kwargs: None self.stream_start() self.xmpp.add_event_handler('message', message) @@ -44,6 +68,22 @@ class TestStreamExceptions(SleekTest): """) + self.recv(""" + + This is going to cause an error. + + """) + + self.send(""" + + This is going to cause an error. + + + + + """) + def testXMPPErrorException(self): """Test raising an XMPPError exception.""" @@ -153,9 +193,8 @@ class TestStreamExceptions(SleekTest): def catch_error(*args, **kwargs): raised_errors.append(True) - sys.excepthook = catch_error - self.stream_start() + self.xmpp.exception = catch_error self.xmpp.add_event_handler('message', message) self.recv(""" @@ -178,6 +217,58 @@ class TestStreamExceptions(SleekTest): self.assertEqual(raised_errors, [True], "Exception was not raised: %s" % raised_errors) + def testUnknownException(self): + """Test Sleek continues to respond after an unknown exception.""" + + raised_errors = [] + + def message(msg): + raise ValueError("Did something wrong") + + def catch_error(*args, **kwargs): + raised_errors.append(True) + + self.stream_start() + self.xmpp.exception = catch_error + self.xmpp.add_event_handler('message', message) + + self.recv(""" + + This is going to cause an error. + + """) + + self.send(""" + + + + + SleekXMPP got into trouble. + + + + """) + + self.recv(""" + + This is going to cause an error. + + """) + + self.send(""" + + + + + SleekXMPP got into trouble. + + + + """) + + self.assertEqual(raised_errors, [True, True], "Exceptions were not raised: %s" % raised_errors) suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamExceptions) diff --git a/tests/test_stream_xep_0030.py b/tests/test_stream_xep_0030.py index c960fc7a..1666d3a1 100644 --- a/tests/test_stream_xep_0030.py +++ b/tests/test_stream_xep_0030.py @@ -12,7 +12,6 @@ class TestStreamDisco(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() def testInfoEmptyDefaultNode(self): @@ -531,11 +530,6 @@ class TestStreamDisco(SleekTest): raised_exceptions = [] - def catch_exception(*args, **kwargs): - raised_exceptions.append(True) - - sys.excepthook = catch_exception - self.stream_start(mode='client', plugins=['xep_0030', 'xep_0059']) @@ -544,8 +538,14 @@ class TestStreamDisco(SleekTest): iterator=True) results.amount = 10 + def run_test(): + try: + results.next() + except StopIteration: + raised_exceptions.append(True) + t = threading.Thread(name="get_items_iterator", - target=results.next) + target=run_test) t.start() self.send(""" diff --git a/tests/test_stream_xep_0128.py b/tests/test_stream_xep_0128.py index 6fee6556..42fc9143 100644 --- a/tests/test_stream_xep_0128.py +++ b/tests/test_stream_xep_0128.py @@ -13,7 +13,6 @@ class TestStreamExtendedDisco(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() def testUsingExtendedInfo(self): diff --git a/tests/test_stream_xep_0249.py b/tests/test_stream_xep_0249.py index f49d1f7e..9a25253f 100644 --- a/tests/test_stream_xep_0249.py +++ b/tests/test_stream_xep_0249.py @@ -13,7 +13,6 @@ class TestStreamDirectInvite(SleekTest): """ def tearDown(self): - sys.excepthook = sys.__excepthook__ self.stream_close() def testReceiveInvite(self): -- cgit v1.2.3 From 086bf89d699c88ab89ad1e1975d6022335ca5c04 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 3 Jul 2011 00:35:36 -0700 Subject: Added XEP-0066: Out-of-Band Data --- sleekxmpp/plugins/xep_0066/__init__.py | 11 +++++ sleekxmpp/plugins/xep_0066/oob.py | 89 ++++++++++++++++++++++++++++++++++ sleekxmpp/plugins/xep_0066/stanza.py | 33 +++++++++++++ tests/test_stream_xep_0066.py | 72 +++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 sleekxmpp/plugins/xep_0066/__init__.py create mode 100644 sleekxmpp/plugins/xep_0066/oob.py create mode 100644 sleekxmpp/plugins/xep_0066/stanza.py create mode 100644 tests/test_stream_xep_0066.py diff --git a/sleekxmpp/plugins/xep_0066/__init__.py b/sleekxmpp/plugins/xep_0066/__init__.py new file mode 100644 index 00000000..ebfbd0c2 --- /dev/null +++ b/sleekxmpp/plugins/xep_0066/__init__.py @@ -0,0 +1,11 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.plugins.xep_0066 import stanza +from sleekxmpp.plugins.xep_0066.stanza import OOB, OOBTransfer +from sleekxmpp.plugins.xep_0066.oob import xep_0066 diff --git a/sleekxmpp/plugins/xep_0066/oob.py b/sleekxmpp/plugins/xep_0066/oob.py new file mode 100644 index 00000000..b4322351 --- /dev/null +++ b/sleekxmpp/plugins/xep_0066/oob.py @@ -0,0 +1,89 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + + +from sleekxmpp.stanza import Message, Presence, Iq +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.xmlstream.handler import Callback +from sleekxmpp.xmlstream.matcher import StanzaPath +from sleekxmpp.plugins.base import base_plugin +from sleekxmpp.plugins.xep_0066 import stanza + + +class xep_0066(base_plugin): + + """ + XEP-0066: Out-of-Band Data + + Out-of-Band Data is a basic method for transferring files between + XMPP agents. The URL of the resource in question is sent to the receiving + entity, which then downloads the resource before responding to the OOB + request. OOB is also used as a generic means to transmit URLs in other + stanzas to indicate where to find additional information. + + Also see . + + Events: + oob_transfer -- Raised when a request to download a resource + has been received. + + Methods: + send_oob -- Send a request to another entity to download a file + or other addressable resource. + """ + + def plugin_init(self): + """Start the XEP-0066 plugin.""" + self.xep = '0066' + self.description = 'Out-of-Band Transfer' + self.stanza = stanza + + register_stanza_plugin(Iq, stanza.OOBTransfer) + register_stanza_plugin(Message, stanza.OOB) + register_stanza_plugin(Presence, stanza.OOB) + + self.xmpp.register_handler( + Callback('OOB Transfer', + StanzaPath('iq@type=set/oob_transfer'), + self._handle_transfer)) + + def post_init(self): + """Handle cross-plugin dependencies.""" + base_plugin.post_init(self) + self.xmpp['xep_0030'].add_feature(stanza.OOBTransfer.namespace) + self.xmpp['xep_0030'].add_feature(stanza.OOB.namespace) + + def send_oob(self, to, url, desc=None, ifrom=None, **iqargs): + """ + Initiate a basic file transfer by sending the URL of + a file or other resource. + + Arguments: + url -- The URL of the resource to transfer. + desc -- An optional human readable description of the item + that is to be transferred. + ifrom -- Specifiy the sender's JID. + block -- If true, block and wait for the stanzas' reply. + timeout -- The time in seconds to block while waiting for + a reply. If None, then wait indefinitely. + callback -- Optional callback to execute when a reply is + received instead of blocking and waiting for + the reply. + """ + iq = self.xmpp.Iq() + iq['type'] = 'set' + iq['to'] = to + if ifrom: + iq['from'] = ifrom + iq['oob_transfer']['url'] = url + iq['oob_transfer']['desc'] = desc + return iq.send(**iqargs) + + def _handle_transfer(self, iq): + """Handle receiving an out-of-band transfer request.""" + self.xmpp.event('oob_transfer', iq) diff --git a/sleekxmpp/plugins/xep_0066/stanza.py b/sleekxmpp/plugins/xep_0066/stanza.py new file mode 100644 index 00000000..21387485 --- /dev/null +++ b/sleekxmpp/plugins/xep_0066/stanza.py @@ -0,0 +1,33 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.xmlstream import ElementBase + + +class OOBTransfer(ElementBase): + + """ + """ + + name = 'query' + namespace = 'jabber:iq:oob' + plugin_attrib = 'oob_transfer' + interfaces = set(('url', 'desc', 'sid')) + sub_interfaces = set(('url', 'desc')) + + +class OOB(ElementBase): + + """ + """ + + name = 'x' + namespace = 'jabber:x:oob' + plugin_attrib = 'oob' + interfaces = set(('url', 'desc')) + sub_interfaces = interfaces diff --git a/tests/test_stream_xep_0066.py b/tests/test_stream_xep_0066.py new file mode 100644 index 00000000..3dbaf840 --- /dev/null +++ b/tests/test_stream_xep_0066.py @@ -0,0 +1,72 @@ +import time +import threading + +from sleekxmpp.test import * + + +class TestOOB(SleekTest): + + def tearDown(self): + self.stream_close() + + def testSendOOB(self): + """Test sending an OOB transfer request.""" + self.stream_start(plugins=['xep_0066', 'xep_0030']) + + url = 'http://github.com/fritzy/SleekXMPP/blob/master/README' + + t = threading.Thread( + name='send_oob', + target=self.xmpp['xep_0066'].send_oob, + args=('user@example.com', url), + kwargs={'desc': 'SleekXMPP README'}) + + t.start() + + self.send(""" + + + http://github.com/fritzy/SleekXMPP/blob/master/README + SleekXMPP README + + + """) + + self.recv(""" + + """) + + t.join() + + def testReceiveOOB(self): + """Test receiving an OOB request.""" + self.stream_start(plugins=['xep_0066', 'xep_0030']) + + events = [] + + def receive_oob(iq): + events.append(iq['oob_transfer']['url']) + + self.xmpp.add_event_handler('oob_transfer', receive_oob) + + self.recv(""" + + + http://github.com/fritzy/SleekXMPP/blob/master/README + SleekXMPP README + + + """) + + time.sleep(0.1) + + self.assertEqual(events, + ['http://github.com/fritzy/SleekXMPP/blob/master/README'], + 'URL was not received: %s' % events) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestOOB) -- cgit v1.2.3