@@ -258,6 +258,16 @@ connection_clear(pysqlite_Connection *self)
258258 return 0 ;
259259}
260260
261+ static void
262+ connection_close (pysqlite_Connection * self )
263+ {
264+ if (self -> db ) {
265+ int rc = sqlite3_close_v2 (self -> db );
266+ assert (rc == SQLITE_OK );
267+ self -> db = NULL ;
268+ }
269+ }
270+
261271static void
262272connection_dealloc (pysqlite_Connection * self )
263273{
@@ -266,9 +276,7 @@ connection_dealloc(pysqlite_Connection *self)
266276 tp -> tp_clear ((PyObject * )self );
267277
268278 /* Clean up if user has not called .close() explicitly. */
269- if (self -> db ) {
270- sqlite3_close_v2 (self -> db );
271- }
279+ connection_close (self );
272280
273281 tp -> tp_free (self );
274282 Py_DECREF (tp );
@@ -353,24 +361,12 @@ static PyObject *
353361pysqlite_connection_close_impl (pysqlite_Connection * self )
354362/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
355363{
356- int rc ;
357-
358364 if (!pysqlite_check_thread (self )) {
359365 return NULL ;
360366 }
361367
362368 pysqlite_do_all_statements (self , ACTION_FINALIZE , 1 );
363-
364- if (self -> db ) {
365- rc = sqlite3_close_v2 (self -> db );
366-
367- if (rc != SQLITE_OK ) {
368- _pysqlite_seterror (self -> db );
369- return NULL ;
370- } else {
371- self -> db = NULL ;
372- }
373- }
369+ connection_close (self );
374370
375371 Py_RETURN_NONE ;
376372}
@@ -1820,6 +1816,9 @@ static PyObject *
18201816pysqlite_connection_enter_impl (pysqlite_Connection * self )
18211817/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
18221818{
1819+ if (!pysqlite_check_connection (self )) {
1820+ return NULL ;
1821+ }
18231822 return Py_NewRef ((PyObject * )self );
18241823}
18251824
0 commit comments