8000 start class support · python/cpython@8505134 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8505134

Browse files
committed
start class support
1 parent 21e1b1a commit 8505134

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

Python/compile.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,24 @@ compiler_class(struct compiler *c, stmt_ty s)
23132313
if (asdl_seq_LEN(decos)) {
23142314
firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
23152315
}
2316+
location loc = LOC(s);
2317+
2318+
asdl_typeparam_seq *typeparams = s->v.ClassDef.typeparams;
2319+
if (typeparams) {
2320+
ADDOP(c, loc, PUSH_NULL);
2321+
PyObject *typeparams_name = PyUnicode_FromFormat("<generic parameters of %U>",
2322+
s->v.ClassDef.name);
2323+
if (!typeparams_name) {
2324+
return ERROR;
2325+
}
2326+
if (compiler_enter_scope(c, typeparams_name, COMPILER_SCOPE_FUNCTION,
2327+
(void *)typeparams, firstlineno) == -1) {
2328+
Py_DECREF(typeparams_name);
2329+
return ERROR;
2330+
}
2331+
Py_DECREF(typeparams_name);
2332+
RETURN_IF_ERROR(compiler_type_params(c, typeparams));
2333+
}
23162334

23172335
/* ultimately generate code for:
23182336
<name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
@@ -2387,7 +2405,6 @@ compiler_class(struct compiler *c, stmt_ty s)
23872405
return ERROR;
23882406
}
23892407

2390-
location loc = LOC(s);
23912408
/* 2. load the 'build_class' function */
23922409
ADDOP(c, loc, PUSH_NULL);
23932410
ADDOP(c, loc, LOAD_BUILD_CLASS);
@@ -2407,6 +2424,20 @@ compiler_class(struct compiler *c, stmt_ty s)
24072424
s->v.ClassDef.bases,
24082425
s->v.ClassDef.keywords));
24092426

2427+
if (typeparams) {
2428+
PyCodeObject *co = assemble(c, 0);
2429+
compiler_exit_scope(c);
2430+
if (co == NULL) {
2431+
return ERROR;
2432+
}
2433+
if (compiler_make_closure(c, loc, co, 0) < 0) {
2434+
Py_DECREF(co);
2435+
return ERROR;
2436+
}
2437+
Py_DECREF(co);
2438+
ADDOP_I(c, loc, CALL, 0);
2439+
}
2440+
24102441
/* 6. apply decorators */
24112442
RETURN_IF_ERROR(compiler_apply_decorators(c, decos));
24122443

Python/symtable.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,18 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
12661266
PyObject *tmp;
12671267
if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL, LOCATION(s)))
12681268
VISIT_QUIT(st, 0);
1269-
VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1270-
VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
12711269
if (s->v.ClassDef.decorator_list)
12721270
VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1271+
if (s->v.ClassDef.typeparams) {
1272+
if (!symtable_enter_block(st, s->v.ClassDef.name,
1273+
FunctionBlock, (void *)s->v.ClassDef.typeparams,
1274+
LOCATION(s))) {
1275+
VISIT_QUIT(st, 0);
1276+
}
1277+
VISIT_SEQ(st, typeparam, s->v.ClassDef.typeparams);
1278+
}
1279+
VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1280+
VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
12731281
if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
12741282
(void *)s, s->lineno, s->col_offset,
12751283
s->end_lineno, s->end_col_offset))
@@ -1280,6 +1288,10 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
12801288
st->st_private = tmp;
12811289
if (!symtable_exit_block(st))
12821290
VISIT_QUIT(st, 0);
1291+
if (s->v.ClassDef.typeparams) {
1292+
if (!symtable_exit_block(st))
1293+
VISIT_QUIT(st, 0);
1294+
}
12831295
break;
12841296
}
12851297
case Return_kind:

0 commit comments

Comments
 (0)
0