Scrapy, a fast high-level web crawling & scraping framework for Python.
Deprecate Crawler.spiders attribute
Julia Medina419026615f04
diff --git a/docs/topics/api.rst b/docs/topics/api.rstindex 0329e2a8f..03a0b4124 100644--- a/docs/topics/api.rst+++ b/docs/topics/api.rst@@ -76,12 +76,6 @@ how you :ref:`configure the downloader middlewaresFor an introduction on extensions and a list of available extensions onScrapy see :ref:`topics-extensions`.- .. attribute:: spiders-- The spider manager which takes care of loading spiders.-- Most extensions won't need to access this attribute.-.. attribute:: engineThe execution engine, which coordinates the core crawling logicdiff --git a/scrapy/crawler.py b/scrapy/crawler.pyindex 352cff6e5..52e57fe83 100644--- a/scrapy/crawler.py+++ b/scrapy/crawler.py@@ -1,5 +1,6 @@import siximport signal+import warningsfrom twisted.internet import reactor, defer@@ -7,6 +8,7 @@ from scrapy.core.engine import ExecutionEnginefrom scrapy.resolver import CachingThreadedResolverfrom scrapy.extension import ExtensionManagerfrom scrapy.signalmanager import SignalManager+from scrapy.exceptions import ScrapyDeprecationWarningfrom scrapy.utils.ossignal import install_shutdown_handlers, signal_namesfrom scrapy.utils.misc import load_objectfrom scrapy import log, signals@@ -23,14 +25,22 @@ class Crawler(object):self.logformatter = lf_cls.from_crawler(self)self.extensions = ExtensionManager.from_crawler(self)- # Attribute kept for backward compatibility (Use CrawlerRunner.spiders)- spman_cls = load_object(self.settings['SPIDER_MANAGER_CLASS'])- self.spiders = spman_cls.from_settings(self.settings)-self.crawling = Falseself.spider = Noneself.engine = None+ @property+ def spiders(self):+ if not hasattr(self, '_spiders'):+ warnings.warn("Crawler.spiders is deprecated, use "+ "CrawlerRunner.spiders or instantiate "+ "scrapy.spidermanager.SpiderManager with your "+ "settings.",+ category=ScrapyDeprecationWarning, stacklevel=2)+ spman_cls = load_object(self.settings['SPIDER_MANAGER_CLASS'])+ self._spiders = spman_cls.from_settings(self.settings)+ return self._spiders+@defer.inlineCallbacksdef crawl(self, *args, **kwargs):assert not self.crawling, "Crawling already taking place"diff --git a/tests/py3-ignores.txt b/tests/py3-ignores.txtindex ef88eab7e..f3c667cd0 100644--- a/tests/py3-ignores.txt+++ b/tests/py3-ignores.txt@@ -8,6 +8,7 @@ tests/test_contrib_exporter.pytests/test_contrib_linkextractors.pytests/test_contrib_loader.pytests/test_crawl.py+tests/test_crawler.pytests/test_djangoitem/__init__.pytests/test_downloader_handlers.pytests/test_downloadermiddleware_ajaxcrawlable.pydiff --git a/tests/test_crawler.py b/tests/test_crawler.pynew file mode 100644index 000000000..55381c030--- /dev/null+++ b/tests/test_crawler.py@@ -0,0 +1,24 @@+import warnings+import unittest++from scrapy.crawler import Crawler+from scrapy.settings import Settings+from scrapy.utils.spider import DefaultSpider+from scrapy.utils.misc import load_object+++class CrawlerTestCase(unittest.TestCase):++ def setUp(self):+ self.crawler = Crawler(DefaultSpider, Settings())++ def test_deprecated_attribute_spiders(self):+ with warnings.catch_warnings(record=True) as w:+ spiders = self.crawler.spiders+ self.assertEqual(len(w), 1)+ self.assertIn("Crawler.spiders", str(w[0].message))+ sm_cls = load_object(self.crawler.settings['SPIDER_MANAGER_CLASS'])+ self.assertIsInstance(spiders, sm_cls)++ self.crawler.spiders+ self.assertEqual(len(w), 1, "Warn deprecated access only once")