Skip to content

Commit fa81c01

Browse files
committed
set __type_variables__ on classes
1 parent b5de372 commit fa81c01

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

Python/compile.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,19 +2156,19 @@ compiler_type_params(struct compiler *c, asdl_typeparam_seq *typeparams)
21562156
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVAR);
21572157
}
21582158
ADDOP_I(c, loc, COPY, 1);
2159-
compiler_nameop(c, loc, typeparam->v.TypeVar.name, Store);
2159+
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVar.name, Store));
21602160
break;
21612161
case TypeVarTuple_kind:
21622162
ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVarTuple.name);
21632163
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVARTUPLE);
21642164
ADDOP_I(c, loc, COPY, 1);
2165-
compiler_nameop(c, loc, typeparam->v.TypeVarTuple.name, Store);
2165+
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVarTuple.name, Store));
21662166
break;
21672167
case ParamSpec_kind:
21682168
ADDOP_LOAD_CONST(c, loc, typeparam->v.ParamSpec.name);
21692169
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_PARAMSPEC);
21702170
ADDOP_I(c, loc, COPY, 1);
2171-
compiler_nameop(c, loc, typeparam->v.ParamSpec.name, Store);
2171+
RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.ParamSpec.name, Store));
21722172
break;
21732173
}
21742174
}
@@ -2346,6 +2346,31 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
23462346
return compiler_nameop(c, loc, name, Store);
23472347
}
23482348

2349+
static int
2350+
compiler_set_type_params_in_class(struct compiler *c, location loc, asdl_typeparam_seq *typeparams)
2351+
{
2352+
Py_ssize_t count = asdl_seq_LEN(typeparams);
2353+
for (Py_ssize_t i = 0; i < count; i++) {
2354+
typeparam_ty typeparam = asdl_seq_GET(typeparams, i);
2355+
PyObject *name;
2356+
switch(typeparam->kind) {
2357+
case TypeVar_kind:
2358+
name = typeparam->v.TypeVar.name;
2359+
break;
2360+
case TypeVarTuple_kind:
2361+
name = typeparam->v.TypeVarTuple.name;
2362+
break;
2363+
case ParamSpec_kind:
2364+
name = typeparam->v.ParamSpec.name;
2365+
break;
2366+
}
2367+
RETURN_IF_ERROR(compiler_nameop(c, loc, name, Load));
2368+
}
2369+
ADDOP_I(c, loc, BUILD_TUPLE, count);
2370+
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_ID(__type_variables__), Store));
2371+
return 1;
2372+
}
2373+
23492374
static int
23502375
compiler_class(struct compiler *c, stmt_ty s)
23512376
{
@@ -2378,7 +2403,6 @@ compiler_class(struct compiler *c, stmt_ty s)
23782403
RETURN_IF_ERROR(compiler_type_params(c, typeparams));
23792404
_Py_DECLARE_STR(type_params, ".type_params");
23802405
RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_STR(type_params), Store));
2381-
//ADDOP(c, loc, POP_TOP);
23822406
}
23832407

23842408
/* ultimately generate code for:
@@ -2417,6 +2441,12 @@ compiler_class(struct compiler *c, stmt_ty s)
24172441
compiler_exit_scope(c);
24182442
return ERROR;
24192443
}
2444+
if (typeparams) {
2445+
if (!compiler_set_type_params_in_class(c, loc, typeparams)) {
2446+
compiler_exit_scope(c);
2447+
return ERROR;
2448+
}
2449+
}
24202450
/* compile the body proper */
24212451
if (compiler_body(c, loc, s->v.ClassDef.body) < 0) {
24222452
compiler_exit_scope(c);

0 commit comments

Comments
 (0)