diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2022-06-10 19:34:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-10 19:34:15 +0100 |
commit | 8f36c735b2aa694b140a19d9425bbdf54de5f212 (patch) | |
tree | a9b4e898c1f4e20cc5f68ee9b25a480112efcfbd /Lib/test/test_patma.py | |
parent | 9041b00283737f77acbb392871886913d82144f8 (diff) | |
download | cpython-git-8f36c735b2aa694b140a19d9425bbdf54de5f212.tar.gz |
[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 53a8b17895e91d08f76a2fb59a555d012cd85ab4)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Diffstat (limited to 'Lib/test/test_patma.py')
-rw-r--r-- | Lib/test/test_patma.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index aa18e29e22..b8444822c8 100644 --- a/Lib/test/test_patma.py +++ b/Lib/test/test_patma.py @@ -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__": """ |