@@ -102,8 +102,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
102102
103103 database = PyBytes_AsString (database_obj );
104104
105- self -> initialized = 1 ;
106-
107105 self -> begin_statement = NULL ;
108106
109107 Py_CLEAR (self -> statement_cache );
@@ -149,7 +147,7 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
149147 Py_INCREF (isolation_level );
150148 }
151149 Py_CLEAR (self -> isolation_level );
152- if (pysqlite_connection_set_isolation_level (self , isolation_level , NULL ) < 0 ) {
150+ if (pysqlite_connection_set_isolation_level (self , isolation_level , NULL ) != 0 ) {
153151 Py_DECREF (isolation_level );
154152 return -1 ;
155153 }
@@ -208,6 +206,8 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
208206 self -> ProgrammingError = pysqlite_ProgrammingError ;
209207 self -> NotSupportedError = pysqlite_NotSupportedError ;
210208
209+ self -> initialized = 1 ;
210+
211211 return 0 ;
212212}
213213
@@ -339,6 +339,12 @@ PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
339339 return NULL ;
340340 }
341341
342+ if (!self -> initialized ) {
343+ PyErr_SetString (pysqlite_ProgrammingError ,
344+ "Base Connection.__init__ not called." );
345+ return NULL ;
346+ }
347+
342348 pysqlite_do_all_statements (self , ACTION_FINALIZE , 1 );
343349
344350 if (self -> db ) {
@@ -1143,6 +1149,9 @@ int pysqlite_check_thread(pysqlite_Connection* self)
11431149
11441150static PyObject * pysqlite_connection_get_isolation_level (pysqlite_Connection * self , void * unused )
11451151{
1152+ if (!pysqlite_check_connection (self )) {
1153+ return NULL ;
1154+ }
11461155 Py_INCREF (self -> isolation_level );
11471156 return self -> isolation_level ;
11481157}
@@ -1175,11 +1184,17 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
11751184 return -1 ;
11761185 }
11771186 if (isolation_level == Py_None ) {
1178- PyObject * res = pysqlite_connection_commit (self , NULL );
1179- if (!res ) {
1180- return -1 ;
1187+ /* We might get called during connection init, so we cannot use
1188+ * pysqlite_connection_commit() here. */
1189+ if (self -> db && !sqlite3_get_autocommit (self -> db )) {
1190+ int rc ;
1191+ Py_BEGIN_ALLOW_THREADS
1192+ rc = sqlite3_exec (self -> db , "COMMIT" , NULL , NULL , NULL );
1193+ Py_END_ALLOW_THREADS
1194+ if (rc != SQLITE_OK ) {
1195+ return _pysqlite_seterror (self -> db , NULL );
1196+ }
11811197 }
1182- Py_DECREF (res );
11831198
11841199 self -> begin_statement = NULL ;
11851200 } else {
0 commit comments