Scrapy, a fast high-level web crawling & scraping framework for Python.
Add warnings to components not working without a reactor (#7355)
Andrey Rakhmatullin03d105ac924e
diff --git a/scrapy/commands/shell.py b/scrapy/commands/shell.pyindex 080f62382..9f4faba3a 100644--- a/scrapy/commands/shell.py+++ b/scrapy/commands/shell.py@@ -83,6 +83,10 @@ class Command(ScrapyCommand):# crawling engine, so the set up in the crawl method won't workcrawler = self.crawler_process._create_crawler(spidercls)crawler._apply_settings()+ if not crawler.settings.getbool("TWISTED_ENABLED"):+ raise RuntimeError(+ "scrapy shell currently doesn't support TWISTED_ENABLED=False"+ )# The Shell class needs a persistent engine in the crawlercrawler.engine = crawler._create_engine()_schedule_coro(crawler.engine.start_async(_start_request_processing=False))diff --git a/scrapy/core/downloader/handlers/ftp.py b/scrapy/core/downloader/handlers/ftp.pyindex f261e8e7f..65660d389 100644--- a/scrapy/core/downloader/handlers/ftp.py+++ b/scrapy/core/downloader/handlers/ftp.py@@ -39,6 +39,7 @@ from urllib.parse import unquotefrom twisted.internet.protocol import ClientCreator, Protocolfrom scrapy.core.downloader.handlers.base import BaseDownloadHandler+from scrapy.exceptions import NotConfiguredfrom scrapy.http import Responsefrom scrapy.responsetypes import responsetypesfrom scrapy.utils.defer import maybe_deferred_to_future@@ -84,6 +85,8 @@ class FTPDownloadHandler(BaseDownloadHandler):}def __init__(self, crawler: Crawler):+ if not crawler.settings.getbool("TWISTED_ENABLED"):+ raise NotConfigured(f"{type(self).__name__} requires a Twisted reactor.")super().__init__(crawler)self.default_user = crawler.settings["FTP_USER"]self.default_password = crawler.settings["FTP_PASSWORD"]diff --git a/scrapy/core/downloader/handlers/http10.py b/scrapy/core/downloader/handlers/http10.pyindex af3588019..3820e79bd 100644--- a/scrapy/core/downloader/handlers/http10.py+++ b/scrapy/core/downloader/handlers/http10.py@@ -5,7 +5,7 @@ from __future__ import annotationsimport warningsfrom typing import TYPE_CHECKING-from scrapy.exceptions import ScrapyDeprecationWarning+from scrapy.exceptions import NotConfigured, ScrapyDeprecationWarningfrom scrapy.utils.defer import maybe_deferred_to_futurefrom scrapy.utils.misc import build_from_crawler, load_objectfrom scrapy.utils.python import to_unicode@@ -33,6 +33,8 @@ class HTTP10DownloadHandler:category=ScrapyDeprecationWarning,stacklevel=2,)+ if not crawler.settings.getbool("TWISTED_ENABLED"): # pragma: no cover+ raise NotConfigured(f"{type(self).__name__} requires a Twisted reactor.")self.HTTPClientFactory: type[ScrapyHTTPClientFactory] = load_object(settings["DOWNLOADER_HTTPCLIENTFACTORY"])diff --git a/scrapy/core/downloader/handlers/http11.py b/scrapy/core/downloader/handlers/http11.pyindex 16879cf76..acf2be55e 100644--- a/scrapy/core/downloader/handlers/http11.py+++ b/scrapy/core/downloader/handlers/http11.py@@ -34,6 +34,7 @@ from scrapy.core.downloader.contextfactory import load_context_factory_from_settfrom scrapy.exceptions import (DownloadCancelledError,DownloadTimeoutError,+ NotConfigured,ResponseDataLossError,StopDownload,)@@ -80,6 +81,8 @@ class _ResultT(TypedDict):class HTTP11DownloadHandler(BaseHttpDownloadHandler):def __init__(self, crawler: Crawler):+ if not crawler.settings.getbool("TWISTED_ENABLED"):+ raise NotConfigured(f"{type(self).__name__} requires a Twisted reactor.")super().__init__(crawler)self._crawler = crawlerdiff --git a/scrapy/core/downloader/handlers/http2.py b/scrapy/core/downloader/handlers/http2.pyindex c749a4a73..9b241d61d 100644--- a/scrapy/core/downloader/handlers/http2.py+++ b/scrapy/core/downloader/handlers/http2.py@@ -9,7 +9,7 @@ from twisted.web.client import URIfrom scrapy.core.downloader.contextfactory import load_context_factory_from_settingsfrom scrapy.core.downloader.handlers.base import BaseDownloadHandlerfrom scrapy.core.http2.agent import H2Agent, H2ConnectionPool, ScrapyProxyH2Agent-from scrapy.exceptions import DownloadTimeoutError+from scrapy.exceptions import DownloadTimeoutError, NotConfiguredfrom scrapy.utils._download_handlers import (normalize_bind_address,wrap_twisted_exceptions,@@ -32,6 +32,8 @@ class H2DownloadHandler(BaseDownloadHandler):lazy = Truedef __init__(self, crawler: Crawler):+ if not crawler.settings.getbool("TWISTED_ENABLED"):+ raise NotConfigured(f"{type(self).__name__} requires a Twisted reactor.")super().__init__(crawler)self._crawler = crawlerdiff --git a/scrapy/mail.py b/scrapy/mail.pyindex 6ebbe195f..718548b03 100644--- a/scrapy/mail.py+++ b/scrapy/mail.py@@ -23,6 +23,7 @@ from twisted.internet.defer import Deferredfrom scrapy.exceptions import ScrapyDeprecationWarningfrom scrapy.utils.misc import arg_to_iterfrom scrapy.utils.python import to_bytes+from scrapy.utils.reactorless import is_reactorlessif TYPE_CHECKING:from collections.abc import Callable, Sequence@@ -70,6 +71,8 @@ class MailSender:smtpssl: bool = False,debug: bool = False,):+ if is_reactorless(): # pragma: no cover+ raise RuntimeError(f"{type(self).__name__} requires a Twisted reactor.")self.smtphost: str = smtphostself.smtpport: int = smtpportself.smtpuser: bytes | None = _to_bytes_or_none(smtpuser)diff --git a/scrapy/shell.py b/scrapy/shell.pyindex 6f33f3f14..4ce6e20c4 100644--- a/scrapy/shell.py+++ b/scrapy/shell.py@@ -27,6 +27,7 @@ from scrapy.utils.console import DEFAULT_PYTHON_SHELLS, start_python_consolefrom scrapy.utils.datatypes import SequenceExcludefrom scrapy.utils.defer import _schedule_coro, deferred_f_from_coro_ffrom scrapy.utils.misc import load_object+from scrapy.utils.python import global_object_namefrom scrapy.utils.reactor import is_asyncio_reactor_installed, set_asyncio_event_loopfrom scrapy.utils.response import open_in_browser@@ -44,6 +45,10 @@ class Shell:code: str | None = None,):self.crawler: Crawler = crawler+ if not crawler.settings.getbool("TWISTED_ENABLED"): # pragma: no cover+ raise RuntimeError(+ f"{global_object_name(self.__class__)} currently doesn't support TWISTED_ENABLED=False."+ )self.update_vars: Callable[[dict[str, Any]], None] = update_vars or (lambda x: None)diff --git a/tests/test_command_shell.py b/tests/test_command_shell.pyindex ee515ede4..5699a64a1 100644--- a/tests/test_command_shell.py+++ b/tests/test_command_shell.py@@ -132,6 +132,15 @@ class TestShellCommand:ret, _, err = proc("shell", "-c", code, "--set", "TWISTED_ENABLED=False")assert ret == 0, err+ def test_no_reactor_unsupported(self) -> None:+ # to be removed when it's supported+ ret, out, err = proc("shell", "-c", "item", "--set", "TWISTED_ENABLED=False")+ assert ret == 1, out or err+ assert (+ "RuntimeError: scrapy shell currently doesn't support TWISTED_ENABLED=False"+ in err+ )+class TestInteractiveShell:def test_fetch(self, mockserver: MockServer) -> None:diff --git a/tests/test_downloader_handler_twisted_ftp.py b/tests/test_downloader_handler_twisted_ftp.pyindex f39e5ecaf..8f43d8071 100644--- a/tests/test_downloader_handler_twisted_ftp.py+++ b/tests/test_downloader_handler_twisted_ftp.py@@ -11,7 +11,10 @@ import pytestfrom pytest_twisted import async_yield_fixturefrom twisted.cred import checkers, credentials, portal+from scrapy import Spiderfrom scrapy.core.downloader.handlers.ftp import FTPDownloadHandler+from scrapy.crawler import Crawler+from scrapy.exceptions import NotConfiguredfrom scrapy.http import HtmlResponse, Request, Responsefrom scrapy.http.response.text import TextResponsefrom scrapy.utils.defer import deferred_f_from_coro_f@@ -200,3 +203,9 @@ class TestAnonymousFTP(TestFTPBase):p = portal.Portal(realm)p.registerChecker(checkers.AllowAnonymousAccess(), credentials.IAnonymous)return FTPFactory(portal=p, userAnonymous=self.username)+++def test_not_configured_without_reactor() -> None:+ crawler = Crawler(Spider, {"TWISTED_ENABLED": False})+ with pytest.raises(NotConfigured):+ FTPDownloadHandler.from_crawler(crawler)diff --git a/tests/test_downloader_handler_twisted_http11.py b/tests/test_downloader_handler_twisted_http11.pyindex 954dcd246..5d093cc17 100644--- a/tests/test_downloader_handler_twisted_http11.py+++ b/tests/test_downloader_handler_twisted_http11.py@@ -6,7 +6,10 @@ from typing import TYPE_CHECKING, Anyimport pytest+from scrapy import Spiderfrom scrapy.core.downloader.handlers.http11 import HTTP11DownloadHandler+from scrapy.crawler import Crawler+from scrapy.exceptions import NotConfiguredfrom tests.test_downloader_handlers_http_base import (TestHttp11Base,TestHttpProxyBase,@@ -32,6 +35,12 @@ class HTTP11DownloadHandlerMixin:return HTTP11DownloadHandler+def test_not_configured_without_reactor() -> None:+ crawler = Crawler(Spider, {"TWISTED_ENABLED": False})+ with pytest.raises(NotConfigured):+ HTTP11DownloadHandler.from_crawler(crawler)++class TestHttp11(HTTP11DownloadHandlerMixin, TestHttp11Base):passdiff --git a/tests/test_downloader_handler_twisted_http2.py b/tests/test_downloader_handler_twisted_http2.pyindex b14356453..7e2c06d6b 100644--- a/tests/test_downloader_handler_twisted_http2.py+++ b/tests/test_downloader_handler_twisted_http2.py@@ -9,7 +9,9 @@ import pytestfrom testfixtures import LogCapturefrom twisted.web.http import H2_ENABLED-from scrapy.exceptions import UnsupportedURLSchemeError+from scrapy import Spider+from scrapy.crawler import Crawler+from scrapy.exceptions import NotConfigured, UnsupportedURLSchemeErrorfrom scrapy.http import Requestfrom scrapy.utils.defer import deferred_f_from_coro_f, maybe_deferred_to_futurefrom tests.test_downloader_handlers_http_base import (@@ -47,6 +49,14 @@ class H2DownloadHandlerMixin:return H2DownloadHandler+def test_not_configured_without_reactor() -> None:+ from scrapy.core.downloader.handlers.http2 import H2DownloadHandler # noqa: PLC0415++ crawler = Crawler(Spider, {"TWISTED_ENABLED": False})+ with pytest.raises(NotConfigured):+ H2DownloadHandler.from_crawler(crawler)++class TestHttps2(H2DownloadHandlerMixin, TestHttps11Base):HTTP2_DATALOSS_SKIP_REASON = "Content-Length mismatch raises InvalidBodyLengthError"