@@ -74,12 +74,14 @@ nextkey, reorganize, and sync.");
74
74
static PyObject *
75
75
newgdbmobject (_gdbm_state * state , const char * file , int flags , int mode )
76
76
{
77
- gdbmobject * dp = PyObject_New (gdbmobject , state -> gdbm_type );
77
+ gdbmobject * dp = PyObject_GC_New (gdbmobject , state -> gdbm_type );
78
78
if (dp == NULL ) {
79
79
return NULL ;
80
80
}
81
81
dp -> di_size = -1 ;
82
82
errno = 0 ;
83
+ PyObject_GC_Track (dp );
84
+
83
85
if ((dp -> di_dbm = gdbm_open ((char * )file , 0 , flags , mode , NULL )) == 0 ) {
84
86
if (errno != 0 ) {
85
87
PyErr_SetFromErrnoWithFilename (state -> gdbm_error , file );
@@ -94,10 +96,17 @@ newgdbmobject(_gdbm_state *state, const char *file, int flags, int mode)
94
96
}
95
97
96
98
/* Methods */
99
+ static int
100
+ gdbm_traverse (gdbmobject * dp , visitproc visit , void * arg )
101
+ {
102
+ Py_VISIT (Py_TYPE (dp ));
103
+ return 0 ;
104
+ }
97
105
98
106
static void
99
107
gdbm_dealloc (gdbmobject * dp )
100
108
{
109
+ PyObject_GC_UnTrack (dp );
101
110
if (dp -> di_dbm ) {
102
111
gdbm_close (dp -> di_dbm );
103
112
}
@@ -554,6 +563,7 @@ static PyMethodDef gdbm_methods[] = {
554
563
555
564
static PyType_Slot gdbmtype_spec_slots [] = {
556
565
{Py_tp_dealloc , gdbm_dealloc },
566
+ {Py_tp_traverse , gdbm_traverse },
557
567
{Py_tp_methods , gdbm_methods },
558
568
{Py_sq_contains , gdbm_contains },
559
569
{Py_mp_length , gdbm_length },
@@ -570,7 +580,8 @@ static PyType_Spec gdbmtype_spec = {
570
580
// dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
571
581
// which prevents to create a subclass.
572
582
// 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 ),
574
585
.slots = gdbmtype_spec_slots ,
575
586
};
576
587
0 commit comments