Skip to content

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.py
index c4e01b3d9..c64a0b417 100644
--- a/scrapy/extensions/telnet.py
+++ b/scrapy/extensions/telnet.py
@@ -10,21 +10,11 @@ import binascii
import logging
import os
import pprint
-import traceback
from typing import TYPE_CHECKING, Any, Dict, List
from twisted.internet import protocol
from 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 signals
from scrapy.exceptions import NotConfigured
from scrapy.utils.decorators import defers
@@ -33,6 +23,8 @@ from scrapy.utils.reactor import listen_tcp
from scrapy.utils.trackref import print_live_refs
if TYPE_CHECKING:
+ from twisted.conch import telnet
+
# typing.Self requires Python 3.11
from 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 = crawler
self.noisy: bool = False
self.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.py
index 791ea1faa..c87e65758 100644
--- a/tests/test_crawler.py
+++ b/tests/test_crawler.py
@@ -21,7 +21,6 @@ import scrapy
from scrapy import Spider
from scrapy.crawler import Crawler, CrawlerProcess, CrawlerRunner
from scrapy.exceptions import ScrapyDeprecationWarning
-from scrapy.extensions import telnet
from scrapy.extensions.throttle import AutoThrottle
from scrapy.settings import Settings, default_settings
from 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.py
index a8d080822..0f75bdb5c 100644
--- a/tests/test_utils_log.py
+++ b/tests/test_utils_log.py
@@ -11,7 +11,6 @@ import pytest
from testfixtures import LogCapture
from twisted.python.failure import Failure
-from scrapy.extensions import telnet
from 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"] = False
self.logger = logging.getLogger("test")
self.logger.setLevel(logging.NOTSET)
self.logger.propagate = False