Skip to content

Commit 2980627

Browse files
committed
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.
1 parent e4515da commit 2980627

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

tests/utils_tests/test_html.py

Lines changed: 24 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
@@ -115,6 +116,21 @@ def test_linebreaks(self):
115116
self.check_output(linebreaks, lazystr(value), output)
116117

117118
def test_strip_tags(self):
119+
# Python fixed a quadratic-time issue in HTMLParser in 3.13.6, 3.12.12,
120+
# 3.11.14, 3.10.19, and 3.9.24. The fix slightly changes HTMLParser's
121+
# output, so tests for particularly malformed input must handle both
122+
# old and new results. The check below is temporary until all supported
123+
# Python versions and CI workers include the fix. See:
124+
# https://github.com/python/cpython/commit/6eb6c5db
125+
min_fixed = {
126+
(3, 14): (3, 14),
127+
(3, 13): (3, 13, 6),
128+
(3, 12): (3, 12, 12),
129+
(3, 11): (3, 11, 14),
130+
(3, 10): (3, 10, 19),
131+
(3, 9): (3, 9, 24),
132+
}
133+
htmlparser_fixed = sys.version_info >= min_fixed[sys.version_info[:2]]
118134
items = (
119135
(
120136
"<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>",
@@ -142,10 +158,16 @@ def test_strip_tags(self):
142158
("&gotcha&#;<>", "&gotcha&#;<>"),
143159
("<sc<!-- -->ript>test<<!-- -->/script>", "ript>test"),
144160
("<script>alert()</script>&h", "alert()h"),
145-
("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
161+
(
162+
"><!" + ("&" * 16000) + "D",
163+
">" if htmlparser_fixed else "><!" + ("&" * 16000) + "D",
164+
),
146165
("X<<<<br>br>br>br>X", "XX"),
147166
("<" * 50 + "a>" * 50, ""),
148-
(">" + "<a" * 500 + "a", ">" + "<a" * 500 + "a"),
167+
(
168+
">" + "<a" * 500 + "a",
169+
">" if htmlparser_fixed else ">" + "<a" * 500 + "a",
170+
),
149171
("<a" * 49 + "a" * 951, "<a" * 49 + "a" * 951),
150172
("<" + "a" * 1_002, "<" + "a" * 1_002),
151173
)

0 commit comments

Comments
 (0)