Skip to content

Commit

Permalink
Remove top-level imports that install the reactor from scrapy.extensi…
Browse files Browse the repository at this point in the history
…ons.telnet. (scrapy#6432)
  • Loading branch information
wRAR authored Jul 9, 2024
1 parent d8ecd28 commit ceedb02
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
22 changes: 7 additions & 15 deletions scrapy/extensions/telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@
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
Expand All @@ -33,6 +23,8 @@
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

Expand All @@ -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] = [
Expand Down Expand Up @@ -88,6 +76,10 @@ def stop_listening(self) -> None:
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"""

Expand Down
4 changes: 0 additions & 4 deletions tests/test_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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
Expand Down Expand Up @@ -482,7 +481,6 @@ class MySpider(scrapy.Spider):
"LOG_FILE": str(log_file),
# settings to avoid extra warnings
"REQUEST_FINGERPRINTER_IMPLEMENTATION": "2.7",
"TELNETCONSOLE_ENABLED": telnet.TWISTED_CONCH_AVAILABLE,
}

configure_logging()
Expand Down Expand Up @@ -516,8 +514,6 @@ class MySpider(scrapy.Spider):
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()
Expand Down
4 changes: 0 additions & 4 deletions tests/test_utils_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from testfixtures import LogCapture
from twisted.python.failure import Failure

from scrapy.extensions import telnet
from scrapy.utils.log import (
LogCounterHandler,
SpiderLoggerAdapter,
Expand Down Expand Up @@ -70,9 +69,6 @@ def test_different_name_logger(self):
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
Expand Down

0 comments on commit ceedb02

Please sign in to comment.