Skip to content

Commit 100972d

Browse files
committed
add a repeat test (temporary use)
1 parent 7d3f8b6 commit 100972d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Modules/_testcapi/heaptype.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,42 @@ pytype_getbasebytoken(PyObject *self, PyObject *args)
500500
Py_XDECREF(result);
501501
return NULL;
502502
}
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;
503539
}
504540

505541

@@ -519,6 +555,7 @@ static PyMethodDef TestMethods[] = {
519555
{"create_type_with_token", create_type_with_token, METH_VARARGS},
520556
{"get_tp_token", get_tp_token, METH_O},
521557
{"pytype_getbasebytoken", pytype_getbasebytoken, METH_VARARGS},
558+
{"repeat_getbasebytoken", repeat_getbasebytoken, METH_VARARGS},
522559
{NULL},
523560
};
524561

0 commit comments

Comments
 (0)