diff --git a/docs/news.rst b/docs/news.rst index ef3b549e7..8b1d51674 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -126,6 +126,15 @@ Backward-incompatible changes also enforced for start requests. (:issue:`6777`) +- Calling :func:`scrapy.utils.reactor.is_asyncio_reactor_installed` without + an installed reactor now raises an exception instead of installing a + reactor. This shouldn't affect normal Scrapy use cases, but it may affect + 3rd-party test suites that use Scrapy internals such as + :class:`~scrapy.crawler.Crawler` and don't install a reactor explicitly. If + you are affected by this change, you most likely need to install the + reactor before running Scrapy code that expects it to be installed. + (:issue:`6732`, :issue:`6735`) + - The ``from_settings()`` method of :class:`~scrapy.spidermiddlewares.urllength.UrlLengthMiddleware`, deprecated in Scrapy 2.12.0, is removed earlier than the usual deprecation diff --git a/scrapy/utils/reactor.py b/scrapy/utils/reactor.py index 9c2754394..1b179f988 100644 --- a/scrapy/utils/reactor.py +++ b/scrapy/utils/reactor.py @@ -202,6 +202,11 @@ def is_asyncio_reactor_installed() -> bool: """Check whether the installed reactor is :class:`~twisted.internet.asyncioreactor.AsyncioSelectorReactor`. Raise a :exc:`RuntimeError` if no reactor is installed. + + .. versionchanged:: 2.13 + In earlier Scrapy versions this function silently installed the default + reactor if there was no reactor installed. Now it raises an exception to + prevent silent problems in this case. """ if not is_reactor_installed(): raise RuntimeError( diff --git a/scrapy/utils/test.py b/scrapy/utils/test.py index 2da526cd8..4a732bd72 100644 --- a/scrapy/utils/test.py +++ b/scrapy/utils/test.py @@ -18,7 +18,7 @@ from twisted.trial.unittest import SkipTest from scrapy.exceptions import ScrapyDeprecationWarning from scrapy.utils.boto import is_botocore_available from scrapy.utils.deprecate import create_deprecated_class -from scrapy.utils.reactor import is_asyncio_reactor_installed +from scrapy.utils.reactor import is_asyncio_reactor_installed, is_reactor_installed from scrapy.utils.spider import DefaultSpider if TYPE_CHECKING: @@ -117,6 +117,11 @@ def get_reactor_settings() -> dict[str, Any]: settings, so tests that run the crawler in the current process may need to pass a correct ``"TWISTED_REACTOR"`` setting value when creating it. """ + if not is_reactor_installed(): + raise RuntimeError( + "get_reactor_settings() called without an installed reactor," + " you may need to install a reactor explicitly when running your tests." + ) settings: dict[str, Any] = {} if not is_asyncio_reactor_installed(): settings["TWISTED_REACTOR"] = None