10000 fix defaults · python/cpython@dba4293 · GitHub
[go: up one dir, main page]

Skip to content

Commit dba4293

Browse files
committed
fix defaults
1 parent ab6b718 commit dba4293

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

Python/compile.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,15 +2198,33 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
21982198
}
21992199

22002200
location loc = LOC(s);
2201+
2202+
if (typeparams) {
2203+
ADDOP(c, loc, PUSH_NULL);
2204+
}
2205+
22012206
funcflags = compiler_default_arguments(c, loc, args);
22022207
if (funcflags == -1) {
22032208
return ERROR;
22042209
}
22052210

22062211
if (typeparams) {
2207-
ADDOP(c, loc, PUSH_NULL);
2208-
RETURN_IF_ERROR(
2209-
compiler_enter_scope(c, name, scope_type, (void *)typeparams, firstlineno));
2212+
PyObject *typeparams_name = PyUnicode_FromFormat("<generic parameters of %U>", name);
2213+
if (!typeparams_name) {
2214+
return ERROR;
2215+
}
2216+
if (compiler_enter_scope(c, typeparams_name, scope_type,
2217+
(void *)typeparams, firstlineno) == -1) {
2218+
Py_DECREF(typeparams_name);
2219+
return ERROR;
2220+
}
2221+
Py_DECREF(typeparams_name);
2222+
if ((funcflags & 0x01) || (funcflags & 0x02)) {
2223+
ADDOP_I(c, loc, LOAD_FAST, 0);
2224+
}
2225+
if ((funcflags & 0x01) && (funcflags & 0x02)) {
2226+
ADDOP_I(c, loc, LOAD_FAST, 1);
2227+
}
22102228
RETURN_IF_ERROR(compiler_type_params(c, typeparams));
22112229
}
22122230

@@ -2252,6 +2270,12 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22522270
}
22532271
Py_DECREF(co);
22542272
if (typeparams) {
2273+
if (funcflags & 0x02) {
2274+
c->u->u_argcount += 1;
2275+
}
2276+
if (funcflags & 0x01) {
2277+
c->u->u_argcount += 1;
2278+
}
22552279
PyCodeObject *co = assemble(c, 0);
22562280
compiler_exit_scope(c);
22572281
if (co == NULL) {
@@ -2262,7 +2286,16 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22622286
return ERROR;
22632287
}
22642288
Py_DECREF(co);
2265-
ADDOP_I(c, loc, CALL, 0);
2289+
int oparg = 0;
2290+
if ((funcflags & 0x01) && (funcflags & 0x02)) {
2291+
ADDOP_I(c, loc, SWAP, 3);
2292+
oparg = 2;
2293+
}
2294+
else if ((funcflags & 0x01) || (funcflags & 0x02)) {
2295+
ADDOP_I(c, loc, SWAP, 2);
2296+
oparg = 1;
2297+
}
2298+
ADDOP_I(c, loc, CALL, oparg);
22662299
}
22672300

22682301
RETURN_IF_ERROR(compiler_apply_decorators(c, decos));
@@ -7115,7 +7148,6 @@ compute_localsplus_info(struct compiler_unit *u, int nlocalsplus,
71157148
{
71167149
PyObject *k, *v;
71177150
Py_ssize_t pos = 0;
7118-
// printf("compute_localsplus_info %d %s\n", nlocalsplus, PyUnicode_AsUTF8(u->u_name));
71197151
while (PyDict_Next(u->u_varnames, &pos, &k, &v)) {
71207152
int offset = (int)PyLong_AS_LONG(v);
71217153
assert(offset >= 0);

Python/symtable.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,17 @@ symtable_record_directive(struct symtable *st, identifier name, int lineno,
11871187
return res == 0;
11881188
}
11891189

1190+
static int
1191+
has_kwonlydefaults(asdl_arg_seq *kwonlyargs, asdl_expr_seq *kw_defaults)
1192+
{
1193+
for (int i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1194+
expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1195+
if (default_) {
1196+
return 1;
1197+
}
1198+
}
1199+
return 0;
1200+
}
11901201

11911202
static int
11921203
symtable_visit_stmt(struct symtable *st, stmt_ty s)
@@ -1212,6 +1223,29 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
12121223
LOCATION(s))) {
12131224
VISIT_QUIT(st, 0);
12141225
}
1226+
if (s->v.FunctionDef.args->defaults) {
1227+
PyObject *defaults_name = PyUnicode_FromString(".defaults");
1228+
if (defaults_name == NULL) {
1229+
VISIT_QUIT(st, 0);
1230+
}
1231+
if (!symtable_add_def(st, defaults_name, DEF_PARAM, LOCATION(s))) {
1232+
Py_DECREF(defaults_name);
1233+
VISIT_QUIT(st, 0);
1234+
}
1235+
Py_DECREF(defaults_name);
1236+
}
1237+
if (has_kwonlydefaults(s->v.FunctionDef.args->kwonlyargs,
1238+
s->v.FunctionDef.args->kw_defaults)) {
1239+
PyObject *kwonly_name = PyUnicode_FromString(".kwonlydefaults");
1240+
if (kwonly_name == NULL) {
1241+
VISIT_QUIT(st, 0);
1242+
}
1243+
if (!symtable_add_def(st, kwonly_name, DEF_PARAM, LOCATION(s))) {
1244+
Py_DECREF(kwonly_name);
1245+
VISIT_QUIT(st, 0);
1246+
}
1247+
Py_DECREF(kwonly_name);
1248+
}
12151249
VISIT_SEQ(st, typeparam, s->v.FunctionDef.typeparams);
12161250
}
12171251
if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,

0 commit comments

Comments
 (0)
0