gh-132479: Fix crash with multiple comprehensions in annotations (#132778)

This commit is contained in:
Jelle Zijlstra 2025-04-21 13:49:59 -07:00 committed by GitHub
parent 08e331d05e
commit 01317bb449
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 72 additions and 6 deletions

View file

@ -1394,7 +1394,7 @@ symtable_exit_block(struct symtable *st)
}
static int
symtable_enter_existing_block(struct symtable *st, PySTEntryObject* ste)
symtable_enter_existing_block(struct symtable *st, PySTEntryObject* ste, bool add_to_children)
{
if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
return 0;
@ -1425,7 +1425,7 @@ symtable_enter_existing_block(struct symtable *st, PySTEntryObject* ste)
if (ste->ste_type == ModuleBlock)
st->st_global = st->st_cur->ste_symbols;
if (prev) {
if (add_to_children && prev) {
if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
return 0;
}
@ -1440,7 +1440,7 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
PySTEntryObject *ste = ste_new(st, name, block, ast, loc);
if (ste == NULL)
return 0;
int result = symtable_enter_existing_block(st, ste);
int result = symtable_enter_existing_block(st, ste, /* add_to_children */true);
Py_DECREF(ste);
if (block == AnnotationBlock || block == TypeVariableBlock || block == TypeAliasBlock) {
_Py_DECLARE_STR(format, ".format");
@ -1866,7 +1866,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
Py_DECREF(new_ste);
return 0;
}
if (!symtable_enter_existing_block(st, new_ste)) {
if (!symtable_enter_existing_block(st, new_ste, /* add_to_children */true)) {
Py_DECREF(new_ste);
return 0;
}
@ -2223,7 +2223,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
Py_DECREF(new_ste);
return 0;
}
if (!symtable_enter_existing_block(st, new_ste)) {
if (!symtable_enter_existing_block(st, new_ste, /* add_to_children */true)) {
Py_DECREF(new_ste);
return 0;
}
@ -2776,7 +2776,8 @@ symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key)
}
}
else {
if (!symtable_enter_existing_block(st, parent_ste->ste_annotation_block)) {
if (!symtable_enter_existing_block(st, parent_ste->ste_annotation_block,
/* add_to_children */false)) {
return 0;
}
}