Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-138349: Fix crash when combining module-level annotation and listcomp
  • Loading branch information
JelleZijlstra committed Sep 1, 2025
commit fe37499b481b29608c7ab18a1e7b321426fa4162
37 changes: 37 additions & 0 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,40 @@ def test_complex_comprehension_inlining_exec(self):
genexp = annos["unique_name_2"][0]
lamb = list(genexp)[0]
self.assertEqual(lamb(), 42)

# gh-138349
def test_module_level_annotation_plus_listcomp(self):
cases = [
"""
def report_error():
pass
try:
[0 for name_2 in unique_name_0 if (lambda: name_2)]
except:
pass
annotated_name: 0
""",
"""
class Generic:
pass
try:
[0 for name_2 in unique_name_0 if (0 for unique_name_1 in unique_name_2 for unique_name_3 in name_2)]
except:
pass
annotated_name: 0
""",
"""
class Generic:
pass
annotated_name: 0
try:
[0 for name_2 in [[0]] for unique_name_1 in unique_name_2 if (lambda: name_2)]
except:
pass
""",
]
for code in cases:
with self.subTest(code=code):
mod = build_module(code)
annos = mod.__annotations__
self.assertEqual(annos, {"annotated_name": 0})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in certain cases where a module contains both a module-level
annotation and a comprehension.
1 change: 1 addition & 0 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ set_add_entry_takeref(PySetObject *so, PyObject *key, Py_hash_t hash)
size_t i; /* Unsigned for defined overflow behavior */
int probes;
int cmp;
assert(PySet_CheckExact(so) || PyFrozenSet_CheckExact(so));

restart:

Expand Down
10 changes: 6 additions & 4 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -5492,10 +5492,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);
Expand Down
Loading