Skip to content

Commit c3f9871

Browse files
committed
[4.2.x] Fixed #36499 -- Adjusted utils_tests.test_html.TestUtilsHtml.test_strip_tags following Python's HTMLParser new behavior.
Python fixed a quadratic complexity processing for HTMLParser in: python/cpython@6eb6c5db. Backport of 2980627 from main.
1 parent 2a79837 commit c3f9871

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

tests/utils_tests/test_html.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from datetime import datetime
34

45
from django.core.exceptions import SuspiciousOperation
@@ -85,6 +86,24 @@ def test_linebreaks(self):
8586
self.check_output(linebreaks, lazystr(value), output)
8687

8788
def test_strip_tags(self):
89+
# Python fixed a quadratic-time issue in HTMLParser in 3.13.6, 3.12.12,
90+
# 3.11.14, 3.10.19, and 3.9.24. The fix slightly changes HTMLParser's
91+
# output, so tests for particularly malformed input must handle both
92+
# old and new results. The check below is temporary until all supported
93+
# Python versions and CI workers include the fix. See:
94+
# https://github.com/python/cpython/commit/6eb6c5db
95+
min_fixed = {
96+
(3, 14): (3, 14),
97+
(3, 13): (3, 13, 6),
98+
(3, 12): (3, 12, 12),
99+
(3, 11): (3, 11, 14),
100+
(3, 10): (3, 10, 19),
101+
(3, 9): (3, 9, 24),
102+
}
103+
py_version = sys.version_info[:2]
104+
htmlparser_fixed = (
105+
py_version in min_fixed and sys.version_info >= min_fixed[py_version]
106+
)
88107
items = (
89108
(
90109
"<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>",
@@ -112,10 +131,16 @@ def test_strip_tags(self):
112131
("&gotcha&#;<>", "&gotcha&#;<>"),
113132
("<sc<!-- -->ript>test<<!-- -->/script>", "ript>test"),
114133
("<script>alert()</script>&h", "alert()h"),
115-
("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
134+
(
135+
"><!" + ("&" * 16000) + "D",
136+
">" if htmlparser_fixed else "><!" + ("&" * 16000) + "D",
137+
),
116138
("X<<<<br>br>br>br>X", "XX"),
117139
("<" * 50 + "a>" * 50, ""),
118-
(">" + "<a" * 500 + "a", ">" + "<a" * 500 + "a"),
140+
(
141+
">" + "<a" * 500 + "a",
142+
">" if htmlparser_fixed else ">" + "<a" * 500 + "a",
143+
),
119144
("<a" * 49 + "a" * 951, "<a" * 49 + "a" * 951),
120145
("<" + "a" * 1_002, "<" + "a" * 1_002),
121146
)

0 commit comments

Comments
 (0)