Fixed #32499 -- Escaped additional metacharacters in simplify_regex().

This commit is contained in:
Nick Pope 2021-07-08 00:19:35 +01:00 committed by Mariusz Felisiak
parent 85540da930
commit 448d974d96
2 changed files with 44 additions and 1 deletions

View File

@ -145,7 +145,7 @@ unnamed_group_matcher = _lazy_re_compile(r'\(')
def replace_metacharacters(pattern):
"""Remove unescaped metacharacters from the pattern."""
return re.sub(
r'((?:^|(?<!\\))(?:\\\\)*)(\\?)([?^$])',
r'((?:^|(?<!\\))(?:\\\\)*)(\\?)([?*+^$]|\\[bBAZ])',
lambda m: m[1] + m[3] if m[2] else m[1],
pattern,
)

View File

@ -407,8 +407,28 @@ class AdminDocViewFunctionsTests(SimpleTestCase):
(r'a?', '/a'),
(r'a??', '/a'),
(r'a???', '/a'),
(r'a*', '/a'),
(r'a**', '/a'),
(r'a***', '/a'),
(r'a+', '/a'),
(r'a++', '/a'),
(r'a+++', '/a'),
(r'\Aa', '/a'),
(r'\A\Aa', '/a'),
(r'\A\A\Aa', '/a'),
(r'a\Z', '/a'),
(r'a\Z\Z', '/a'),
(r'a\Z\Z\Z', '/a'),
(r'\ba', '/a'),
(r'\b\ba', '/a'),
(r'\b\b\ba', '/a'),
(r'a\B', '/a'),
(r'a\B\B', '/a'),
(r'a\B\B\B', '/a'),
# Multiple mixed metacharacters.
(r'^a/?$', '/a/'),
(r'\Aa\Z', '/a'),
(r'\ba\B', '/a'),
# Escaped single metacharacters.
(r'\^a', r'/^a'),
(r'\\^a', r'/\\a'),
@ -425,6 +445,26 @@ class AdminDocViewFunctionsTests(SimpleTestCase):
(r'a\\\?', r'/a\\?'),
(r'a\\\\?', r'/a\\\\'),
(r'a\\\\\?', r'/a\\\\?'),
(r'a\*', r'/a*'),
(r'a\\*', r'/a\\'),
(r'a\\\*', r'/a\\*'),
(r'a\\\\*', r'/a\\\\'),
(r'a\\\\\*', r'/a\\\\*'),
(r'a\+', r'/a+'),
(r'a\\+', r'/a\\'),
(r'a\\\+', r'/a\\+'),
(r'a\\\\+', r'/a\\\\'),
(r'a\\\\\+', r'/a\\\\+'),
(r'\\Aa', r'/\Aa'),
(r'\\\Aa', r'/\\a'),
(r'\\\\Aa', r'/\\\Aa'),
(r'\\\\\Aa', r'/\\\\a'),
(r'\\\\\\Aa', r'/\\\\\Aa'),
(r'a\\Z', r'/a\Z'),
(r'a\\\Z', r'/a\\'),
(r'a\\\\Z', r'/a\\\Z'),
(r'a\\\\\Z', r'/a\\\\'),
(r'a\\\\\\Z', r'/a\\\\\Z'),
# Escaped mixed metacharacters.
(r'^a\?$', r'/a?'),
(r'^a\\?$', r'/a\\'),
@ -437,6 +477,9 @@ class AdminDocViewFunctionsTests(SimpleTestCase):
(r'^a\\\?\\\$', r'/a\\?\\$'),
(r'^a\\\\?\\\\$', r'/a\\\\\\\\'),
(r'^a\\\\\?\\\\\$', r'/a\\\\?\\\\$'),
# Complex examples with metacharacters and (un)named groups.
(r'^\b(?P<slug>\w+)\B/(\w+)?', '/<slug>/<var>'),
(r'^\A(?P<slug>\w+)\Z', '/<slug>'),
)
for pattern, output in tests:
with self.subTest(pattern=pattern):