@@ -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+ create_managed_weakref_nogc_type(PyObject *self, PyObject *Py_UNUSED(args))
2593+ {
2594+ return PyType_FromSpec(&ManagedWeakrefNoGC_spec);
2595+ }
2596+
2597+
25652598static 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+ create_managed_weakref_nogc_type, METH_NOARGS},
26592694 {NULL, NULL} /* sentinel */
26602695};
26612696
0 commit comments