@@ -198,8 +198,8 @@ tuple_add(PyObject *self, Py_ssize_t len, PyObject *item)
198
198
return 0 ;
199
199
}
200
200
201
- static PyObject *
202
- make_parameters (PyObject * args )
201
+ PyObject *
202
+ _Py_make_parameters (PyObject * args )
203
203
{
204
204
Py_ssize_t nargs = PyTuple_GET_SIZE (args );
205
205
Py_ssize_t len = nargs ;
@@ -294,18 +294,10 @@ subs_tvars(PyObject *obj, PyObject *params, PyObject **argitems)
294
294
return obj ;
295
295
}
296
296
297
- static PyObject *
298
- ga_getitem (PyObject * self , PyObject * item )
297
+ PyObject *
298
+ _Py_subs_parameters (PyObject * self , PyObject * args , PyObject * parameters , PyObject * item )
299
299
{
300
- gaobject * alias = (gaobject * )self ;
301
- // do a lookup for __parameters__ so it gets populated (if not already)
302
- if (alias -> parameters == NULL ) {
303
- alias -> parameters = make_parameters (alias -> args );
304
- if (alias -> parameters == NULL ) {
305
- return NULL ;
306
- }
307
- }
308
- Py_ssize_t nparams = PyTuple_GET_SIZE (alias -> parameters );
300
+ Py_ssize_t nparams = PyTuple_GET_SIZE (parameters );
309
301
if (nparams == 0 ) {
310
302
return PyErr_Format (PyExc_TypeError ,
311
303
"There are no type variables left in %R" ,
@@ -320,32 +312,32 @@ ga_getitem(PyObject *self, PyObject *item)
320
312
nitems > nparams ? "many" : "few" ,
321
313
self );
322
314
}
323
- /* Replace all type variables (specified by alias-> parameters)
315
+ /* Replace all type variables (specified by parameters)
324
316
with corresponding values specified by argitems.
325
317
t = list[T]; t[int] -> newargs = [int]
326
318
t = dict[str, T]; t[int] -> newargs = [str, int]
327
319
t = dict[T, list[S]]; t[str, int] -> newargs = [str, list[int]]
328
320
*/
329
- Py_ssize_t nargs = PyTuple_GET_SIZE (alias -> args );
321
+ Py_ssize_t nargs = PyTuple_GET_SIZE (args );
330
322
PyObject * newargs = PyTuple_New (nargs );
331
323
if (newargs == NULL ) {
332
324
return NULL ;
333
325
}
334
326
for (Py_ssize_t iarg = 0 ; iarg < nargs ; iarg ++ ) {
335
- PyObject * arg = PyTuple_GET_ITEM (alias -> args , iarg );
327
+ PyObject * arg = PyTuple_GET_ITEM (args , iarg );
336
328
int typevar = is_typevar (arg );
337
329
if (typevar < 0 ) {
338
330
Py_DECREF (newargs );
339
331
return NULL ;
340
332
}
341
333
if (typevar ) {
342
- Py_ssize_t iparam = tuple_index (alias -> parameters , nparams , arg );
334
+ Py_ssize_t iparam = tuple_index (parameters , nparams , arg );
343
335
assert (iparam >= 0 );
344
336
arg = argitems [iparam ];
345
337
Py_INCREF (arg );
346
338
}
347
339
else {
348
- arg = subs_tvars (arg , alias -> parameters , argitems );
340
+ arg = subs_tvars (arg , parameters , argitems );
349
341
if (arg == NULL ) {
350
342
Py_DECREF (newargs );
351
343
return NULL ;
@@ -354,6 +346,26 @@ ga_getitem(PyObject *self, PyObject *item)
354
346
PyTuple_SET_ITEM (newargs , iarg , arg );
355
347
}
356
348
349
+ return newargs ;
350
+ }
351
+
352
+ static PyObject *
353
+ ga_getitem (PyObject * self , PyObject * item )
354
+ {
355
+ gaobject * alias = (gaobject * )self ;
356
+ // Populate __parameters__ if needed.
357
+ if (alias -> parameters == NULL ) {
358
+ alias -> parameters = _Py_make_parameters (alias -> args );
359
+ if (alias -> parameters == NULL ) {
360
+ return NULL ;
361
+ }
362
+ }
363
+
364
+ PyObject * newargs = _Py_subs_parameters (self , alias -> args , alias -> parameters , item );
365
+ if (newargs == NULL ) {
366
+ return NULL ;
367
+ }
368
+
357
369
PyObject * res = Py_GenericAlias (alias -> origin , newargs );
358
370
359
371
Py_DECREF (newargs );
@@ -550,7 +562,7 @@ ga_parameters(PyObject *self, void *unused)
550
562
{
551
563
gaobject * alias = (gaobject * )self ;
552
564
if (alias -> parameters == NULL ) {
553
- alias -> parameters = make_parameters (alias -> args );
565
+ alias -> parameters = _Py_make_parameters (alias -> args );
554
566
if (alias -> parameters == NULL ) {
555
567
return NULL ;
556
568
}
0 commit comments