@@ -74,12 +74,14 @@ nextkey, reorganize, and sync.");
7474static PyObject *
7575newgdbmobject (_gdbm_state * state , const char * file , int flags , int mode )
7676{
77- gdbmobject * dp = PyObject_New (gdbmobject , state -> gdbm_type );
77+ gdbmobject * dp = PyObject_GC_New (gdbmobject , state -> gdbm_type );
7878 if (dp == NULL ) {
7979 return NULL ;
8080 }
8181 dp -> di_size = -1 ;
8282 errno = 0 ;
83+ PyObject_GC_Track (dp );
84+
8385 if ((dp -> di_dbm = gdbm_open ((char * )file , 0 , flags , mode , NULL )) == 0 ) {
8486 if (errno != 0 ) {
8587 PyErr_SetFromErrnoWithFilename (state -> gdbm_error , file );
@@ -94,10 +96,17 @@ newgdbmobject(_gdbm_state *state, const char *file, int flags, int mode)
9496}
9597
9698/* Methods */
99+ static int
100+ gdbm_traverse (gdbmobject * dp , visitproc visit , void * arg )
101+ {
102+ Py_VISIT (Py_TYPE (dp ));
103+ return 0 ;
104+ }
97105
98106static void
99107gdbm_dealloc (gdbmobject * dp )
100108{
109+ PyObject_GC_UnTrack (dp );
101110 if (dp -> di_dbm ) {
102111 gdbm_close (dp -> di_dbm );
103112 }
@@ -554,6 +563,7 @@ static PyMethodDef gdbm_methods[] = {
554563
555564static PyType_Slot gdbmtype_spec_slots [] = {
556565 {Py_tp_dealloc , gdbm_dealloc },
566+ {Py_tp_traverse , gdbm_traverse },
557567 {Py_tp_methods , gdbm_methods },
558568 {Py_sq_contains , gdbm_contains },
559569 {Py_mp_length , gdbm_length },
@@ -570,7 +580,8 @@ static PyType_Spec gdbmtype_spec = {
570580 // dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
571581 // which prevents to create a subclass.
572582 // So calling PyType_GetModuleState() in this file is always safe.
573- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
583+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
584+ Py_TPFLAGS_HAVE_GC ),
574585 .slots = gdbmtype_spec_slots ,
575586};
576587
0 commit comments