Skip to content

gh-153404: Silently ignore non-decimal digits in robots.txt Crawl-delay and Request-rate#153405

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
tonghuaroot:robotparser-isdigit-decimal
Jul 17, 2026
Merged

gh-153404: Silently ignore non-decimal digits in robots.txt Crawl-delay and Request-rate#153405
serhiy-storchaka merged 3 commits into
python:mainfrom
tonghuaroot:robotparser-isdigit-decimal

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

str.isdigit() returns True for non-decimal Unicode digits such as U+00B2 SUPERSCRIPT TWO, which int() then rejects with ValueError. In RobotFileParser.parse() this raised a raw ValueError that aborted parsing of the entire robots.txt file, even though every other malformed directive is silently ignored (Allow and Disallow already do try/except ValueError: pass, and invalid Crawl-delay: pears / Request-rate: 9/banana values are already dropped, with existing tests).

The Crawl-delay and Request-rate guards now use str.isdecimal(), the predicate for the decimal digits int() accepts, so a value written with non-decimal digits is ignored instead of raising.

Scope note: a control character that str.strip() removes but int() rejects (for example a byte in U+001C..U+001F inside a Request-rate token) still raises, because the Request-rate branch converts the unstripped token. That is a separate, pre-existing divergence, left out of this single-purpose change.

…wl-delay and Request-rate

str.isdigit() returns True for non-decimal Unicode digits such as
U+00B2 SUPERSCRIPT TWO, which int() then rejects with ValueError. In
RobotFileParser.parse() this raised a raw ValueError that aborted parsing
of the entire robots.txt file, unlike every other malformed directive
which is silently ignored. Guard the Crawl-delay and Request-rate
conversions with str.isdecimal() so a value written with non-decimal
digits is ignored instead of raising.
Comment thread Lib/test/test_robotparser.py Outdated
Crawl-delay: ²
"""
good = ['/foo.html']
bad = []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can consolidate test classes.
Add crawl_delay and request_rate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Consolidated into one class with both directives.

Comment thread Lib/test/test_robotparser.py Outdated
robots_txt = """\
User-agent: *
Disallow: /tmp/
Request-rate: ²/5

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add also line with decimal numerator and non-decimal denominator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Request-rate: 5/² case.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add explicit crawl_delay = None and request_rate = None.

Please modify test_request_rate so that it asserts request_rate even if it is None.

@tonghuaroot

Copy link
Copy Markdown
Contributor Author

Done. Added explicit crawl_delay = None and request_rate = None, and assertIsNone in the else branch of test_request_rate.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 17, 2026
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) July 17, 2026 05:31
@serhiy-storchaka
serhiy-storchaka merged commit 597ec2d into python:main Jul 17, 2026
103 of 105 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app

bedevere-app Bot commented Jul 17, 2026

Copy link
Copy Markdown

GH-153830 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 17, 2026
@bedevere-app

bedevere-app Bot commented Jul 17, 2026

Copy link
Copy Markdown

GH-153831 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 17, 2026
@bedevere-app

bedevere-app Bot commented Jul 17, 2026

Copy link
Copy Markdown

GH-153832 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jul 17, 2026
serhiy-storchaka pushed a commit that referenced this pull request Jul 17, 2026
…awl-delay and Request-rate (GH-153405) (GH-153832)

str.isdigit() returned True for non-decimal Unicode digits such as
U+00B2 SUPERSCRIPT TWO, which int() then rejected with ValueError.
(cherry picked from commit 597ec2d)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Jul 17, 2026
…awl-delay and Request-rate (GH-153405) (GH-153831)

str.isdigit() returned True for non-decimal Unicode digits such as
U+00B2 SUPERSCRIPT TWO, which int() then rejected with ValueError.
(cherry picked from commit 597ec2d)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants