gh-151238: Check for _get_resized_exprs failure in _PyPegen_{joined,template}_str (#151259)

This commit is contained in:
Stan Ulbrych 2026-06-11 15:26:46 +01:00 committed by GitHub
parent 0066fd73a2
commit 4b44b1e1fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 0 deletions

View file

@ -593,6 +593,17 @@ def test_compile_time_concat_errors(self):
r"""b'' f''""",
])
def test_concat_decode_failure_does_not_crash(self):
script = r'''
import builtins
builtins.__import__ = builtins # Breaks warning machinery so _get_resized_exprs returns NULL
try:
compile('"x"f"\]"b""', '<test>', 'exec')
except Exception:
pass
'''
assert_python_ok('-c', script)
def test_literal(self):
self.assertEqual(f'', '')
self.assertEqual(f'a', 'a')

View file

@ -0,0 +1,2 @@
Fix a crash when compiling a concatenated f-string or t-string if an error
occurs when processing one of it's parts.

View file

@ -1416,6 +1416,9 @@ expr_ty
_PyPegen_template_str(Parser *p, Token *a, asdl_expr_seq *raw_expressions, Token *b) {
asdl_expr_seq *resized_exprs = _get_resized_exprs(p, a, raw_expressions, b, TSTRING);
if (resized_exprs == NULL) {
return NULL;
}
return _PyAST_TemplateStr(resized_exprs, a->lineno, a->col_offset,
b->end_lineno, b->end_col_offset,
p->arena);
@ -1425,6 +1428,9 @@ expr_ty
_PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b) {
asdl_expr_seq *resized_exprs = _get_resized_exprs(p, a, raw_expressions, b, FSTRING);
if (resized_exprs == NULL) {
return NULL;
}
return _PyAST_JoinedStr(resized_exprs, a->lineno, a->col_offset,
b->end_lineno, b->end_col_offset,
p->arena);