Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_robotparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,28 @@ class InvalidCrawlDelayTest(BaseRobotTest, unittest.TestCase):
bad = []


class NonDecimalCrawlDelayTest(BaseRequestRateTest, unittest.TestCase):
# Non-decimal Unicode digits pass str.isdigit() but int() rejects
# them, so the directive must be silently ignored, not raise.
robots_txt = """\
User-Agent: *
Disallow: /.
Crawl-delay: ²
"""
good = ['/foo.html']
bad = []


class NonDecimalRequestRateTest(BaseRequestRateTest, unittest.TestCase):
robots_txt = """\
User-agent: *
Disallow: /tmp/
Request-rate: ²/5
"""
good = ['/foo.html']
bad = ['/tmp/']


class AnotherInvalidRequestRateTest(BaseRobotTest, unittest.TestCase):
# also test that Allow and Diasallow works well with each other
robots_txt = """\
Expand Down
6 changes: 3 additions & 3 deletions Lib/urllib/robotparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ def parse(self, lines):
# before trying to convert to int we need to make
# sure that robots.txt has valid syntax otherwise
# it will crash
if line[1].strip().isdigit():
if line[1].strip().isdecimal():
entry.delay = int(line[1])
state = 2
elif line[0] == "request-rate":
if state != 0:
numbers = line[1].split('/')
# check if all values are sane
if (len(numbers) == 2 and numbers[0].strip().isdigit()
and numbers[1].strip().isdigit()):
if (len(numbers) == 2 and numbers[0].strip().isdecimal()
and numbers[1].strip().isdecimal()):
entry.req_rate = RequestRate(int(numbers[0]), int(numbers[1]))
state = 2
elif line[0] == "sitemap":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:class:`urllib.robotparser.RobotFileParser` now silently ignores a
``Crawl-delay`` or ``Request-rate`` value written with non-decimal digits
(such as ``U+00B2 SUPERSCRIPT TWO``) instead of raising :exc:`ValueError`
and aborting the parse of the whole ``robots.txt`` file.
Loading