Skip to content

Commit 5fce482

Browse files
authored
gh-121404: compiler_annassign --> codegen_annassign (#123245)
1 parent 5d3201f commit 5fce482

File tree

1 file changed

+100
-63
lines changed

1 file changed

+100
-63
lines changed

Python/compile.c

Lines changed: 100 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static PySTEntryObject *compiler_symtable_entry(struct compiler *c);
9595
#define OPTIMIZATION_LEVEL(C) compiler_optimization_level(C)
9696
#define IS_INTERACTIVE(C) compiler_is_interactive(C)
9797
#define IS_NESTED_SCOPE(C) compiler_is_nested_scope(C)
98+
#define SCOPE_TYPE(C) compiler_scope_type(C)
9899

99100
typedef _Py_SourceLocation location;
100101
typedef struct _PyCfgBuilder cfg_builder;
@@ -104,6 +105,7 @@ static PyObject *compiler_maybe_mangle(struct compiler *c, PyObject *name);
104105
static int compiler_optimization_level(struct compiler *c);
105106
static int compiler_is_interactive(struct compiler *c);
106107
static int compiler_is_nested_scope(struct compiler *c);
108+
static int compiler_scope_type(struct compiler *c);
107109

108110
#define LOCATION(LNO, END_LNO, COL, END_COL) \
109111
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
@@ -314,7 +316,7 @@ static int compiler_visit_stmt(struct compiler *, stmt_ty);
314316
static int compiler_visit_keyword(struct compiler *, keyword_ty);
315317
static int compiler_visit_expr(struct compiler *, expr_ty);
316318
static int codegen_augassign(struct compiler *, stmt_ty);
317-
static int compiler_annassign(struct compiler *, stmt_ty);
319+
static int codegen_annassign(struct compiler *, stmt_ty);
318320
static int codegen_subscript(struct compiler *, expr_ty);
319321
static int codegen_slice(struct compiler *, expr_ty);
320322

@@ -1481,7 +1483,7 @@ codegen_setup_annotations_scope(struct compiler *c, location loc,
14811483
ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_NOTIMPLEMENTEDERROR);
14821484
ADDOP_I(c, loc, RAISE_VARARGS, 1);
14831485
USE_LABEL(c, body);
1484-
return 0;
1486+
return SUCCESS;
14851487
}
14861488

14871489
static int
@@ -1501,11 +1503,69 @@ codegen_leave_annotations_scope(struct compiler *c, location loc,
15011503
return SUCCESS;
15021504
}
15031505

1506+
static PyObject *
1507+
compiler_deferred_annotations(struct compiler *c)
1508+
{
1509+
return c->u->u_deferred_annotations;
1510+
}
1511+
1512+
static int
1513+
codegen_process_deferred_annotations(struct compiler *c, location loc)
1514+
{
1515+
PyObject *deferred_anno = compiler_deferred_annotations(c);
1516+
if (deferred_anno == NULL) {
1517+
return SUCCESS;
1518+
}
1519+
Py_INCREF(deferred_anno);
1520+
1521+
// It's possible that ste_annotations_block is set but
1522+
// u_deferred_annotations is not, because the former is still
1523+
// set if there are only non-simple annotations (i.e., annotations
1524+
// for attributes, subscripts, or parenthesized names). However, the
1525+
// reverse should not be possible.
1526+
PySTEntryObject *ste = SYMTABLE_ENTRY(c);
1527+
assert(ste->ste_annotation_block != NULL);
1528+
void *key = (void *)((uintptr_t)ste->ste_id + 1);
1529+
if (codegen_setup_annotations_scope(c, loc, key,
1530+
ste->ste_annotation_block->ste_name) < 0) {
1531+
Py_DECREF(deferred_anno);
1532+
return ERROR;
1533+
}
1534+
Py_ssize_t annotations_len = PyList_Size(deferred_anno);
1535+
for (Py_ssize_t i = 0; i < annotations_len; i++) {
1536+
PyObject *ptr = PyList_GET_ITEM(deferred_anno, i);
1537+
stmt_ty st = (stmt_ty)PyLong_AsVoidPtr(ptr);
1538+
if (st == NULL) {
1539+
compiler_exit_scope(c);
1540+
Py_DECREF(deferred_anno);
1541+
return ERROR;
1542+
}
1543+
PyObject *mangled = compiler_mangle(c, st->v.AnnAssign.target->v.Name.id);
1544+
if (!mangled) {
1545+
compiler_exit_scope(c);
1546+
Py_DECREF(deferred_anno);
1547+
return ERROR;
1548+
}
1549+
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled);
1550+
VISIT(c, expr, st->v.AnnAssign.annotation);
1551+
}
1552+
Py_DECREF(deferred_anno);
1553+
1554+
RETURN_IF_ERROR(
1555+
codegen_leave_annotations_scope(c, loc, annotations_len)
1556+
);
1557+
RETURN_IF_ERROR(
1558+
compiler_nameop(c, loc, &_Py_ID(__annotate__), Store)
1559+
);
1560+
1561+
return SUCCESS;
1562+
}
1563+
15041564
/* Compile a sequence of statements, checking for a docstring
15051565
and for annotations. */
15061566

15071567
static int
1508-
compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1568+
codegen_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
15091569
{
15101570
/* If from __future__ import annotations is active,
15111571
* every annotated class and module should have __annotations__.
@@ -1542,44 +1602,8 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
15421602
// If there are annotations and the future import is not on, we
15431603
// collect the annotations in a separate pass and generate an
15441604
// __annotate__ function. See PEP 649.
1545-
if (!(FUTURE_FEATURES(c) & CO_FUTURE_ANNOTATIONS) &&
1546-
c->u->u_deferred_annotations != NULL) {
1547-
1548-
// It's possible that ste_annotations_block is set but
1549-
// u_deferred_annotations is not, because the former is still
1550-
// set if there are only non-simple annotations (i.e., annotations
1551-
// for attributes, subscripts, or parenthesized names). However, the
1552-
// reverse should not be possible.
1553-
PySTEntryObject *ste = SYMTABLE_ENTRY(c);
1554-
assert(ste->ste_annotation_block != NULL);
1555-
PyObject *deferred_anno = Py_NewRef(c->u->u_deferred_annotations);
1556-
void *key = (void *)((uintptr_t)ste->ste_id + 1);
1557-
if (codegen_setup_annotations_scope(c, loc, key,
1558-
ste->ste_annotation_block->ste_name) == -1) {
1559-
Py_DECREF(deferred_anno);
1560-
return ERROR;
1561-
}
1562-
Py_ssize_t annotations_len = PyList_Size(deferred_anno);
1563-
for (Py_ssize_t i = 0; i < annotations_len; i++) {
1564-
PyObject *ptr = PyList_GET_ITEM(deferred_anno, i);
1565-
stmt_ty st = (stmt_ty)PyLong_AsVoidPtr(ptr);
1566-
if (st == NULL) {
1567-
compiler_exit_scope(c);
1568-
Py_DECREF(deferred_anno);
1569-
return ERROR;
1570-
}
1571-
PyObject *mangled = compiler_mangle(c, st->v.AnnAssign.target->v.Name.id);
1572-
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled);
1573-
VISIT(c, expr, st->v.AnnAssign.annotation);
1574-
}
1575-
Py_DECREF(deferred_anno);
1576-
1577-
RETURN_IF_ERROR(
1578-
codegen_leave_annotations_scope(c, loc, annotations_len)
1579-
);
1580-
RETURN_IF_ERROR(
1581-
compiler_nameop(c, loc, &_Py_ID(__annotate__), Store)
1582-
);
1605+
if (!(FUTURE_FEATURES(c) & CO_FUTURE_ANNOTATIONS)) {
1606+
RETURN_IF_ERROR(codegen_process_deferred_annotations(c, loc));
15831607
}
15841608
return SUCCESS;
15851609
}
@@ -1606,13 +1630,13 @@ compiler_codegen(struct compiler *c, mod_ty mod)
16061630
switch (mod->kind) {
16071631
case Module_kind: {
16081632
asdl_stmt_seq *stmts = mod->v.Module.body;
1609-
RETURN_IF_ERROR(compiler_body(c, start_location(stmts), stmts));
1633+
RETURN_IF_ERROR(codegen_body(c, start_location(stmts), stmts));
16101634
break;
16111635
}
16121636
case Interactive_kind: {
16131637
c->c_interactive = 1;
16141638
asdl_stmt_seq *stmts = mod->v.Interactive.body;
1615-
RETURN_IF_ERROR(compiler_body(c, start_location(stmts), stmts));
1639+
RETURN_IF_ERROR(codegen_body(c, start_location(stmts), stmts));
16161640
break;
16171641
}
16181642
case Expression_kind: {
@@ -2385,7 +2409,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
23852409
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__classdict__), cellvars);
23862410
}
23872411
/* compile the body proper */
2388-
RETURN_IF_ERROR_IN_SCOPE(c, compiler_body(c, loc, s->v.ClassDef.body));
2412+
RETURN_IF_ERROR_IN_SCOPE(c, codegen_body(c, loc, s->v.ClassDef.body));
23892413
assert(c->u->u_static_attributes);
23902414
PyObject *static_attributes = PySequence_Tuple(c->u->u_static_attributes);
23912415
if (static_attributes == NULL) {
@@ -3847,7 +3871,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s)
38473871
case AugAssign_kind:
38483872
return codegen_augassign(c, s);
38493873
case AnnAssign_kind:
3850-
return compiler_annassign(c, s);
3874+
return codegen_annassign(c, s);
38513875
case For_kind:
38523876
return codegen_for(c, s);
38533877
case While_kind:
@@ -6287,7 +6311,28 @@ codegen_check_ann_subscr(struct compiler *c, expr_ty e)
62876311
}
62886312

62896313
static int
6290-
compiler_annassign(struct compiler *c, stmt_ty s)
6314+
compiler_add_deferred_annotation(struct compiler *c, stmt_ty s)
6315+
{
6316+
if (c->u->u_deferred_annotations == NULL) {
6317+
c->u->u_deferred_annotations = PyList_New(0);
6318+
if (c->u->u_deferred_annotations == NULL) {
6319+
return ERROR;
6320+
}
6321+
}
6322+
PyObject *ptr = PyLong_FromVoidPtr((void *)s);
6323+
if (ptr == NULL) {
6324+
return ERROR;
6325+
}
6326+
if (PyList_Append(c->u->u_deferred_annotations, ptr) < 0) {
6327+
Py_DECREF(ptr);
6328+
return ERROR;
6329+
}
6330+
Py_DECREF(ptr);
6331+
return SUCCESS;
6332+
}
6333+
6334+
static int
6335+
codegen_annassign(struct compiler *c, stmt_ty s)
62916336
{
62926337
location loc = LOC(s);
62936338
expr_ty targ = s->v.AnnAssign.target;
@@ -6305,8 +6350,8 @@ compiler_annassign(struct compiler *c, stmt_ty s)
63056350
case Name_kind:
63066351
/* If we have a simple name in a module or class, store annotation. */
63076352
if (s->v.AnnAssign.simple &&
6308-
(c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
6309-
c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
6353+
(SCOPE_TYPE(c) == COMPILER_SCOPE_MODULE ||
6354+
SCOPE_TYPE(c) == COMPILER_SCOPE_CLASS)) {
63106355
if (future_annotations) {
63116356
VISIT(c, annexpr, s->v.AnnAssign.annotation);
63126357
ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__annotations__), names);
@@ -6315,21 +6360,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
63156360
ADDOP(c, loc, STORE_SUBSCR);
63166361
}
63176362
else {
6318-
if (c->u->u_deferred_annotations == NULL) {
6319-
c->u->u_deferred_annotations = PyList_New(0);
6320-
if (c->u->u_deferred_annotations == NULL) {
6321-
return ERROR;
6322-
}
6323-
}
6324-
PyObject *ptr = PyLong_FromVoidPtr((void *)s);
6325-
if (ptr == NULL) {
6326-
return ERROR;
6327-
}
6328-
if (PyList_Append(c->u->u_deferred_annotations, ptr) < 0) {
6329-
Py_DECREF(ptr);
6330-
return ERROR;
6331-
}
6332-
Py_DECREF(ptr);
6363+
RETURN_IF_ERROR(compiler_add_deferred_annotation(c, s));
63336364
}
63346365
}
63356366
break;
@@ -7399,6 +7430,12 @@ compiler_is_nested_scope(struct compiler *c)
73997430
return PyList_GET_SIZE(c->c_stack) > 0;
74007431
}
74017432

7433+
static int
7434+
compiler_scope_type(struct compiler *c)
7435+
{
7436+
return c->u->u_scope_type;
7437+
}
7438+
74027439
static int
74037440
compute_code_flags(struct compiler *c)
74047441
{

0 commit comments

Comments
 (0)