@@ -250,6 +250,19 @@ connection_clear(pysqlite_Connection *self)
250
250
return 0 ;
251
251
}
252
252
253
+ static int
254
+ connection_close (pysqlite_Connection * self )
255
+ {
256
+ int rc = SQLITE_OK ;
257
+
258
+ if (self -> db ) {
259
+ rc = sqlite3_close_v2 (self -> db );
260
+ self -> db = NULL ;
261
+ }
262
+
263
+ return rc ;
264
+ }
265
+
253
266
static void
254
267
connection_dealloc (pysqlite_Connection * self )
255
268
{
@@ -258,9 +271,7 @@ connection_dealloc(pysqlite_Connection *self)
258
271
tp -> tp_clear ((PyObject * )self );
259
272
260
273
/* Clean up if user has not called .close() explicitly. */
261
- if (self -> db ) {
262
- sqlite3_close_v2 (self -> db );
263
- }
274
+ (void )connection_close (self );
264
275
265
276
tp -> tp_free (self );
266
277
Py_DECREF (tp );
@@ -345,22 +356,21 @@ static PyObject *
345
356
pysqlite_connection_close_impl (pysqlite_Connection * self )
346
357
/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
347
358
{
348
- int rc ;
349
-
350
359
if (!pysqlite_check_thread (self )) {
351
360
return NULL ;
352
361
}
353
362
354
- pysqlite_do_all_statements (self , ACTION_FINALIZE , 1 );
355
-
356
363
if (self -> db ) {
357
- rc = sqlite3_close_v2 (self -> db );
364
+ /* Free pending statements before closing. This implies also cleaning
365
+ * up cursors, as they may have strong refs to statements. */
366
+ Py_CLEAR (self -> statement_cache );
367
+ Py_CLEAR (self -> statements );
368
+ Py_CLEAR (self -> cursors );
358
369
370
+ int rc = connection_close (self );
359
371
if (rc != SQLITE_OK ) {
360
372
_pysqlite_seterror (self -> db );
361
373
return NULL ;
362
- } else {
363
- self -> db = NULL ;
364
374
}
365
375
}
366
376
0 commit comments