Skip to content

Commit 252c6bd

Browse files
Add tests
1 parent 76e7aa7 commit 252c6bd

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Lib/test/test_capi/test_type.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,10 @@ def test_extension_managed_dict_type(self):
274274
obj.__dict__ = {'bar': 3}
275275
self.assertEqual(obj.__dict__, {'bar': 3})
276276
self.assertEqual(obj.bar, 3)
277+
278+
def test_extension_managed_weakref_nogc_type(self):
279+
msg = ("type _testcapi.ManagedWeakrefNoGCType "
280+
"has the Py_TPFLAGS_MANAGED_WEAKREF "
281+
"flag but not Py_TPFLAGS_HAVE_GC flag")
282+
with self.assertRaisesRegex(SystemError, msg):
283+
_testcapi.create_managed_weakref_nogc_type()

Modules/_testcapimodule.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,39 @@ toggle_reftrace_printer(PyObject *ob, PyObject *arg)
25622562
Py_RETURN_NONE;
25632563
}
25642564

2565+
2566+
typedef struct {
2567+
PyObject_HEAD
2568+
} ManagedWeakrefNoGCObject;
2569+
2570+
static void
2571+
ManagedWeakrefNoGC_dealloc(PyObject *self)
2572+
{
2573+
PyObject_ClearWeakRefs(self);
2574+
PyTypeObject *tp = Py_TYPE(self);
2575+
tp->tp_free(self);
2576+
Py_DECREF(tp);
2577+
}
2578+
2579+
static PyType_Slot ManagedWeakrefNoGC_slots[] = {
2580+
{Py_tp_dealloc, ManagedWeakrefNoGC_dealloc},
2581+
{0, 0}
2582+
};
2583+
2584+
static PyType_Spec ManagedWeakrefNoGC_spec = {
2585+
.name = "_testcapi.ManagedWeakrefNoGCType",
2586+
.basicsize = sizeof(ManagedWeakrefNoGCObject),
2587+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_MANAGED_WEAKREF),
2588+
.slots = ManagedWeakrefNoGC_slots,
2589+
};
2590+
2591+
static PyObject *
2592+
test_create_managed_weakref_nogc_type(PyObject *self, PyObject *Py_UNUSED(args))
2593+
{
2594+
return PyType_FromSpec(&ManagedWeakrefNoGC_spec);
2595+
}
2596+
2597+
25652598
static PyMethodDef TestMethods[] = {
25662599
{"set_errno", set_errno, METH_VARARGS},
25672600
{"test_config", test_config, METH_NOARGS},
@@ -2656,6 +2689,8 @@ static PyMethodDef TestMethods[] = {
26562689
{"test_atexit", test_atexit, METH_NOARGS},
26572690
{"code_offset_to_line", _PyCFunction_CAST(code_offset_to_line), METH_FASTCALL},
26582691
{"toggle_reftrace_printer", toggle_reftrace_printer, METH_O},
2692+
{"create_managed_weakref_nogc_type",
2693+
test_create_managed_weakref_nogc_type, METH_NOARGS},
26592694
{NULL, NULL} /* sentinel */
26602695
};
26612696

0 commit comments

Comments
 (0)