@@ -180,11 +180,22 @@ tuple_extend(PyObject **dst, Py_ssize_t dstindex,
180180PyObject *
181181_Py_make_parameters (PyObject * args )
182182{
183+ assert (PyTuple_Check (args ) || PyList_Check (args ));
184+ const bool is_args_list = PyList_Check (args );
185+ PyObject * tuple_args = NULL ;
186+ if (is_args_list ) {
187+ args = tuple_args = PySequence_Tuple (args );
188+ if (args == NULL ) {
189+ return NULL ;
190+ }
191+ }
183192 Py_ssize_t nargs = PyTuple_GET_SIZE (args );
184193 Py_ssize_t len = nargs ;
185194 PyObject * parameters = PyTuple_New (len );
186- if (parameters == NULL )
195+ if (parameters == NULL ) {
196+ Py_XDECREF (tuple_args );
187197 return NULL ;
198+ }
188199 Py_ssize_t iparam = 0 ;
189200 for (Py_ssize_t iarg = 0 ; iarg < nargs ; iarg ++ ) {
190201 PyObject * t = PyTuple_GET_ITEM (args , iarg );
@@ -195,6 +206,7 @@ _Py_make_parameters(PyObject *args)
195206 int rc = PyObject_HasAttrWithError (t , & _Py_ID (__typing_subst__ ));
196207 if (rc < 0 ) {
197208 Py_DECREF (parameters );
209+ Py_XDECREF (tuple_args );
198210 return NULL ;
199211 }
200212 if (rc ) {
@@ -205,8 +217,19 @@ _Py_make_parameters(PyObject *args)
205217 if (PyObject_GetOptionalAttr (t , & _Py_ID (__parameters__ ),
206218 & subparams ) < 0 ) {
207219 Py_DECREF (parameters );
220+ Py_XDECREF (tuple_args );
208221 return NULL ;
209222 }
223+ if (!subparams && (PyTuple_Check (t ) || PyList_Check (t ))) {
224+ // Recursively call _Py_make_parameters for lists/tuples and
225+ // add the results to the current parameters.
226+ subparams = _Py_make_parameters (t );
227+ if (subparams == NULL ) {
228+ Py_DECREF (parameters );
229+ Py_XDECREF (tuple_args );
230+ return NULL ;
231+ }
232+ }
210233 if (subparams && PyTuple_Check (subparams )) {
211234 Py_ssize_t len2 = PyTuple_GET_SIZE (subparams );
212235 Py_ssize_t needed = len2 - 1 - (iarg - iparam );
@@ -215,6 +238,7 @@ _Py_make_parameters(PyObject *args)
215238 if (_PyTuple_Resize (& parameters , len ) < 0 ) {
216239 Py_DECREF (subparams );
217240 Py_DECREF (parameters );
241+ Py_XDECREF (tuple_args );
218242 return NULL ;
219243 }
220244 }
@@ -229,9 +253,11 @@ _Py_make_parameters(PyObject *args)
229253 if (iparam < len ) {
230254 if (_PyTuple_Resize (& parameters , iparam ) < 0 ) {
231255 Py_XDECREF (parameters );
256+ Py_XDECREF (tuple_args );
232257 return NULL ;
233258 }
234259 }
260+ Py_XDECREF (tuple_args );
235261 return parameters ;
236262}
237263
@@ -416,11 +442,22 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
416442 t = list[T]; t[int] -> newargs = [int]
417443 t = dict[str, T]; t[int] -> newargs = [str, int]
418444 t = dict[T, list[S]]; t[str, int] -> newargs = [str, list[int]]
445+ t = list[[T]]; t[str] -> newargs = [[str]]
419446 */
447+ assert (PyTuple_Check (args ) || PyList_Check (args ));
448+ const bool is_args_list = PyList_Check (args );
449+ PyObject * tuple_args = NULL ;
450+ if (is_args_list ) {
451+ args = tuple_args = PySequence_Tuple (args );
452+ if (args == NULL ) {
453+ return NULL ;
454+ }
455+ }
420456 Py_ssize_t nargs = PyTuple_GET_SIZE (args );
421457 PyObject * newargs = PyTuple_New (nargs );
422458 if (newargs == NULL ) {
423459 Py_DECREF (item );
460+ Py_XDECREF (tuple_args );
424461 return NULL ;
425462 }
426463 for (Py_ssize_t iarg = 0 , jarg = 0 ; iarg < nargs ; iarg ++ ) {
@@ -430,17 +467,46 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
430467 jarg ++ ;
431468 continue ;
432469 }
433-
470+ // Recursively substitute params in lists/tuples.
471+ if (PyTuple_Check (arg ) || PyList_Check (arg )) {
472+ PyObject * subargs = _Py_subs_parameters (self , arg , parameters , item );
473+ if (subargs == NULL ) {
474+ Py_DECREF (newargs );
475+ Py_DECREF (item );
476+ Py_XDECREF (tuple_args );
477+ return NULL ;
478+ }
479+ if (PyTuple_Check (arg )) {
480+ PyTuple_SET_ITEM (newargs , jarg , subargs );
481+ }
482+ else {
483+ // _Py_subs_parameters returns a tuple. If the original arg was a list,
484+ // convert subargs to a list as well.
485+ PyObject * subargs_list = PySequence_List (subargs );
486+ Py_DECREF (subargs );
487+ if (subargs_list == NULL ) {
488+ Py_DECREF (newargs );
489+ Py_DECREF (item );
490+ Py_XDECREF (tuple_args );
491+ return NULL ;
492+ }
493+ PyTuple_SET_ITEM (newargs , jarg , subargs_list );
494+ }
495+ jarg ++ ;
496+ continue ;
497+ }
434498 int unpack = _is_unpacked_typevartuple (arg );
435499 if (unpack < 0 ) {
436500 Py_DECREF (newargs );
437501 Py_DECREF (item );
502+ Py_XDECREF (tuple_args );
438503 return NULL ;
439504 }
440505 PyObject * subst ;
441506 if (PyObject_GetOptionalAttr (arg , & _Py_ID (__typing_subst__ ), & subst ) < 0 ) {
442507 Py_DECREF (newargs );
443508 Py_DECREF (item );
509+ Py_XDECREF (tuple_args );
444510 return NULL ;
445511 }
446512 if (subst ) {
@@ -455,6 +521,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
455521 if (arg == NULL ) {
456522 Py_DECREF (newargs );
457523 Py_DECREF (item );
524+ Py_XDECREF (tuple_args );
458525 return NULL ;
459526 }
460527 if (unpack ) {
@@ -463,6 +530,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
463530 Py_DECREF (arg );
464531 if (jarg < 0 ) {
465532 Py_DECREF (item );
533+ Py_XDECREF (tuple_args );
466534 return NULL ;
467535 }
468536 }
@@ -473,6 +541,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
473541 }
474542
475543 Py_DECREF (item );
544+ Py_XDECREF (tuple_args );
476545 return newargs ;
477546}
478547
0 commit comments