@@ -2156,19 +2156,19 @@ compiler_type_params(struct compiler *c, asdl_typeparam_seq *typeparams)
2156
2156
ADDOP_I (c , loc , CALL_INTRINSIC_1 , INTRINSIC_TYPEVAR );
2157
2157
}
2158
2158
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 ) );
2160
2160
break ;
2161
2161
case TypeVarTuple_kind :
2162
2162
ADDOP_LOAD_CONST (c , loc , typeparam -> v .TypeVarTuple .name );
2163
2163
ADDOP_I (c , loc , CALL_INTRINSIC_1 , INTRINSIC_TYPEVARTUPLE );
2164
2164
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 ) );
2166
2166
break ;
2167
2167
case ParamSpec_kind :
2168
2168
ADDOP_LOAD_CONST (c , loc , typeparam -> v .ParamSpec .name );
2169
2169
ADDOP_I (c , loc , CALL_INTRINSIC_1 , INTRINSIC_PARAMSPEC );
2170
2170
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 ) );
2172
2172
break ;
2173
2173
}
2174
2174
}
@@ -2346,6 +2346,31 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2346
2346
return compiler_nameop (c , loc , name , Store );
2347
2347
}
2348
2348
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
+
2349
2374
static int
2350
2375
compiler_class (struct compiler * c , stmt_ty s )
2351
2376
{
@@ -2378,7 +2403,6 @@ compiler_class(struct compiler *c, stmt_ty s)
2378
2403
RETURN_IF_ERROR (compiler_type_params (c , typeparams ));
2379
2404
_Py_DECLARE_STR (type_params , ".type_params" );
2380
2405
RETURN_IF_ERROR (compiler_nameop (c , loc , & _Py_STR (type_params ), Store ));
2381
- //ADDOP(c, loc, POP_TOP);
2382
2406
}
2383
2407
2384
2408
/* ultimately generate code for:
@@ -2417,6 +2441,12 @@ compiler_class(struct compiler *c, stmt_ty s)
2417
2441
compiler_exit_scope (c );
2418
2442
return ERROR ;
2419
2443
}
2444
+ if (typeparams ) {
2445
+ if (!compiler_set_type_params_in_class (c , loc , typeparams )) {
2446
+ compiler_exit_scope (c );
2447
+ return ERROR ;
2448
+ }
2449
+ }
2420
2450
/* compile the body proper */
2421
2451
if (compiler_body (c , loc , s -> v .ClassDef .body ) < 0 ) {
2422
2452
compiler_exit_scope (c );
0 commit comments