Skip to content

Commit 5820ce8

Browse files
committed
fix codegen in annotations
1 parent a297926 commit 5820ce8

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Lib/test/test_pdb.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,21 +3818,21 @@ def bar():
38183818

38193819
def test_issue135700(self):
38203820
# See gh-135700
3821-
module_code = """\
3821+
module_code = textwrap.dedent("""\
38223822
22
38233823
38243824
class ClassVar:
38253825
pass
38263826
__dataclass_fields__: ClassVar
3827-
"""
3827+
""")
38283828
with open("testmod.py", "w") as f:
38293829
f.write(module_code)
38303830
self.addCleanup(os_helper.unlink, "testmod.py")
38313831

3832-
script = """
3832+
script = textwrap.dedent("""
38333833
import testmod
38343834
print(testmod.__annotations__)
3835-
"""
3835+
""")
38363836
commands = """
38373837
b testmod.py:1
38383838
c

Python/codegen.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ codegen_deferred_annotations_body(compiler *c, location loc,
788788
ADDOP_I(c, LOC(st), COPY, 2);
789789
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled);
790790
// stack now contains <annos> <name> <annos> <value>
791-
ADDOP(c, loc, STORE_SUBSCR);
791+
ADDOP(c, LOC(st), STORE_SUBSCR);
792792
// stack now contains <annos>
793793

794794
USE_LABEL(c, not_set);
@@ -823,7 +823,14 @@ codegen_process_deferred_annotations(compiler *c, location loc)
823823
PySTEntryObject *ste = SYMTABLE_ENTRY(c);
824824
assert(ste->ste_annotation_block != NULL);
825825
void *key = (void *)((uintptr_t)ste->ste_id + 1);
826-
if (codegen_setup_annotations_scope(c, loc, key,
826+
827+
// Get the first annotation location
828+
PyObject* ptr = PyList_GET_ITEM(deferred_anno, 0);
829+
stmt_ty st = (stmt_ty)PyLong_AsVoidPtr(ptr);
830+
if (st == NULL) {
831+
goto error;
832+
}
833+
if (codegen_setup_annotations_scope(c, LOC(st), key,
827834
ste->ste_annotation_block->ste_name) < 0) {
828835
goto error;
829836
}
@@ -836,12 +843,11 @@ codegen_process_deferred_annotations(compiler *c, location loc)
836843
Py_DECREF(deferred_anno);
837844
Py_DECREF(conditional_annotation_indices);
838845

839-
RETURN_IF_ERROR(codegen_leave_annotations_scope(c, loc));
846+
RETURN_IF_ERROR(codegen_leave_annotations_scope(c, LOC(st)));
840847
RETURN_IF_ERROR(codegen_nameop(
841-
c, loc,
848+
c, LOC(st),
842849
ste->ste_type == ClassBlock ? &_Py_ID(__annotate_func__) : &_Py_ID(__annotate__),
843850
Store));
844-
845851
if (need_separate_block) {
846852
RETURN_IF_ERROR(_PyCompile_EndAnnotationSetup(c));
847853
}
@@ -868,8 +874,8 @@ int
868874
_PyCodegen_Module(compiler *c, location loc, asdl_stmt_seq *stmts, bool is_interactive)
869875
{
870876
if (SYMTABLE_ENTRY(c)->ste_has_conditional_annotations) {
871-
ADDOP_I(c, loc, BUILD_SET, 0);
872-
ADDOP_N(c, loc, STORE_NAME, &_Py_ID(__conditional_annotations__), names);
877+
ADDOP_I(c, NO_LOCATION, BUILD_SET, 0);
878+
ADDOP_N(c, NO_LOCATION, STORE_NAME, &_Py_ID(__conditional_annotations__), names);
873879
}
874880
return codegen_body(c, loc, stmts, is_interactive);
875881
}
@@ -1562,8 +1568,8 @@ codegen_class_body(compiler *c, stmt_ty s, int firstlineno)
15621568
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__classdict__), cellvars);
15631569
}
15641570
if (SYMTABLE_ENTRY(c)->ste_has_conditional_annotations) {
1565-
ADDOP_I(c, loc, BUILD_SET, 0);
1566-
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__conditional_annotations__), cellvars);
1571+
ADDOP_I(c, NO_LOCATION, BUILD_SET, 0);
1572+
ADDOP_N_IN_SCOPE(c, NO_LOCATION, STORE_DEREF, &_Py_ID(__conditional_annotations__), cellvars);
15671573
}
15681574
/* compile the body proper */
15691575
RETURN_IF_ERROR_IN_SCOPE(c, codegen_body(c, loc, s->v.ClassDef.body, false));

0 commit comments

Comments
 (0)