Skip to content

Commit 896c5ba

Browse files
committed
Add test code for non Py_TPFLAGS_PREHEADER case
1 parent 15ac413 commit 896c5ba

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎Modules/_testcapi/mem.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,46 @@ test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
342342
return test_setallocators(PYMEM_DOMAIN_OBJ);
343343
}
344344

345+
static PyObject *
346+
test_pyobject_gc(PyObject *self, PyObject *Py_UNUSED(ignored))
347+
{
348+
PyTupleObject *obj = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, 3);
349+
obj->ob_item[0] = Py_NewRef(Py_None);
350+
obj->ob_item[1] = Py_NewRef(Py_None);
351+
obj->ob_item[2] = Py_NewRef(Py_None);
352+
353+
if (obj == NULL) {
354+
goto alloc_failed;
355+
}
356+
if (Py_SIZE(obj) != 3) {
357+
Py_DECREF(obj);
358+
PyErr_SetString(PyExc_RuntimeError, "Invalid Py_SIZE(obj)");
359+
return NULL;
360+
}
361+
362+
PyTupleObject *new_obj = PyObject_GC_Resize(PyTupleObject, obj, 6);
363+
if (new_obj == NULL) {
364+
Py_DECREF(obj);
365+
goto alloc_failed;
366+
}
367+
368+
new_obj->ob_item[3] = Py_NewRef(Py_None);
369+
new_obj->ob_item[4] = Py_NewRef(Py_None);
370+
new_obj->ob_item[5] = Py_NewRef(Py_None);
371+
if (Py_SIZE(new_obj) != 6) {
372+
Py_DECREF(new_obj);
373+
PyErr_SetString(PyExc_RuntimeError, "Invalid Py_SIZE(new_obj)");
374+
return NULL;
375+
}
376+
377+
Py_DECREF(new_obj);
378+
Py_RETURN_NONE;
379+
380+
alloc_failed:
381+
PyErr_NoMemory();
382+
return NULL;
383+
}
384+
345385
static PyObject *
346386
test_pyobject_new(PyObject *self, PyObject *Py_UNUSED(ignored))
347387
{
@@ -693,6 +733,7 @@ static PyMethodDef test_methods[] = {
693733
{"test_pymem_setrawallocators", test_pymem_setrawallocators, METH_NOARGS},
694734
{"test_pyobject_new", test_pyobject_new, METH_NOARGS},
695735
{"test_pyobject_setallocators", test_pyobject_setallocators, METH_NOARGS},
736+
{"test_pyobject_gc", test_pyobject_gc, METH_NOARGS},
696737

697738
// Tracemalloc tests
698739
{"tracemalloc_track", tracemalloc_track, METH_VARARGS},

0 commit comments

Comments
 (0)