Skip to content

Commit a2d0818

Browse files
authored
gh-123881: make compiler add the .generic_base base class without constructing AST nodes (#123883)
1 parent 3597642 commit a2d0818

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

Include/internal/pycore_compile.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ int _PyCompile_IsNestedScope(struct _PyCompiler *c);
136136
int _PyCompile_IsInInlinedComp(struct _PyCompiler *c);
137137
int _PyCompile_ScopeType(struct _PyCompiler *c);
138138
int _PyCompile_OptimizationLevel(struct _PyCompiler *c);
139-
PyArena *_PyCompile_Arena(struct _PyCompiler *c);
140139
int _PyCompile_LookupArg(struct _PyCompiler *c, PyCodeObject *co, PyObject *name);
141140
PyObject *_PyCompile_Qualname(struct _PyCompiler *c);
142141
_PyCompile_CodeUnitMetadata *_PyCompile_Metadata(struct _PyCompiler *c);

Python/codegen.c

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ typedef struct _PyCompiler compiler;
7676
#define SCOPE_TYPE(C) _PyCompile_ScopeType(C)
7777
#define QUALNAME(C) _PyCompile_Qualname(C)
7878
#define METADATA(C) _PyCompile_Metadata(C)
79-
#define ARENA(C) _PyCompile_Arena(C)
8079

8180
typedef _PyInstruction instruction;
8281
typedef _PyInstructionSequence instr_sequence;
@@ -209,6 +208,11 @@ static int codegen_call_simple_kw_helper(compiler *c,
209208
location loc,
210209
asdl_keyword_seq *keywords,
211210
Py_ssize_t nkwelts);
211+
static int codegen_call_helper_impl(compiler *c, location loc,
212+
int n, /* Args already pushed */
213+
asdl_expr_seq *args,
214+
PyObject *injected_arg,
215+
asdl_keyword_seq *keywords);
212216
static int codegen_call_helper(compiler *c, location loc,
213217
int n, asdl_expr_seq *args,
214218
asdl_keyword_seq *keywords);
@@ -1549,28 +1553,10 @@ codegen_class(compiler *c, stmt_ty s)
15491553
ADDOP_I_IN_SCOPE(c, loc, CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC);
15501554
RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_STR(generic_base), Store));
15511555

1552-
Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
1553-
asdl_expr_seq *bases = _Py_asdl_expr_seq_new(
1554-
original_len + 1, ARENA(c));
1555-
if (bases == NULL) {
1556-
_PyCompile_ExitScope(c);
1557-
return ERROR;
1558-
}
1559-
for (Py_ssize_t i = 0; i < original_len; i++) {
1560-
asdl_seq_SET(bases, i, asdl_seq_GET(s->v.ClassDef.bases, i));
1561-
}
1562-
expr_ty name_node = _PyAST_Name(
1563-
&_Py_STR(generic_base), Load,
1564-
loc.lineno, loc.col_offset, loc.end_lineno, loc.end_col_offset, ARENA(c)
1565-
);
1566-
if (name_node == NULL) {
1567-
_PyCompile_ExitScope(c);
1568-
return ERROR;
1569-
}
1570-
asdl_seq_SET(bases, original_len, name_node);
1571-
RETURN_IF_ERROR_IN_SCOPE(c, codegen_call_helper(c, loc, 2,
1572-
bases,
1573-
s->v.ClassDef.keywords));
1556+
RETURN_IF_ERROR_IN_SCOPE(c, codegen_call_helper_impl(c, loc, 2,
1557+
s->v.ClassDef.bases,
1558+
&_Py_STR(generic_base),
1559+
s->v.ClassDef.keywords));
15741560

15751561
PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 0);
15761562

@@ -3187,19 +3173,18 @@ codegen_boolop(compiler *c, expr_ty e)
31873173
}
31883174

31893175
static int
3190-
starunpack_helper(compiler *c, location loc,
3191-
asdl_expr_seq *elts, int pushed,
3192-
int build, int add, int extend, int tuple)
3176+
starunpack_helper_impl(compiler *c, location loc,
3177+
asdl_expr_seq *elts, PyObject *injected_arg, int pushed,
3178+
int build, int add, int extend, int tuple)
31933179
{
31943180
Py_ssize_t n = asdl_seq_LEN(elts);
3195-
if (n > 2 && are_all_items_const(elts, 0, n)) {
3181+
if (!injected_arg && n > 2 && are_all_items_const(elts, 0, n)) {
31963182
PyObject *folded = PyTuple_New(n);
31973183
if (folded == NULL) {
31983184
return ERROR;
31993185
}
3200-
PyObject *val;
32013186
for (Py_ssize_t i = 0; i < n; i++) {
3202-
val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3187+
PyObject *val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
32033188
PyTuple_SET_ITEM(folded, i, Py_NewRef(val));
32043189
}
32053190
if (tuple && !pushed) {
@@ -3221,7 +3206,7 @@ starunpack_helper(compiler *c, location loc,
32213206
return SUCCESS;
32223207
}
32233208

3224-
int big = n+pushed > STACK_USE_GUIDELINE;
3209+
int big = n + pushed + (injected_arg ? 1 : 0) > STACK_USE_GUIDELINE;
32253210
int seen_star = 0;
32263211
for (Py_ssize_t i = 0; i < n; i++) {
32273212
expr_ty elt = asdl_seq_GET(elts, i);
@@ -3235,6 +3220,10 @@ starunpack_helper(compiler *c, location loc,
32353220
expr_ty elt = asdl_seq_GET(elts, i);
32363221
VISIT(c, expr, elt);
32373222
}
3223+
if (injected_arg) {
3224+
RETURN_IF_ERROR(codegen_nameop(c, loc, injected_arg, Load));
3225+
n++;
3226+
}
32383227
if (tuple) {
32393228
ADDOP_I(c, loc, BUILD_TUPLE, n+pushed);
32403229
} else {
@@ -3265,12 +3254,25 @@ starunpack_helper(compiler *c, location loc,
32653254
}
32663255
}
32673256
assert(sequence_built);
3257+
if (injected_arg) {
3258+
RETURN_IF_ERROR(codegen_nameop(c, loc, injected_arg, Load));
3259+
ADDOP_I(c, loc, add, 1);
3260+
}
32683261
if (tuple) {
32693262
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_LIST_TO_TUPLE);
32703263
}
32713264
return SUCCESS;
32723265
}
32733266

3267+
static int
3268+
starunpack_helper(compiler *c, location loc,
3269+
asdl_expr_seq *elts, int pushed,
3270+
int build, int add, int extend, int tuple)
3271+
{
3272+
return starunpack_helper_impl(c, loc, elts, NULL, pushed,
3273+
build, add, extend, tuple);
3274+
}
3275+
32743276
static int
32753277
unpack_helper(compiler *c, location loc, asdl_expr_seq *elts)
32763278
{
@@ -3973,13 +3975,13 @@ codegen_call_simple_kw_helper(compiler *c, location loc,
39733975
return SUCCESS;
39743976
}
39753977

3976-
39773978
/* shared code between codegen_call and codegen_class */
39783979
static int
3979-
codegen_call_helper(compiler *c, location loc,
3980-
int n, /* Args already pushed */
3981-
asdl_expr_seq *args,
3982-
asdl_keyword_seq *keywords)
3980+
codegen_call_helper_impl(compiler *c, location loc,
3981+
int n, /* Args already pushed */
3982+
asdl_expr_seq *args,
3983+
PyObject *injected_arg,
3984+
asdl_keyword_seq *keywords)
39833985
{
39843986
Py_ssize_t i, nseen, nelts, nkwelts;
39853987

@@ -4010,6 +4012,10 @@ codegen_call_helper(compiler *c, location loc,
40104012
assert(elt->kind != Starred_kind);
40114013
VISIT(c, expr, elt);
40124014
}
4015+
if (injected_arg) {
4016+
RETURN_IF_ERROR(codegen_nameop(c, loc, injected_arg, Load));
4017+
nelts++;
4018+
}
40134019
if (nkwelts) {
40144020
VISIT_SEQ(c, keyword, keywords);
40154021
RETURN_IF_ERROR(
@@ -4024,12 +4030,12 @@ codegen_call_helper(compiler *c, location loc,
40244030
ex_call:
40254031

40264032
/* Do positional arguments. */
4027-
if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4033+
if (n == 0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
40284034
VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
40294035
}
40304036
else {
4031-
RETURN_IF_ERROR(starunpack_helper(c, loc, args, n, BUILD_LIST,
4032-
LIST_APPEND, LIST_EXTEND, 1));
4037+
RETURN_IF_ERROR(starunpack_helper_impl(c, loc, args, injected_arg, n,
4038+
BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1));
40334039
}
40344040
/* Then keyword arguments */
40354041
if (nkwelts) {
@@ -4074,6 +4080,14 @@ codegen_call_helper(compiler *c, location loc,
40744080
return SUCCESS;
40754081
}
40764082

4083+
static int
4084+
codegen_call_helper(compiler *c, location loc,
4085+
int n, /* Args already pushed */
4086+
asdl_expr_seq *args,
4087+
asdl_keyword_seq *keywords)
4088+
{
4089+
return codegen_call_helper_impl(c, loc, n, args, NULL, keywords);
4090+
}
40774091

40784092
/* List and set comprehensions and generator expressions work by creating a
40794093
nested function to perform the actual iteration. This means that the

Python/compile.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ typedef struct _PyCompiler {
8888
including names tuple */
8989
struct compiler_unit *u; /* compiler state for current block */
9090
PyObject *c_stack; /* Python list holding compiler_unit ptrs */
91-
PyArena *c_arena; /* pointer to memory allocation arena */
9291

9392
bool c_save_nested_seqs; /* if true, construct recursive instruction sequences
9493
* (including instructions for nested code objects)
@@ -112,7 +111,6 @@ compiler_setup(compiler *c, mod_ty mod, PyObject *filename,
112111
}
113112

114113
c->c_filename = Py_NewRef(filename);
115-
c->c_arena = arena;
116114
if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
117115
return ERROR;
118116
}
@@ -1244,12 +1242,6 @@ _PyCompile_Metadata(compiler *c)
12441242
return &c->u->u_metadata;
12451243
}
12461244

1247-
PyArena *
1248-
_PyCompile_Arena(compiler *c)
1249-
{
1250-
return c->c_arena;
1251-
}
1252-
12531245
#ifndef NDEBUG
12541246
int
12551247
_PyCompile_IsTopLevelAwait(compiler *c)

0 commit comments

Comments
 (0)