@@ -225,28 +225,51 @@ pysqlite_do_all_statements(pysqlite_Connection *self, int action,
225225 }
226226}
227227
228+ static int
229+ connection_traverse (pysqlite_Connection * self , visitproc visit , void * arg )
230+ {
231+ Py_VISIT (self -> statement_cache );
232+ Py_VISIT (self -> isolation_level );
233+ Py_VISIT (self -> function_pinboard_trace_callback );
234+ Py_VISIT (self -> function_pinboard_progress_handler );
235+ Py_VISIT (self -> function_pinboard_authorizer_cb );
236+ Py_VISIT (self -> row_factory );
237+ Py_VISIT (self -> text_factory );
238+ Py_VISIT (self -> collations );
239+ Py_VISIT (self -> statements );
240+ Py_VISIT (self -> cursors );
241+ Py_VISIT (Py_TYPE (self ));
242+ return 0 ;
243+ }
244+
245+ static int
246+ connection_clear (pysqlite_Connection * self )
247+ {
248+ Py_CLEAR (self -> statement_cache );
249+ Py_CLEAR (self -> isolation_level );
250+ Py_CLEAR (self -> function_pinboard_trace_callback );
251+ Py_CLEAR (self -> function_pinboard_progress_handler );
252+ Py_CLEAR (self -> function_pinboard_authorizer_cb );
253+ Py_CLEAR (self -> row_factory );
254+ Py_CLEAR (self -> text_factory );
255+ Py_CLEAR (self -> collations );
256+ Py_CLEAR (self -> statements );
257+ Py_CLEAR (self -> cursors );
258+ return 0 ;
259+ }
260+
228261static void
229- pysqlite_connection_dealloc (pysqlite_Connection * self )
262+ connection_dealloc (pysqlite_Connection * self )
230263{
231264 PyTypeObject * tp = Py_TYPE (self );
232-
233- Py_XDECREF ( self -> statement_cache );
265+ PyObject_GC_UnTrack ( self );
266+ tp -> tp_clear (( PyObject * ) self );
234267
235268 /* Clean up if user has not called .close() explicitly. */
236269 if (self -> db ) {
237270 sqlite3_close_v2 (self -> db );
238271 }
239272
240- Py_XDECREF (self -> isolation_level );
241- Py_XDECREF (self -> function_pinboard_trace_callback );
242- Py_XDECREF (self -> function_pinboard_progress_handler );
243- Py_XDECREF (self -> function_pinboard_authorizer_cb );
244- Py_XDECREF (self -> row_factory );
245- Py_XDECREF (self -> text_factory );
246- Py_XDECREF (self -> collations );
247- Py_XDECREF (self -> statements );
248- Py_XDECREF (self -> cursors );
249-
250273 tp -> tp_free (self );
251274 Py_DECREF (tp );
252275}
@@ -1328,7 +1351,7 @@ pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
13281351
13291352 _pysqlite_drop_unused_statement_references (self );
13301353
1331- statement = PyObject_New (pysqlite_Statement , pysqlite_StatementType );
1354+ statement = PyObject_GC_New (pysqlite_Statement , pysqlite_StatementType );
13321355 if (!statement ) {
13331356 return NULL ;
13341357 }
@@ -1909,21 +1932,22 @@ static struct PyMemberDef connection_members[] =
19091932};
19101933
19111934static PyType_Slot connection_slots [] = {
1912- {Py_tp_dealloc , pysqlite_connection_dealloc },
1935+ {Py_tp_dealloc , connection_dealloc },
19131936 {Py_tp_doc , (void * )connection_doc },
19141937 {Py_tp_methods , connection_methods },
19151938 {Py_tp_members , connection_members },
19161939 {Py_tp_getset , connection_getset },
1917- {Py_tp_new , PyType_GenericNew },
19181940 {Py_tp_init , pysqlite_connection_init },
19191941 {Py_tp_call , pysqlite_connection_call },
1942+ {Py_tp_traverse , connection_traverse },
1943+ {Py_tp_clear , connection_clear },
19201944 {0 , NULL },
19211945};
19221946
19231947static PyType_Spec connection_spec = {
19241948 .name = MODULE_NAME ".Connection" ,
19251949 .basicsize = sizeof (pysqlite_Connection ),
1926- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE ,
1950+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC ,
19271951 .slots = connection_slots ,
19281952};
19291953
0 commit comments