Skip to content

Commit 06b2a40

Browse files
committed
Make pegen output error detection checks after loop body for '*' rules.
Currently, the pegen output produces this for '+' rules: if (_n == 0 || p->error_indicator) { PyMem_Free(_children); p->level--; return NULL; } But for '*' rules, no check is done at all, (skipping the _n == 0 makes sesnse of course) For '*' it's possible for an error to be raised when parsing children, and currently the parser sets the error flag/exception but does not return NULL, so parent rules might not detect the error appropriately, as in this code: if ( (a = _PyPegen_expect_token(p, FSTRING_START)) // token='FSTRING_START' && (b = _loop0_79_rule(p)) // fstring_middle* && (c = _PyPegen_expect_token(p, FSTRING_END)) // token='FSTRING_END' ) In this example, _PyPegen_expect_token can raise a SyntaxWarning which trips a debug assert. This change outputs the same p->error_indicator check for '*' rules as for '+' rules.
1 parent 1d19650 commit 06b2a40

4 files changed

Lines changed: 241 additions & 4 deletions

File tree

‎Lib/test/test_fstring.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,19 @@ def test_concat_decode_failure_does_not_crash(self):
604604
'''
605605
assert_python_ok('-c', script)
606606

607+
def test_memoryerror_during_concat_does_not_crash(self):
608+
# gh-153182: Recursion/Memory errors parsing f-strings should be properly unwound
609+
# and the parser should not attempt to continue parsing after the error. (In this case,
610+
# a SyntaxWarning will subsequently be raised for the invalid escape, triggering a debug assert
611+
# from the existing Exception)
612+
script = r'''
613+
try:
614+
compile('(x""f"\\{0}"<' * 1276, "<test>", "exec")
615+
except MemoryError:
616+
pass
617+
'''
618+
assert_python_ok('-c', script)
619+
607620
def test_literal(self):
608621
self.assertEqual(f'', '')
609622
self.assertEqual(f'a', 'a')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an issue where some grammar rules tried to continue parsing after an
2+
error

0 commit comments

Comments
 (0)