Skip to content

Commit bf1b5d3

Browse files
authored
gh-121404: more compiler_* -> codegen_*, class_body and comprehensions (#123262)
1 parent 58fdb16 commit bf1b5d3

File tree

1 file changed

+82
-59
lines changed

1 file changed

+82
-59
lines changed

Python/compile.c

Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static PySTEntryObject *compiler_symtable_entry(struct compiler *c);
9696
#define IS_INTERACTIVE(C) compiler_is_interactive(C)
9797
#define IS_NESTED_SCOPE(C) compiler_is_nested_scope(C)
9898
#define SCOPE_TYPE(C) compiler_scope_type(C)
99+
#define QUALNAME(C) compiler_qualname(C)
99100

100101
typedef _Py_SourceLocation location;
101102
typedef struct _PyCfgBuilder cfg_builder;
@@ -106,6 +107,7 @@ static int compiler_optimization_level(struct compiler *c);
106107
static int compiler_is_interactive(struct compiler *c);
107108
static int compiler_is_nested_scope(struct compiler *c);
108109
static int compiler_scope_type(struct compiler *c);
110+
static PyObject *compiler_qualname(struct compiler *c);
109111

110112
#define LOCATION(LNO, END_LNO, COL, END_COL) \
111113
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
@@ -336,14 +338,14 @@ static int codegen_call_helper(struct compiler *c, location loc,
336338
static int codegen_try_except(struct compiler *, stmt_ty);
337339
static int codegen_try_star_except(struct compiler *, stmt_ty);
338340

339-
static int compiler_sync_comprehension_generator(
341+
static int codegen_sync_comprehension_generator(
340342
struct compiler *c, location loc,
341343
asdl_comprehension_seq *generators, int gen_index,
342344
int depth,
343345
expr_ty elt, expr_ty val, int type,
344346
int iter_on_stack);
345347

346-
static int compiler_async_comprehension_generator(
348+
static int codegen_async_comprehension_generator(
347349
struct compiler *c, location loc,
348350
asdl_comprehension_seq *generators, int gen_index,
349351
int depth,
@@ -1714,6 +1716,13 @@ dict_lookup_arg(PyObject *dict, PyObject *name)
17141716
return PyLong_AsLong(v);
17151717
}
17161718

1719+
static int
1720+
compiler_lookup_cellvar(struct compiler *c, PyObject *name)
1721+
{
1722+
assert(c->u->u_metadata.u_cellvars);
1723+
return dict_lookup_arg(c->u->u_metadata.u_cellvars, name);
1724+
}
1725+
17171726
static int
17181727
compiler_lookup_arg(struct compiler *c, PyCodeObject *co, PyObject *name)
17191728
{
@@ -2367,8 +2376,15 @@ codegen_set_type_params_in_class(struct compiler *c, location loc)
23672376
return SUCCESS;
23682377
}
23692378

2379+
static PyObject *
2380+
compiler_static_attributes_tuple(struct compiler *c)
2381+
{
2382+
assert(c->u->u_static_attributes);
2383+
return PySequence_Tuple(c->u->u_static_attributes);
2384+
}
2385+
23702386
static int
2371-
compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2387+
codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
23722388
{
23732389
/* ultimately generate code for:
23742390
<name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
@@ -2391,8 +2407,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
23912407
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__name__), Load));
23922408
/* ... and store it as __module__ */
23932409
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__module__), Store));
2394-
assert(c->u->u_metadata.u_qualname);
2395-
ADDOP_LOAD_CONST(c, loc, c->u->u_metadata.u_qualname);
2410+
ADDOP_LOAD_CONST(c, loc, QUALNAME(c));
23962411
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__qualname__), Store));
23972412
ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromLong(c->u->u_metadata.u_firstlineno));
23982413
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__firstlineno__), Store));
@@ -2410,8 +2425,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24102425
}
24112426
/* compile the body proper */
24122427
RETURN_IF_ERROR_IN_SCOPE(c, codegen_body(c, loc, s->v.ClassDef.body));
2413-
assert(c->u->u_static_attributes);
2414-
PyObject *static_attributes = PySequence_Tuple(c->u->u_static_attributes);
2428+
PyObject *static_attributes = compiler_static_attributes_tuple(c);
24152429
if (static_attributes == NULL) {
24162430
compiler_exit_scope(c);
24172431
return ERROR;
@@ -2424,7 +2438,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24242438
/* Set __classdictcell__ if necessary */
24252439
if (SYMTABLE_ENTRY(c)->ste_needs_classdict) {
24262440
/* Store __classdictcell__ into class namespace */
2427-
int i = dict_lookup_arg(c->u->u_metadata.u_cellvars, &_Py_ID(__classdict__));
2441+
int i = compiler_lookup_cellvar(c, &_Py_ID(__classdict__));
24282442
RETURN_IF_ERROR_IN_SCOPE(c, i);
24292443
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
24302444
RETURN_IF_ERROR_IN_SCOPE(
@@ -2433,7 +2447,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24332447
/* Return __classcell__ if it is referenced, otherwise return None */
24342448
if (SYMTABLE_ENTRY(c)->ste_needs_class_closure) {
24352449
/* Store __classcell__ into class namespace & return it */
2436-
int i = dict_lookup_arg(c->u->u_metadata.u_cellvars, &_Py_ID(__class__));
2450+
int i = compiler_lookup_cellvar(c, &_Py_ID(__class__));
24372451
RETURN_IF_ERROR_IN_SCOPE(c, i);
24382452
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
24392453
ADDOP_I(c, NO_LOCATION, COPY, 1);
@@ -2503,7 +2517,7 @@ codegen_class(struct compiler *c, stmt_ty s)
25032517
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Store));
25042518
}
25052519

2506-
int ret = compiler_class_body(c, s, firstlineno);
2520+
int ret = codegen_class_body(c, s, firstlineno);
25072521
if (is_generic) {
25082522
RETURN_IF_ERROR_IN_SCOPE(c, ret);
25092523
}
@@ -5107,31 +5121,31 @@ codegen_call_helper(struct compiler *c, location loc,
51075121

51085122

51095123
static int
5110-
compiler_comprehension_generator(struct compiler *c, location loc,
5111-
asdl_comprehension_seq *generators, int gen_index,
5112-
int depth,
5113-
expr_ty elt, expr_ty val, int type,
5114-
int iter_on_stack)
5124+
codegen_comprehension_generator(struct compiler *c, location loc,
5125+
asdl_comprehension_seq *generators, int gen_index,
5126+
int depth,
5127+
expr_ty elt, expr_ty val, int type,
5128+
int iter_on_stack)
51155129
{
51165130
comprehension_ty gen;
51175131
gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
51185132
if (gen->is_async) {
5119-
return compiler_async_comprehension_generator(
5133+
return codegen_async_comprehension_generator(
51205134
c, loc, generators, gen_index, depth, elt, val, type,
51215135
iter_on_stack);
51225136
} else {
5123-
return compiler_sync_comprehension_generator(
5137+
return codegen_sync_comprehension_generator(
51245138
c, loc, generators, gen_index, depth, elt, val, type,
51255139
iter_on_stack);
51265140
}
51275141
}
51285142

51295143
static int
5130-
compiler_sync_comprehension_generator(struct compiler *c, location loc,
5131-
asdl_comprehension_seq *generators,
5132-
int gen_index, int depth,
5133-
expr_ty elt, expr_ty val, int type,
5134-
int iter_on_stack)
5144+
codegen_sync_comprehension_generator(struct compiler *c, location loc,
5145+
asdl_comprehension_seq *generators,
5146+
int gen_index, int depth,
5147+
expr_ty elt, expr_ty val, int type,
5148+
int iter_on_stack)
51355149
{
51365150
/* generate code for the iterator, then each of the ifs,
51375151
and then write to the element */
@@ -5145,8 +5159,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
51455159

51465160
if (!iter_on_stack) {
51475161
if (gen_index == 0) {
5148-
/* Receive outermost iter as an implicit argument */
5149-
c->u->u_metadata.u_argcount = 1;
5162+
assert(c->u->u_metadata.u_argcount == 1);
51505163
ADDOP_I(c, loc, LOAD_FAST, 0);
51515164
}
51525165
else {
@@ -5195,9 +5208,9 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
51955208

51965209
if (++gen_index < asdl_seq_LEN(generators)) {
51975210
RETURN_IF_ERROR(
5198-
compiler_comprehension_generator(c, loc,
5199-
generators, gen_index, depth,
5200-
elt, val, type, 0));
5211+
codegen_comprehension_generator(c, loc,
5212+
generators, gen_index, depth,
5213+
elt, val, type, 0));
52015214
}
52025215

52035216
location elt_loc = LOC(elt);
@@ -5252,7 +5265,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
52525265
}
52535266

52545267
static int
5255-
compiler_async_comprehension_generator(struct compiler *c, location loc,
5268+
codegen_async_comprehension_generator(struct compiler *c, location loc,
52565269
asdl_comprehension_seq *generators,
52575270
int gen_index, int depth,
52585271
expr_ty elt, expr_ty val, int type,
@@ -5267,8 +5280,7 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
52675280

52685281
if (!iter_on_stack) {
52695282
if (gen_index == 0) {
5270-
/* Receive outermost iter as an implicit argument */
5271-
c->u->u_metadata.u_argcount = 1;
5283+
assert(c->u->u_metadata.u_argcount == 1);
52725284
ADDOP_I(c, loc, LOAD_FAST, 0);
52735285
}
52745286
else {
@@ -5300,9 +5312,9 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
53005312
depth++;
53015313
if (++gen_index < asdl_seq_LEN(generators)) {
53025314
RETURN_IF_ERROR(
5303-
compiler_comprehension_generator(c, loc,
5304-
generators, gen_index, depth,
5305-
elt, val, type, 0));
5315+
codegen_comprehension_generator(c, loc,
5316+
generators, gen_index, depth,
5317+
elt, val, type, 0));
53065318
}
53075319

53085320
location elt_loc = LOC(elt);
@@ -5622,15 +5634,15 @@ codegen_comprehension_iter(struct compiler *c, location loc,
56225634
}
56235635

56245636
static int
5625-
compiler_comprehension(struct compiler *c, expr_ty e, int type,
5626-
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
5627-
expr_ty val)
5637+
codegen_comprehension(struct compiler *c, expr_ty e, int type,
5638+
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
5639+
expr_ty val)
56285640
{
56295641
PyCodeObject *co = NULL;
56305642
inlined_comprehension_state inline_state = {NULL, NULL, NULL, NO_LABEL};
56315643
comprehension_ty outermost;
56325644
#ifndef NDEBUG
5633-
int scope_type = c->u->u_scope_type;
5645+
int scope_type = SCOPE_TYPE(c);
56345646
int is_top_level_await = IS_TOP_LEVEL_AWAIT(c);
56355647
#endif
56365648
PySTEntryObject *entry = _PySymtable_Lookup(SYMTABLE(c), (void *)e);
@@ -5652,8 +5664,12 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
56525664
}
56535665
}
56545666
else {
5667+
/* Receive outermost iter as an implicit argument */
5668+
_PyCompile_CodeUnitMetadata umd = {
5669+
.u_argcount = 1,
5670+
};
56555671
if (compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
5656-
(void *)e, e->lineno, NULL, NULL) < 0) {
5672+
(void *)e, e->lineno, NULL, &umd) < 0) {
56575673
goto error;
56585674
}
56595675
}
@@ -5689,8 +5705,8 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
56895705
}
56905706
}
56915707

5692-
if (compiler_comprehension_generator(c, loc, generators, 0, 0,
5693-
elt, val, type, is_inlined) < 0) {
5708+
if (codegen_comprehension_generator(c, loc, generators, 0, 0,
5709+
elt, val, type, is_inlined) < 0) {
56945710
goto error_in_scope;
56955711
}
56965712

@@ -5753,29 +5769,29 @@ codegen_genexp(struct compiler *c, expr_ty e)
57535769
{
57545770
assert(e->kind == GeneratorExp_kind);
57555771
_Py_DECLARE_STR(anon_genexpr, "<genexpr>");
5756-
return compiler_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
5757-
e->v.GeneratorExp.generators,
5758-
e->v.GeneratorExp.elt, NULL);
5772+
return codegen_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
5773+
e->v.GeneratorExp.generators,
5774+
e->v.GeneratorExp.elt, NULL);
57595775
}
57605776

57615777
static int
57625778
codegen_listcomp(struct compiler *c, expr_ty e)
57635779
{
57645780
assert(e->kind == ListComp_kind);
57655781
_Py_DECLARE_STR(anon_listcomp, "<listcomp>");
5766-
return compiler_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
5767-
e->v.ListComp.generators,
5768-
e->v.ListComp.elt, NULL);
5782+
return codegen_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
5783+
e->v.ListComp.generators,
5784+
e->v.ListComp.elt, NULL);
57695785
}
57705786

57715787
static int
57725788
codegen_setcomp(struct compiler *c, expr_ty e)
57735789
{
57745790
assert(e->kind == SetComp_kind);
57755791
_Py_DECLARE_STR(anon_setcomp, "<setcomp>");
5776-
return compiler_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
5777-
e->v.SetComp.generators,
5778-
e->v.SetComp.elt, NULL);
5792+
return codegen_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
5793+
e->v.SetComp.generators,
5794+
e->v.SetComp.elt, NULL);
57795795
}
57805796

57815797

@@ -5784,9 +5800,9 @@ codegen_dictcomp(struct compiler *c, expr_ty e)
57845800
{
57855801
assert(e->kind == DictComp_kind);
57865802
_Py_DECLARE_STR(anon_dictcomp, "<dictcomp>");
5787-
return compiler_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
5788-
e->v.DictComp.generators,
5789-
e->v.DictComp.key, e->v.DictComp.value);
5803+
return codegen_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
5804+
e->v.DictComp.generators,
5805+
e->v.DictComp.key, e->v.DictComp.value);
57905806
}
57915807

57925808

@@ -6264,7 +6280,7 @@ codegen_check_ann_expr(struct compiler *c, expr_ty e)
62646280
}
62656281

62666282
static int
6267-
compiler_check_annotation(struct compiler *c, stmt_ty s)
6283+
codegen_check_annotation(struct compiler *c, stmt_ty s)
62686284
{
62696285
/* Annotations of complex targets does not produce anything
62706286
under annotations future */
@@ -6273,8 +6289,8 @@ compiler_check_annotation(struct compiler *c, stmt_ty s)
62736289
}
62746290

62756291
/* Annotations are only evaluated in a module or class. */
6276-
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
6277-
c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
6292+
if (SCOPE_TYPE(c) == COMPILER_SCOPE_MODULE ||
6293+
SCOPE_TYPE(c) == COMPILER_SCOPE_CLASS) {
62786294
return codegen_check_ann_expr(c, s->v.AnnAssign.annotation);
62796295
}
62806296
return SUCCESS;
@@ -6384,7 +6400,7 @@ codegen_annassign(struct compiler *c, stmt_ty s)
63846400
return ERROR;
63856401
}
63866402
/* Annotation is evaluated last. */
6387-
if (future_annotations && !s->v.AnnAssign.simple && compiler_check_annotation(c, s) < 0) {
6403+
if (future_annotations && !s->v.AnnAssign.simple && codegen_check_annotation(c, s) < 0) {
63886404
return ERROR;
63896405
}
63906406
return SUCCESS;
@@ -6604,7 +6620,7 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
66046620
}
66056621

66066622
static int
6607-
compiler_error_duplicate_store(struct compiler *c, location loc, identifier n)
6623+
codegen_error_duplicate_store(struct compiler *c, location loc, identifier n)
66086624
{
66096625
return compiler_error(c, loc,
66106626
"multiple assignments to name %R in pattern", n);
@@ -6632,7 +6648,7 @@ codegen_pattern_helper_store_name(struct compiler *c, location loc,
66326648
int duplicate = PySequence_Contains(pc->stores, n);
66336649
RETURN_IF_ERROR(duplicate);
66346650
if (duplicate) {
6635-
return compiler_error_duplicate_store(c, loc, n);
6651+
return codegen_error_duplicate_store(c, loc, n);
66366652
}
66376653
// Rotate this object underneath any items we need to preserve:
66386654
Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1;
@@ -7125,7 +7141,7 @@ codegen_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
71257141
goto error;
71267142
}
71277143
if (dupe) {
7128-
compiler_error_duplicate_store(c, LOC(p), name);
7144+
codegen_error_duplicate_store(c, LOC(p), name);
71297145
goto error;
71307146
}
71317147
if (PyList_Append(pc->stores, name)) {
@@ -7436,6 +7452,13 @@ compiler_scope_type(struct compiler *c)
74367452
return c->u->u_scope_type;
74377453
}
74387454

7455+
static PyObject *
7456+
compiler_qualname(struct compiler *c)
7457+
{
7458+
assert(c->u->u_metadata.u_qualname);
7459+
return c->u->u_metadata.u_qualname;
7460+
}
7461+
74397462
static int
74407463
compute_code_flags(struct compiler *c)
74417464
{

0 commit comments

Comments
 (0)