From 19250b774ed6f26889813e12cbc3262eaf62ac8d Mon Sep 17 00:00:00 2001 From: vadimtoptunov Date: Thu, 19 Nov 2026 19:55:41 +0200 Subject: [PATCH 1/2] 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. --- Lib/test/test_robotparser.py | 18 ++++++++++++++++++ Lib/urllib/robotparser.py | 4 ++++ ...6-11-19-17-55-31.gh-issue-162282.1WW-xb.rst | 1 + 3 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-19-17-55-31.gh-issue-162282.1WW-xb.rst diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py index 9693cfc259286d..6bab9b6719e4f8 100644 --- a/Lib/test/test_robotparser.py +++ b/Lib/test/test_robotparser.py @@ -875,5 +875,23 @@ def test_read_404(self): self.assertIsNone(parser.crawl_delay('*')) self.assertIsNone(parser.request_rate('*')) + +class RepeatedUserAgentPerformanceTest(unittest.TestCase): + """Parsing many identical ``User-agent:`` lines must stay linear.""" + + def test_many_duplicate_user_agents(self): + import time + body = ("User-agent: evil\n" * 50_000) + "Disallow: /\n" + parser = urllib.robotparser.RobotFileParser() + parser.modified() + start = time.perf_counter() + parser.parse(body.splitlines()) + elapsed = time.perf_counter() - start + self.assertLess(elapsed, 5.0, + f"parse of 50_000 duplicate User-agent lines " + f"took {elapsed:.3f}s") + self.assertFalse(parser.can_fetch("evil", "/")) + + if __name__=='__main__': unittest.main() diff --git a/Lib/urllib/robotparser.py b/Lib/urllib/robotparser.py index 985333c7100438..844e35c5a5b20c 100644 --- a/Lib/urllib/robotparser.py +++ b/Lib/urllib/robotparser.py @@ -90,8 +90,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 00000000000000..0d72db13e0c2be --- /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. From 3fd9f49250a1b3eb480364f792b0a7d5e4975146 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 22 Nov 2026 16:40:43 +0100 Subject: [PATCH 2/2] Update Lib/test/test_robotparser.py --- Lib/test/test_robotparser.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py index 6bab9b6719e4f8..10b1d5e9bd5386 100644 --- a/Lib/test/test_robotparser.py +++ b/Lib/test/test_robotparser.py @@ -876,20 +876,17 @@ def test_read_404(self): self.assertIsNone(parser.request_rate('*')) -class RepeatedUserAgentPerformanceTest(unittest.TestCase): - """Parsing many identical ``User-agent:`` lines must stay linear.""" +class RepeatedUserAgentTest(unittest.TestCase): def test_many_duplicate_user_agents(self): - import time - body = ("User-agent: evil\n" * 50_000) + "Disallow: /\n" + # 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.modified() - start = time.perf_counter() parser.parse(body.splitlines()) - elapsed = time.perf_counter() - start - self.assertLess(elapsed, 5.0, - f"parse of 50_000 duplicate User-agent lines " - f"took {elapsed:.3f}s") + entry = parser.groups["evil"] + self.assertIs(entry, parser.entries[0]) + self.assertEqual(len(entry.rulelines), 1) self.assertFalse(parser.can_fetch("evil", "/"))