Skip to content

Commit 098880d

Browse files
[3.14] gh-149122: Fix segfault in compiler when certain builtin functions are passed a coroutine as arg (GH-149138) (#149151)
gh-149122: Fix segfault in compiler when certain builtin functions are passed a coroutine as arg (GH-149138) (cherry picked from commit 16f292e) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
1 parent 4d0ae4c commit 098880d

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

Lib/test/test_builtin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,27 @@ def f_tuple():
293293
self.assertEqual(overridden_outputs, ['all', 'any', 'tuple'])
294294

295295

296+
def test_builtin_call_async_genexpr_no_crash(self):
297+
async def f_all():
298+
return all(await 2 for _ in [])
299+
300+
async def f_any():
301+
return any(await 2 for _ in [])
302+
303+
async def f_tuple():
304+
return tuple(await 2 for _ in [])
305+
306+
async def f_list():
307+
return list(await 2 for _ in [])
308+
309+
async def f_set():
310+
return set(await 2 for _ in [])
311+
312+
for f in (f_all, f_any, f_tuple, f_list, f_set):
313+
with self.subTest(func=f.__name__):
314+
with self.assertRaises(TypeError):
315+
run_yielding_async_fn(f)
316+
296317
def test_ascii(self):
297318
self.assertEqual(ascii(''), '\'\'')
298319
self.assertEqual(ascii(0), '0')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a crash in optimized calls to :func:`all`, :func:`any`, :func:`tuple`,
2+
:func:`list`, and :func:`set` with an async generator expression argument
3+
(for example, ``tuple(await x for x in y)``). These calls now correctly raise
4+
``TypeError`` instead of crashing.

Python/codegen.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3861,6 +3861,12 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end)
38613861
return 0;
38623862
}
38633863

3864+
expr_ty generator_exp = asdl_seq_GET(args, 0);
3865+
PySTEntryObject *generator_entry = _PySymtable_Lookup(SYMTABLE(c), (void *)generator_exp);
3866+
if (generator_entry->ste_coroutine) {
3867+
return 0;
3868+
}
3869+
38643870
location loc = LOC(func);
38653871

38663872
int optimized = 0;
@@ -3892,7 +3898,6 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end)
38923898
if (const_oparg == CONSTANT_BUILTIN_TUPLE) {
38933899
ADDOP_I(c, loc, BUILD_LIST, 0);
38943900
}
3895-
expr_ty generator_exp = asdl_seq_GET(args, 0);
38963901
VISIT(c, expr, generator_exp);
38973902

38983903
NEW_JUMP_TARGET_LABEL(c, loop);

0 commit comments

Comments
 (0)