From 7b3e75f73186381a9ec1e7de64e8389aae2e3435 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Thu, 11 Dec 2025 08:44:19 -0500 Subject: [4.2.x] Refs #36499 -- Adjusted test_strip_tags following Python behavior change for incomplete entities. Backport of 7b80b2186300620931009fd62c2969f108fe7a62 from main. --- tests/utils_tests/test_html.py | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py index f755b8cebc..c1786a9799 100644 --- a/tests/utils_tests/test_html.py +++ b/tests/utils_tests/test_html.py @@ -1,3 +1,4 @@ +import math import os import sys from datetime import datetime @@ -92,7 +93,7 @@ class TestUtilsHtml(SimpleTestCase): # old and new results. The check below is temporary until all supported # Python versions and CI workers include the fix. See: # https://github.com/python/cpython/commit/6eb6c5db - min_fixed = { + min_fixed_security = { (3, 14): (3, 14), (3, 13): (3, 13, 6), (3, 12): (3, 12, 12), @@ -100,9 +101,27 @@ class TestUtilsHtml(SimpleTestCase): (3, 10): (3, 10, 19), (3, 9): (3, 9, 24), } - py_version = sys.version_info[:2] - htmlparser_fixed = ( - py_version in min_fixed and sys.version_info >= min_fixed[py_version] + htmlparser_fixed_security = ( + sys.version_info >= min_fixed_security[sys.version_info[:2]] + ) + # Similarly, there was a fix for terminating incomplete entities. See: + # https://github.com/python/cpython/commit/95296a9d + min_fixed_incomplete_entities = { + (3, 14): (3, 14, 1), + (3, 13): (3, 13, 10), + # Not fixed in the following versions. + (3, 12): (3, 12, math.inf), + (3, 11): (3, 11, math.inf), + (3, 10): (3, 10, math.inf), + (3, 9): (3, 9, math.inf), + } + major_version = sys.version_info[:2] + htmlparser_fixed_security = sys.version_info >= min_fixed_security.get( + major_version, major_version + ) + htmlparser_fixed_incomplete_entities = ( + sys.version_info + >= min_fixed_incomplete_entities.get(major_version, major_version) ) items = ( ( @@ -130,16 +149,19 @@ class TestUtilsHtml(SimpleTestCase): # https://bugs.python.org/issue20288 ("&gotcha&#;<>", "&gotcha&#;<>"), ("ript>test</script>", "ript>test"), - ("&h", "alert()h"), + ( + "&h", + "alert()&h;" if htmlparser_fixed_incomplete_entities else "alert()h", + ), ( ">" if htmlparser_fixed else ">" if htmlparser_fixed_security else ">br>br>br>X", "XX"), ("<" * 50 + "a>" * 50, ""), ( ">" + "" if htmlparser_fixed else ">" + "" if htmlparser_fixed_security else ">" + "