[3.14] gh-138349: Fix crash when combining module-level annotation and listcomp (#138363) (#138749)

* gh-138349: Fix crash when combining module-level annotation and listcomp (#138363)

(cherry picked from commit 7a6fd4a45d)

---------

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Victor Stinner 2025-09-11 07:43:57 +02:00 committed by GitHub
parent 38cd031111
commit 3643a26a01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 48 additions and 6 deletions

View file

@ -5500,10 +5500,12 @@ codegen_annassign(compiler *c, stmt_ty s)
RETURN_IF_ERROR(_PyCompile_AddDeferredAnnotation(
c, s, &conditional_annotation_index));
if (conditional_annotation_index != NULL) {
ADDOP_NAME(
c, loc,
SCOPE_TYPE(c) == COMPILE_SCOPE_CLASS ? LOAD_DEREF : LOAD_NAME,
&_Py_ID(__conditional_annotations__), cellvars);
if (SCOPE_TYPE(c) == COMPILE_SCOPE_CLASS) {
ADDOP_NAME(c, loc, LOAD_DEREF, &_Py_ID(__conditional_annotations__), cellvars);
}
else {
ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__conditional_annotations__), names);
}
ADDOP_LOAD_CONST_NEW(c, loc, conditional_annotation_index);
ADDOP_I(c, loc, SET_ADD, 1);
ADDOP(c, loc, POP_TOP);