(continued from previous page) 

{ NULL } /* Sentinel */ }; static PyTypeObject CustomType={ PyVarObject_HEAD_INIT(NULL ,0) .tp_name ="custom4.Custom ", .tp_doc="Custom objects", .tp_basicsize= sizeof (CustomObject), .tp_itemsize=0, .tp_flags =Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, .tp_new=Custom_new, .tp_init=(initproc) Custom_init, .tp_dealloc =(destructor) Custom_dealloc, .tp_traverse =(traverseproc) Custom_traverse, .tp_clear =(inquiry) Custom_clear, .tp_members=Custom_members, .tp_methods=Custom_methods, .tp_getset =Custom_getsetters, }; static PyModuleDef custommodule={ PyModuleDef_HEAD_INIT, .m_name="custom4 ", .m_doc="Example module that creates an extension type. ", .m_size=-1 , }; PyMODINIT_FUNC PyInit_custom4 ( void ) { PyObject *m; if (PyType_Ready( &CustomType)<0) return NULL ; m =PyModule_Create(&custommodule); if (m==NULL ) return NULL ; Py_INCREF(&CustomType); if (PyModule_AddObject(m, "Custom", (PyObject*)&CustomType)<0) { Py_DECREF( &CustomType); Py_DECREF(m); return NULL ; } return m; } 

First, the traversal method lets the cyclic GC know about subobjects that could participate in cycles: 

static int Custom_traverse(CustomObject *self, visitproc visit, void *arg) { int vret; if (self ->first) { vret =visit(self ->first, arg); if (vret!=0) return vret; } if (self ->last) { vret =visit(self ->last, arg); 

(continues on next page) 

