Scrapy, a fast high-level web crawling & scraping framework for Python.
utils.signal: made send_catch_log function more robust (by using pydispatch.robust.sendRobust) and added unittests
Pablo Hoffman499dc6677cd5
diff --git a/scrapy/tests/test_utils_signal.py b/scrapy/tests/test_utils_signal.pynew file mode 100644index 000000000..314ab9454--- /dev/null+++ b/scrapy/tests/test_utils_signal.py@@ -0,0 +1,42 @@+import unittest++from scrapy.xlib.pydispatch import dispatcher+from scrapy.utils.signal import send_catch_log+from scrapy import log++test_signal = object()++class SignalUtilsTest(unittest.TestCase):++ def test_send_catch_log(self):+ handlers_called = set()++ def test_handler_error(arg):+ handlers_called.add(test_handler_error)+ a = 1/0++ def test_handler_check(arg):+ handlers_called.add(test_handler_check)+ assert arg == 'test'++ def log_received(message, level):+ handlers_called.add(log_received)+ assert "test_handler_error" in message+ assert level == log.ERROR++ dispatcher.connect(log_received, signal=log.logmessage_received)+ dispatcher.connect(test_handler_error, signal=test_signal)+ dispatcher.connect(test_handler_check, signal=test_signal)+ send_catch_log(test_signal, arg='test')++ assert test_handler_error in handlers_called+ assert test_handler_check in handlers_called+ assert log_received in handlers_called++ dispatcher.disconnect(log_received, signal=log.logmessage_received)+ dispatcher.disconnect(test_handler_error, signal=test_signal)+ dispatcher.disconnect(test_handler_check, signal=test_signal)+++if __name__ == "__main__":+ unittest.main()diff --git a/scrapy/utils/signal.py b/scrapy/utils/signal.pyindex a19449486..8c0ca3ae6 100644--- a/scrapy/utils/signal.py+++ b/scrapy/utils/signal.py@@ -1,13 +1,14 @@"""Helper functinos for working with signals"""from scrapy.xlib.pydispatch import dispatcher+from scrapy.xlib.pydispatch.robust import sendRobustfrom scrapy import logdef send_catch_log(*args, **kwargs):- """Same as dispatcher.send but logs any exceptions raised by the signal- handlers+ """Same as dispatcher.robust.sendRobust but logs any exceptions raised by+ the signal handlers"""- try:- dispatcher.send(*args, **kwargs)- except:- log.exc("Exception catched on signal dispatch")+ for receiver, result in sendRobust(*args, **kwargs):+ if isinstance(result, Exception):+ log.msg("Exception caught on signal dispatch: receiver=%r, " \+ " exception=%r" % (receiver, result), level=log.ERROR)