diff --git a/scrapy/tests/test_engine.py b/scrapy/tests/test_engine.py index 8fabd2913..29ccb76a3 100644 --- a/scrapy/tests/test_engine.py +++ b/scrapy/tests/test_engine.py @@ -101,6 +101,14 @@ class CrawlingSession(object): scrapymanager.start() self.port.stopListening() self.wasrun = True + # FIXME: extremly ugly hack to avoid propagating errors to other + # stats because of living signals. This whole test_engine.py should + # be rewritten from scratch actually. + from scrapy.utils.signal import disconnect_all + from scrapy.stats import signals as stats_signals + disconnect_all(stats_signals.stats_spider_opened) + disconnect_all(stats_signals.stats_spider_closing) + disconnect_all(stats_signals.stats_spider_closed) def geturl(self, path): return "http://localhost:%s%s" % (self.portno, path) diff --git a/scrapy/utils/signal.py b/scrapy/utils/signal.py index a979382d9..dc5471616 100644 --- a/scrapy/utils/signal.py +++ b/scrapy/utils/signal.py @@ -2,7 +2,8 @@ from twisted.python.failure import Failure -from scrapy.xlib.pydispatch.dispatcher import Any, Anonymous, liveReceivers, getAllReceivers +from scrapy.xlib.pydispatch.dispatcher import Any, Anonymous, liveReceivers, \ + getAllReceivers, disconnect from scrapy.xlib.pydispatch.robustapply import robustApply from scrapy import log @@ -27,3 +28,10 @@ def send_catch_log(signal=Any, sender=Anonymous, *arguments, **named): result = response responses.append((receiver, result)) return responses + +def disconnect_all(signal=Any, sender=Any): + """Disconnect all signal handlers. Useful for cleaning up after running + tests + """ + for receiver in liveReceivers(getAllReceivers(sender, signal)): + disconnect(receiver, signal=signal, sender=sender)