[3.10] gh-93671: Avoid exponential backtracking in deeply nested sequence patterns in match statements (GH-93680) (#93690)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>.
(cherry picked from commit 53a8b17895)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
Pablo Galindo Salgado 2022-06-10 19:34:15 +01:00 committed by GitHub
parent 9041b00283
commit 8f36c735b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 2 deletions

View file

@ -3138,6 +3138,27 @@ class TestTracing(unittest.TestCase):
self.assertListEqual(self._trace(f, "go x"), [1, 2, 3])
self.assertListEqual(self._trace(f, "spam"), [1, 2, 3])
def test_parser_deeply_nested_patterns(self):
# Deeply nested patterns can cause exponential backtracking when parsing.
# See gh-93671 for more information.
levels = 100
patterns = [
"A" + "(" * levels + ")" * levels,
"{1:" * levels + "1" + "}" * levels,
"[" * levels + "1" + "]" * levels,
]
for pattern in patterns:
with self.subTest(pattern):
code = inspect.cleandoc("""
match None:
case {}:
pass
""".format(pattern))
compile(code, "<string>", "exec")
if __name__ == "__main__":
"""