GeezerVison is showing github.com — bpo-46707: Avoid potential exponential backtracking in some syntax er… · python/cpython@b71dc71
File tree
Expand file treeCollapse file tree
Open diff view settings
Misc/NEWS.d/next/Core and Builtins
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Grammar/python.gram +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1095,13 +1095,15 @@ invalid_expression: |
1095 | 1095 | RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") } |
1096 | 1096 | | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") } |
1097 | 1097 |
|
| 1098 | +invalid_left_assignment_prefixes(memo): list|tuple|genexp|'True'|'None'|'False' |
| 1099 | + |
1098 | 1100 | invalid_named_expression: |
1099 | 1101 | | a=expression ':=' expression { |
1100 | 1102 | RAISE_SYNTAX_ERROR_KNOWN_LOCATION( |
1101 | 1103 | a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) } |
1102 | 1104 | | a=NAME '=' b=bitwise_or !('='|':=') { |
1103 | 1105 | p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") } |
1104 | | - | !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':=') { |
| 1106 | + | !invalid_left_assignment_prefixes a=bitwise_or b='=' bitwise_or !('='|':=') { |
1105 | 1107 | p->in_raw_rule ? NULL : RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?", |
1106 | 1108 | _PyPegen_get_expr_name(a)) } |
1107 | 1109 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: Lib/test/test_syntax.py +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1770,6 +1770,14 @@ def test_error_on_parser_stack_overflow(self): |
1770 | 1770 | with self.assertRaises(MemoryError): |
1771 | 1771 | compile(source, "<string>", mode) |
1772 | 1772 |
|
| 1773 | + @support.cpython_only |
| 1774 | + def test_deep_invalid_rule(self): |
| 1775 | + # Check that a very deep invalid rule in the PEG |
| 1776 | + # parser doesn't have exponential backtracking. |
| 1777 | + source = "d{{{{{{{{{{{{{{{{{{{{{{{{{```{{{{{{{ef f():y" |
| 1778 | + with self.assertRaises(SyntaxError): |
| 1779 | + compile(source, "<string>", "exec") |
| 1780 | + |
1773 | 1781 |
|
1774 | 1782 | def load_tests(loader, tests, pattern): |
1775 | 1783 | tests.addTest(doctest.DocTestSuite()) |
|
Collapse file
Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +Avoid potential exponential backtracking when producing some syntax errors |
| 2 | +involving lots of brackets. Patch by Pablo Galindo. |