@@ -269,9 +269,7 @@ _Py_make_parameters(PyObject *args)
269269 a non-empty tuple, return a new reference to obj. */
270270static PyObject *
271271subs_tvars (PyObject * obj , PyObject * params ,
272- PyObject * * argitems , Py_ssize_t nargs ,
273- Py_ssize_t varparam , Py_ssize_t left , Py_ssize_t right ,
274- PyObject * fillarg )
272+ PyObject * * argitems , Py_ssize_t nargs )
275273{
276274 PyObject * subparams ;
277275 if (_PyObject_LookupAttr (obj , & _Py_ID (__parameters__ ), & subparams ) < 0 ) {
@@ -289,30 +287,22 @@ subs_tvars(PyObject *obj, PyObject *params,
289287 for (Py_ssize_t i = 0 ; i < nsubargs ; ++ i ) {
290288 PyObject * arg = PyTuple_GET_ITEM (subparams , i );
291289 Py_ssize_t iparam = tuple_index (params , nparams , arg );
292- if (iparam == varparam ) {
293- j = tuple_extend (& subargs , j ,
294- argitems + left , nargs - left - right );
295- if (j < 0 ) {
296- return NULL ;
297- }
298- }
299- else {
300- if (iparam >= 0 ) {
301- if (iparam < left ) {
302- arg = argitems [iparam ];
303- }
304- else if (iparam >= nparams - right ) {
305- iparam += nargs - nparams ;
306- arg = argitems [iparam ];
307- }
308- else {
309- arg = fillarg ;
290+ if (iparam >= 0 ) {
291+ PyObject * param = PyTuple_GET_ITEM (params , iparam );
292+ arg = argitems [iparam ];
293+ if (Py_TYPE (param )-> tp_iter && PyTuple_Check (arg )) { // TypeVarTuple
294+ j = tuple_extend (& subargs , j ,
295+ & PyTuple_GET_ITEM (arg , 0 ),
296+ PyTuple_GET_SIZE (arg ));
297+ if (j < 0 ) {
298+ return NULL ;
310299 }
300+ continue ;
311301 }
312- Py_INCREF (arg );
313- PyTuple_SET_ITEM (subargs , j , arg );
314- j ++ ;
315302 }
303+ Py_INCREF (arg );
304+ PyTuple_SET_ITEM (subargs , j , arg );
305+ j ++ ;
316306 }
317307 assert (j == PyTuple_GET_SIZE (subargs ));
318308
@@ -409,27 +399,6 @@ _unpack_args(PyObject *item)
409399 return newargs ;
410400}
411401
412- static PyObject *
413- _get_unpacked_var_tuple_arg (PyObject * arg )
414- {
415- if (PyType_Check (arg )) {
416- return NULL ;
417- }
418- PyObject * subargs = _unpacked_tuple_args (arg );
419- if (subargs != NULL &&
420- PyTuple_Check (subargs ) &&
421- PyTuple_GET_SIZE (subargs ) == 2 &&
422- PyTuple_GET_ITEM (subargs , 1 ) == Py_Ellipsis )
423- {
424- PyObject * subarg = PyTuple_GET_ITEM (subargs , 0 );
425- Py_INCREF (subarg );
426- Py_DECREF (subargs );
427- return subarg ;
428- }
429- Py_XDECREF (subargs );
430- return NULL ;
431- }
432-
433402PyObject *
434403_Py_subs_parameters (PyObject * self , PyObject * args , PyObject * parameters , PyObject * item )
435404{
@@ -440,67 +409,36 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
440409 self );
441410 }
442411 item = _unpack_args (item );
443- int is_tuple = PyTuple_Check (item );
444- Py_ssize_t nitems = is_tuple ? PyTuple_GET_SIZE (item ) : 1 ;
445- PyObject * * argitems = is_tuple ? & PyTuple_GET_ITEM (item , 0 ) : & item ;
446- Py_ssize_t varparam = nparams ;
447412 for (Py_ssize_t i = 0 ; i < nparams ; i ++ ) {
448413 PyObject * param = PyTuple_GET_ITEM (parameters , i );
449- if (Py_TYPE (param )-> tp_iter ) { // TypeVarTuple
450- if (varparam < nparams ) {
451- Py_DECREF (item );
452- return PyErr_Format (PyExc_TypeError ,
453- "More than one TypeVarTuple parameter in %S" ,
454- self );
455- }
456- varparam = i ;
414+ PyObject * prepare , * tmp ;
415+ if (_PyObject_LookupAttr (param , & _Py_ID (__typing_prepare_subst__ ), & prepare ) < 0 ) {
416+ Py_DECREF (item );
417+ return NULL ;
457418 }
458- }
459- PyObject * fillarg = NULL ;
460- Py_ssize_t vartuplearg = nitems ;
461- Py_ssize_t left = varparam ;
462- Py_ssize_t right = nparams - varparam - 1 ;
463- if (varparam < nparams ) {
464- for (Py_ssize_t i = 0 ; i < nitems ; i ++ ) {
465- PyObject * arg = _get_unpacked_var_tuple_arg (argitems [i ]);
466- if (arg ) {
467- if (vartuplearg < nitems ) {
468- Py_DECREF (arg );
469- Py_DECREF (fillarg );
470- Py_DECREF (item );
471- return PyErr_Format (PyExc_TypeError ,
472- "More than one unpacked arbitrary-length tuple argument" ,
473- self );
474- }
475- vartuplearg = i ;
476- fillarg = arg ;
419+ if (prepare && prepare != Py_None ) {
420+ if (PyTuple_Check (item )) {
421+ tmp = PyObject_CallFunction (prepare , "OO" , self , item );
477422 }
478- else if (PyErr_Occurred ()) {
479- Py_XDECREF (fillarg );
480- Py_DECREF (item );
423+ else {
424+ tmp = PyObject_CallFunction (prepare , "O(O)" , self , item );
425+ }
426+ Py_DECREF (prepare );
427+ Py_SETREF (item , tmp );
428+ if (item == NULL ) {
481429 return NULL ;
482430 }
483431 }
484- if (vartuplearg < nitems ) {
485- assert (fillarg );
486- left = Py_MIN (left , vartuplearg );
487- right = Py_MIN (right , nitems - vartuplearg - 1 );
488- }
489- else if (left + right > nitems ) {
490- Py_DECREF (item );
491- return PyErr_Format (PyExc_TypeError ,
492- "Too few arguments for %R" ,
493- self );
494- }
495432 }
496- else {
497- if (nitems != nparams ) {
498- Py_DECREF (item );
499- return PyErr_Format (PyExc_TypeError ,
500- "Too %s arguments for %R; actual %zd, expected %zd" ,
501- nitems > nparams ? "many" : "few" ,
502- self , nitems , nparams );
503- }
433+ int is_tuple = PyTuple_Check (item );
434+ Py_ssize_t nitems = is_tuple ? PyTuple_GET_SIZE (item ) : 1 ;
435+ PyObject * * argitems = is_tuple ? & PyTuple_GET_ITEM (item , 0 ) : & item ;
436+ if (nitems != nparams ) {
437+ Py_DECREF (item );
438+ return PyErr_Format (PyExc_TypeError ,
439+ "Too %s arguments for %R; actual %zd, expected %zd" ,
440+ nitems > nparams ? "many" : "few" ,
441+ self , nitems , nparams );
504442 }
505443 /* Replace all type variables (specified by parameters)
506444 with corresponding values specified by argitems.
@@ -511,7 +449,6 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
511449 Py_ssize_t nargs = PyTuple_GET_SIZE (args );
512450 PyObject * newargs = PyTuple_New (nargs );
513451 if (newargs == NULL ) {
514- Py_XDECREF (fillarg );
515452 Py_DECREF (item );
516453 return NULL ;
517454 }
@@ -520,50 +457,26 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
520457 int unpack = _is_unpacked_typevartuple (arg );
521458 if (unpack < 0 ) {
522459 Py_DECREF (newargs );
523- Py_XDECREF (fillarg );
524460 Py_DECREF (item );
525461 return NULL ;
526462 }
527463 PyObject * subst ;
528464 if (_PyObject_LookupAttr (arg , & _Py_ID (__typing_subst__ ), & subst ) < 0 ) {
529465 Py_DECREF (newargs );
530- Py_XDECREF (fillarg );
531466 Py_DECREF (item );
532467 return NULL ;
533468 }
534469 if (subst ) {
535470 Py_ssize_t iparam = tuple_index (parameters , nparams , arg );
536471 assert (iparam >= 0 );
537- if (iparam == varparam ) {
538- Py_DECREF (subst );
539- Py_DECREF (newargs );
540- Py_XDECREF (fillarg );
541- Py_DECREF (item );
542- PyErr_SetString (PyExc_TypeError ,
543- "Substitution of bare TypeVarTuple is not supported" );
544- return NULL ;
545- }
546- if (iparam < left ) {
547- arg = argitems [iparam ];
548- }
549- else if (iparam >= nparams - right ) {
550- iparam += nitems - nparams ;
551- arg = argitems [iparam ];
552- }
553- else {
554- assert (fillarg );
555- arg = fillarg ;
556- }
557- arg = PyObject_CallOneArg (subst , arg );
472+ arg = PyObject_CallOneArg (subst , argitems [iparam ]);
558473 Py_DECREF (subst );
559474 }
560475 else {
561- arg = subs_tvars (arg , parameters , argitems , nitems ,
562- varparam , left , right , fillarg );
476+ arg = subs_tvars (arg , parameters , argitems , nitems );
563477 }
564478 if (arg == NULL ) {
565479 Py_DECREF (newargs );
566- Py_XDECREF (fillarg );
567480 Py_DECREF (item );
568481 return NULL ;
569482 }
@@ -572,7 +485,6 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
572485 & PyTuple_GET_ITEM (arg , 0 ), PyTuple_GET_SIZE (arg ));
573486 Py_DECREF (arg );
574487 if (jarg < 0 ) {
575- Py_XDECREF (fillarg );
576488 Py_DECREF (item );
577489 return NULL ;
578490 }
@@ -583,7 +495,6 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
583495 }
584496 }
585497
586- Py_XDECREF (fillarg );
587498 Py_DECREF (item );
588499 return newargs ;
589500}
0 commit comments