Scrapy, a fast high-level web crawling & scraping framework for Python.
Remove top-level imports that install the reactor from scrapy.extensions.telnet. (#6432)
Andrey Rakhmatullinceedb026f8c8
diff --git a/scrapy/extensions/telnet.py b/scrapy/extensions/telnet.pyindex c4e01b3d9..c64a0b417 100644--- a/scrapy/extensions/telnet.py+++ b/scrapy/extensions/telnet.py@@ -10,21 +10,11 @@ import binasciiimport loggingimport osimport pprint-import tracebackfrom typing import TYPE_CHECKING, Any, Dict, Listfrom twisted.internet import protocolfrom twisted.internet.tcp import Port-try:- from twisted.conch import manhole, telnet- from twisted.conch.insults import insults-- TWISTED_CONCH_AVAILABLE = True-except (ImportError, SyntaxError):- _TWISTED_CONCH_TRACEBACK = traceback.format_exc()- TWISTED_CONCH_AVAILABLE = False-from scrapy import signalsfrom scrapy.exceptions import NotConfiguredfrom scrapy.utils.decorators import defers@@ -33,6 +23,8 @@ from scrapy.utils.reactor import listen_tcpfrom scrapy.utils.trackref import print_live_refsif TYPE_CHECKING:+ from twisted.conch import telnet+# typing.Self requires Python 3.11from typing_extensions import Self@@ -50,11 +42,7 @@ class TelnetConsole(protocol.ServerFactory):def __init__(self, crawler: Crawler):if not crawler.settings.getbool("TELNETCONSOLE_ENABLED"):raise NotConfigured- if not TWISTED_CONCH_AVAILABLE:- raise NotConfigured(- "TELNETCONSOLE_ENABLED setting is True but required twisted "- "modules failed to import:\n" + _TWISTED_CONCH_TRACEBACK- )+self.crawler: Crawler = crawlerself.noisy: bool = Falseself.portrange: List[int] = [@@ -88,6 +76,10 @@ class TelnetConsole(protocol.ServerFactory):self.port.stopListening()def protocol(self) -> telnet.TelnetTransport: # type: ignore[override]+ # these import twisted.internet.reactor+ from twisted.conch import manhole, telnet+ from twisted.conch.insults import insults+class Portal:"""An implementation of IPortal"""diff --git a/tests/test_crawler.py b/tests/test_crawler.pyindex 791ea1faa..c87e65758 100644--- a/tests/test_crawler.py+++ b/tests/test_crawler.py@@ -21,7 +21,6 @@ import scrapyfrom scrapy import Spiderfrom scrapy.crawler import Crawler, CrawlerProcess, CrawlerRunnerfrom scrapy.exceptions import ScrapyDeprecationWarning-from scrapy.extensions import telnetfrom scrapy.extensions.throttle import AutoThrottlefrom scrapy.settings import Settings, default_settingsfrom scrapy.spiderloader import SpiderLoader@@ -482,7 +481,6 @@ class CrawlerLoggingTestCase(unittest.TestCase):"LOG_FILE": str(log_file),# settings to avoid extra warnings"REQUEST_FINGERPRINTER_IMPLEMENTATION": "2.7",- "TELNETCONSOLE_ENABLED": telnet.TWISTED_CONCH_AVAILABLE,}configure_logging()@@ -516,8 +514,6 @@ class CrawlerLoggingTestCase(unittest.TestCase):custom_settings = {"LOG_FILE": str(log_file),"LOG_FILE_APPEND": False,- # disable telnet if not available to avoid an extra warning- "TELNETCONSOLE_ENABLED": telnet.TWISTED_CONCH_AVAILABLE,}configure_logging()diff --git a/tests/test_utils_log.py b/tests/test_utils_log.pyindex a8d080822..0f75bdb5c 100644--- a/tests/test_utils_log.py+++ b/tests/test_utils_log.py@@ -11,7 +11,6 @@ import pytestfrom testfixtures import LogCapturefrom twisted.python.failure import Failure-from scrapy.extensions import telnetfrom scrapy.utils.log import (LogCounterHandler,SpiderLoggerAdapter,@@ -70,9 +69,6 @@ class TopLevelFormatterTest(unittest.TestCase):class LogCounterHandlerTest(unittest.TestCase):def setUp(self):settings = {"LOG_LEVEL": "WARNING"}- if not telnet.TWISTED_CONCH_AVAILABLE:- # disable it to avoid the extra warning- settings["TELNETCONSOLE_ENABLED"] = Falseself.logger = logging.getLogger("test")self.logger.setLevel(logging.NOTSET)self.logger.propagate = False