https://github.com/python/cpython/commit/047d65cc518b5004db22bc79a3faf47708589d19 commit: 047d65cc518b5004db22bc79a3faf47708589d19 branch: 3.15 author: Miss Islington (bot) <[email protected]> committer: asvetlov <[email protected]> date: 2026-11-22T16:53:41Z summary:
[3.15] gh-162282: Fix quadratic cost in urllib.robotparser._add_entry (GH-162283) (#162446) gh-162282: Fix quadratic cost in urllib.robotparser._add_entry (GH-162283) * gh-162282: Fix quadratic cost in urllib.robotparser._add_entry Parsing a robots.txt with many identical User-agent: lines in a single group ran in O(N^2) time: _add_entry iterated entry.useragents without deduplication, and from the second duplicate on merge_entries was called once per iteration; merge_entries is itself O(N). Track the agents already registered during the current call and skip duplicates. For every input shape other than 'same agent repeated within one entry' the behavior is identical. On 10,000 duplicate User-agent lines the parse time drops from ~2.5 s to ~0.003 s. A regression test parses 50,000 duplicate User-agent lines and requires completion in under 5 seconds. * Update Lib/test/test_robotparser.py --------- (cherry picked from commit 45e9b3db69f4f58a74911ea4b699f254b5edc036) Co-authored-by: Vadim Toptunov <[email protected]> Co-authored-by: Andrew Svetlov <[email protected]> files: A Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst M Lib/test/test_robotparser.py M Lib/urllib/robotparser.py diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py index 1ec64da064d52b7..2660820b4c7484e 100644 --- a/Lib/test/test_robotparser.py +++ b/Lib/test/test_robotparser.py @@ -844,5 +844,20 @@ def test_read_404(self): self.assertIsNone(parser.crawl_delay('*')) self.assertIsNone(parser.request_rate('*')) + +class RepeatedUserAgentTest(unittest.TestCase): + + def test_many_duplicate_user_agents(self): + # gh-162282: a User-agent repeated within one group must not be + # merged into that group once per repetition (quadratic time). + body = "User-agent: evil\n" * 1000 + "Disallow: /\n" + parser = urllib.robotparser.RobotFileParser() + parser.parse(body.splitlines()) + entry = parser.groups["evil"] + self.assertIs(entry, parser.entries[0]) + self.assertEqual(len(entry.rulelines), 1) + self.assertFalse(parser.can_fetch("evil", "/")) + + if __name__=='__main__': unittest.main() diff --git a/Lib/urllib/robotparser.py b/Lib/urllib/robotparser.py index d267ed00345c490..9cb97d5f54a0665 100644 --- a/Lib/urllib/robotparser.py +++ b/Lib/urllib/robotparser.py @@ -83,8 +83,12 @@ def read(self): def _add_entry(self, entry): self.entries.append(entry) + seen = set() for agent in entry.useragents: agent = agent.lower() + if agent in seen: + continue + seen.add(agent) if agent not in self.groups: self.groups[agent] = entry else: diff --git a/Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst b/Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst new file mode 100644 index 000000000000000..0d72db13e0c2bec --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst @@ -0,0 +1 @@ +Fix quadratic-time cost in :meth:`urllib.robotparser.RobotFileParser.parse` when a single group contains many identical ``User-agent:`` lines. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
