8000 Avoid complicating compile_call_helper · python/cpython@48e23b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48e23b0

Browse files
committed
Avoid complicating compile_call_helper
1 parent 6312d7b commit 48e23b0

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct _Py_global_strings {
4444
STRUCT_FOR_STR(dot, ".")
4545
STRUCT_FOR_STR(dot_locals, ".<locals>")
4646
STRUCT_FOR_STR(empty, "")
47+
STRUCT_FOR_STR(generic_base, ".generic_base")
4748
STRUCT_FOR_STR(json_decoder, "json.decoder")
4849
STRUCT_FOR_STR(kwdefaults, ".kwdefaults")
4950
STRUCT_FOR_STR(list_err, "list index out of range")

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compile.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@ static int compiler_call_simple_kw_helper(struct compiler *c,
467467
Py_ssize_t nkwelts);
468468
static int compiler_call_helper(struct compiler *c, location loc,
469469
int n, asdl_expr_seq *args,
470-
asdl_keyword_seq *keywords,
471-
PyObject *extra_positional_arg);
470+
asdl_keyword_seq *keywords);
472471
static int compiler_try_except(struct compiler *, stmt_ty);
473472
static int compiler_try_star_except(struct compiler *, stmt_ty);
474473
static int compiler_set_qualname(struct compiler *);
@@ -2410,10 +2409,28 @@ compiler_class(struct compiler *c, stmt_ty s)
24102409

24112410
if (typeparams) {
24122411
_Py_DECLARE_STR(type_params, ".type_params");
2412+
_Py_DECLARE_STR(generic_base, ".generic_base");
2413+
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_STR(type_params), Load));
2414+
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC);
2415+
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_STR(generic_base), Store));
2416+
2417+
Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
2418+
asdl_expr_seq *bases = _Py_asdl_expr_seq_new(
2419+
original_len + 1, c->c_arena);
2420+
for (Py_ssize_t i = 0; i < original_len; i++) {
2421+
asdl_seq_SET(bases, i, asdl_seq_GET(s->v.ClassDef.bases, i));
2422+
}
2423+
expr_ty name_node = _PyAST_Name(
2424+
&_Py_STR(generic_base), Load,
2425+
loc.lineno, loc.col_offset, loc.end_lineno, loc.end_col_offset, c->c_arena
2426+
);
2427+
if (name_node == NULL) {
2428+
return ERROR;
2429+
}
2430+
asdl_seq_SET(bases, original_len, name_node);
24132431
RETURN_IF_ERROR(compiler_call_helper(c, loc, 2,
2414-
s->v.ClassDef.bases,
2415-
s->v.ClassDef.keywords,
2416-
&_Py_STR(type_params)));
2432+
bases,
2433+
s->v.ClassDef.keywords));
24172434

24182435
int is_in_class = c->u->u_ste->ste_type_params_in_class;
24192436
c->u->u_metadata.u_argcount = is_in_class;
@@ -2434,8 +2451,7 @@ compiler_class(struct compiler *c, stmt_ty s)
24342451
} else {
24352452
RETURN_IF_ERROR(compiler_call_helper(c, loc, 2,
24362453
s->v.ClassDef.bases,
2437-
s->v.ClassDef.keywords,
2438-
NULL));
2454+
s->v.ClassDef.keywords));
24392455
}
24402456

24412457
/* 6. apply decorators */
@@ -4607,8 +4623,7 @@ compiler_call(struct compiler *c, expr_ty e)
46074623
loc = LOC(e);
46084624
return compiler_call_helper(c, loc, 0,
46094625
e->v.Call.args,
4610-
e->v.Call.keywords,
4611-
NULL);
4626+
e->v.Call.keywords);
46124627
}
46134628

46144629
static int
@@ -4758,18 +4773,16 @@ static int
47584773
compiler_call_helper(struct compiler *c, location loc,
47594774
int n, /* Args already pushed */
47604775
asdl_expr_seq *args,
4761-
asdl_keyword_seq *keywords,
4762-
PyObject *extra_positional_arg)
4776+
asdl_keyword_seq *keywords)
47634777
{
4764-
Py_ssize_t i, nseen, nelts, nkwelts, real_nelts;
4778+
Py_ssize_t i, nseen, nelts, nkwelts;
47654779

47664780
RETURN_IF_ERROR(validate_keywords(c, keywords));
47674781

47684782
nelts = asdl_seq_LEN(args);
47694783
nkwelts = asdl_seq_LEN(keywords);
4770-
real_nelts = extra_positional_arg == NULL ? nelts : nelts + 1;
47714784

4772-
if (real_nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4785+
if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
47734786
goto ex_call;
47744787
}
47754788
for (i = 0; i < nelts; i++) {
@@ -4791,22 +4804,16 @@ compiler_call_helper(struct compiler *c, location loc,
47914804
assert(elt->kind != Starred_kind);
47924805
VISIT(c, expr, elt);
47934806
}
4794-
if (extra_positional_arg != NULL) {
4795-
RETURN_IF_ERROR(compiler_nameop(c, loc, extra_positional_arg, Load));
4796-
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC);
4797-
}
47984807
if (nkwelts) {
47994808
VISIT_SEQ(c, keyword, keywords);
48004809
RETURN_IF_ERROR(
48014810
compiler_call_simple_kw_helper(c, loc, keywords, nkwelts));
48024811
}
4803-
ADDOP_I(c, loc, CALL, n + real_nelts + nkwelts);
4812+
ADDOP_I(c, loc, CALL, n + nelts + nkwelts);
48044813
return SUCCESS;
48054814

48064815
ex_call:
48074816

4808-
assert(extra_positional_arg == NULL); // TODO(PEP 695)
4809-
48104817
/* Do positional arguments. */
48114818
if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
48124819
VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);

Python/symtable.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,16 @@ symtable_enter_typeparam_block(struct symtable *st, identifier name,
11561156
lineno, col_offset, end_lineno, end_col_offset)) {
11571157
return 0;
11581158
}
1159+
// This is used for setting the generic base
1160+
_Py_DECLARE_STR(generic_base, ".generic_base");
1161+
if (!symtable_add_def(st, &_Py_STR(generic_base), DEF_LOCAL,
1162+
lineno, col_offset, end_lineno, end_col_offset)) {
1163+
return 0;
1164+
}
1165+
if (!symtable_add_def(st, &_Py_STR(generic_base), USE,
1166+
lineno, col_offset, end_lineno, end_col_offset)) {
1167+
return 0;
1168+
}
11591169
}
11601170
if (has_defaults) {
11611171
_Py_DECLARE_STR(defaults, ".defaults");

0 commit comments

Comments
 (0)
0