mirror of
https://github.com/python/cpython.git
synced 2025-08-19 00:00:48 +00:00
[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:
parent
9041b00283
commit
8f36c735b2
4 changed files with 37 additions and 2 deletions
|
@ -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__":
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue