@@ -500,6 +500,42 @@ pytype_getbasebytoken(PyObject *self, PyObject *args)
500
500
Py_XDECREF (result );
501
501
return NULL ;
502
502
}
503
+
504
+ static PyObject *
505
+ repeat_getbasebytoken (PyObject * self , PyObject * args )
506
+ {
507
+ PyTypeObject * type ;
508
+ PyObject * py_token , * repeat , * use_mro ;
509
+ if (!PyArg_ParseTuple (args , "OOOO" , & type , & py_token , & repeat , & use_mro )) {
510
+ return NULL ;
511
+ }
512
+ assert (PyType_Check (type ));
513
+
514
+ PyObject * mro_save = type -> tp_mro ;
515
+ if (use_mro != Py_True ) {
516
+ type -> tp_mro = NULL ;
517
+ }
518
+
519
+ void * token = PyLong_AsVoidPtr (py_token );
520
+ int n = PyLong_AsLong (repeat );
521
+ int found = 0 ;
522
+ PyObject * result ;
523
+ for (int i = 0 ; i < n ; i ++ ) {
524
+ if (PyType_GetBaseByToken (type , token , (PyTypeObject * * )& result ) < 0 ) {
525
+ found = -1 ;
526
+ break ;
527
+ }
528
+ if (result ) {
529
+ Py_DECREF (result );
530
+ found = 1 ;
531
+ }
532
+ }
533
+
534
+ type -> tp_mro = mro_save ;
535
+ if (!found ) {
536
+ PyErr_SetString (PyExc_ValueError , "token not found" );
537
+ }
538
+ return found == 1 ? Py_None : NULL ;
503
539
}
504
540
505
541
@@ -519,6 +555,7 @@ static PyMethodDef TestMethods[] = {
519
555
{"create_type_with_token" , create_type_with_token , METH_VARARGS },
520
556
{"get_tp_token" , get_tp_token , METH_O },
521
557
{"pytype_getbasebytoken" , pytype_getbasebytoken , METH_VARARGS },
558
+ {"repeat_getbasebytoken" , repeat_getbasebytoken , METH_VARARGS },
522
559
{NULL },
523
560
};
524
561
0 commit comments