Remove top-level imports that install the reactor from scrapy.extensions.telnet. (#6432)

This commit is contained in:
Andrey Rakhmatullin 2024-07-09 11:34:58 +05:00 committed by GitHub
parent d8ecd28c55
commit ceedb026f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 23 deletions

View File

@ -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"""

View File

@ -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()

View File

@ -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