Scrapy, a fast high-level web crawling & scraping framework for Python.
Cleanup decode_robotstxt(). (#8316)
Andrey Rakhmatullincc4238c3a27a
diff --git a/scrapy/robotstxt.py b/scrapy/robotstxt.pyindex 04066d306..ba8740978 100644--- a/scrapy/robotstxt.py+++ b/scrapy/robotstxt.py@@ -1,13 +1,13 @@from __future__ import annotations-import logging-import sys+import warningsfrom abc import ABCMeta, abstractmethodfrom typing import TYPE_CHECKING, Self, castfrom urllib.robotparser import RobotFileParserfrom protego import Protego+from scrapy.exceptions import ScrapyDeprecationWarningfrom scrapy.utils.python import to_unicodeif TYPE_CHECKING:@@ -15,28 +15,26 @@ if TYPE_CHECKING:from scrapy.crawler import Crawler-logger = logging.getLogger(__name__)--def decode_robotstxt(- robotstxt_body: bytes, spider: Spider | None, to_native_str_type: bool = False+ robotstxt_body: bytes,+ spider: Spider | None = None,+ to_native_str_type: bool | None = None,) -> str:- try:- if to_native_str_type:- body_decoded = robotstxt_body.decode()- else:- body_decoded = robotstxt_body.decode("utf-8-sig", errors="ignore")- except UnicodeDecodeError:- # If we found garbage or robots.txt in an encoding other than UTF-8, disregard it.- # Switch to 'allow all' state.- logger.warning(- "Failure while parsing robots.txt. File either contains garbage or "- "is in an encoding other than UTF-8, treating it as an empty file.",- exc_info=sys.exc_info(),- extra={"spider": spider},+ if spider is not None: # pragma: no cover+ warnings.warn(+ "Passing a spider argument to decode_robotstxt() is "+ "deprecated and has no effect.",+ category=ScrapyDeprecationWarning,+ stacklevel=2,+ )+ if to_native_str_type is not None: # pragma: no cover+ warnings.warn(+ "Passing a to_native_str_type argument to decode_robotstxt() is "+ "deprecated and has no effect.",+ category=ScrapyDeprecationWarning,+ stacklevel=2,)- body_decoded = ""- return body_decoded+ return robotstxt_body.decode("utf-8-sig", errors="ignore")class RobotParser(metaclass=ABCMeta):@@ -77,7 +75,7 @@ class RobotParser(metaclass=ABCMeta):class PythonRobotParser(RobotParser):def __init__(self, robotstxt_body: bytes, spider: Spider | None):self.spider: Spider | None = spider- body_decoded = decode_robotstxt(robotstxt_body, spider, to_native_str_type=True)+ body_decoded = decode_robotstxt(robotstxt_body)self.rp: RobotFileParser = RobotFileParser()self.rp.parse(body_decoded.splitlines())@@ -102,7 +100,7 @@ class RerpRobotParser(RobotParser):self.spider: Spider | None = spiderself.rp: RobotExclusionRulesParser = RobotExclusionRulesParser()- body_decoded = decode_robotstxt(robotstxt_body, spider)+ body_decoded = decode_robotstxt(robotstxt_body)self.rp.parse(body_decoded)@classmethod@@ -123,7 +121,7 @@ class RerpRobotParser(RobotParser):class ProtegoRobotParser(RobotParser):def __init__(self, robotstxt_body: bytes, spider: Spider | None):self.spider: Spider | None = spider- body_decoded = decode_robotstxt(robotstxt_body, spider)+ body_decoded = decode_robotstxt(robotstxt_body)self.rp = Protego.parse(body_decoded)@classmethoddiff --git a/tests/test_robotstxt_interface.py b/tests/test_robotstxt_interface.pyindex ace6a2f31..bfb249f2b 100644--- a/tests/test_robotstxt_interface.py+++ b/tests/test_robotstxt_interface.py@@ -86,6 +86,16 @@ class TestRobotParserBase(ABC):assert rp.allowed("https://site.local/index.html", "*")assert rp.allowed("https://site.local/disallowed", "*")+ def test_non_utf8_response(self) -> None:+ rp = self._parse(b"# caf\xe9\nUser-agent: *\nDisallow: /disallowed\n")+ assert rp.allowed("https://site.local/", "*")+ assert not rp.allowed("https://site.local/disallowed", "*")++ def test_utf8_bom(self) -> None:+ rp = self._parse(b"\xef\xbb\xbfUser-agent: *\nDisallow: /disallowed\n")+ assert rp.allowed("https://site.local/", "*")+ assert not rp.allowed("https://site.local/disallowed", "*")+def test_crawl_delay(self) -> None:robotstxt_body = b"User-agent: *\nDisallow: /private\nCrawl-delay: 10\n"rp = self._parse(robotstxt_body)@@ -135,27 +145,20 @@ class TestRobotParser:class TestDecodeRobotsTxt:- def test_native_string_conversion(self) -> None:- robotstxt_body = b"User-agent: *\nDisallow: /\n"- decoded_content = decode_robotstxt(- robotstxt_body, spider=None, to_native_str_type=True- )- assert decoded_content == "User-agent: *\nDisallow: /\n"-def test_decode_utf8(self) -> None:robotstxt_body = b"User-agent: *\nDisallow: /\n"- decoded_content = decode_robotstxt(robotstxt_body, spider=None)+ decoded_content = decode_robotstxt(robotstxt_body)assert decoded_content == "User-agent: *\nDisallow: /\n"def test_decode_non_utf8(self) -> None:robotstxt_body = b"User-agent: *\n\xffDisallow: /\n"- decoded_content = decode_robotstxt(robotstxt_body, spider=None)+ decoded_content = decode_robotstxt(robotstxt_body)assert decoded_content == "User-agent: *\nDisallow: /\n"# UTF-8 BOM at the beginning of the file ignoreddef test_decode_utf8_bom(self) -> None:robotstxt_body = b"\xef\xbb\xbfUser-agent: *\nDisallow: /\n"- decoded_content = decode_robotstxt(robotstxt_body, spider=None)+ decoded_content = decode_robotstxt(robotstxt_body)assert decoded_content == "User-agent: *\nDisallow: /\n"