Skip to content

Commit 0dffd84

Browse files
authored
Revert "bpo-31746: Prevent segfaults when sqlite3.Connection is uninitialised (GH-27431)"
This reverts commit 7e311e4.
1 parent 3edec5d commit 0dffd84

File tree

2 files changed

+7
-44
lines changed

2 files changed

+7
-44
lines changed

Lib/sqlite3/test/dbapi.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,6 @@ def test_drop_unused_refs(self):
243243
self.assertEqual(cu.fetchone()[0], n)
244244

245245

246-
class UninitialisedConnectionTests(unittest.TestCase):
247-
def setUp(self):
248-
self.cx = sqlite.Connection.__new__(sqlite.Connection)
249-
250-
def test_uninit_operations(self):
251-
funcs = (
252-
lambda: self.cx.isolation_level,
253-
lambda: self.cx.total_changes,
254-
lambda: self.cx.in_transaction,
255-
lambda: self.cx.iterdump(),
256-
lambda: self.cx.cursor(),
257-
lambda: self.cx.close(),
258-
)
259-
for func in funcs:
260-
with self.subTest(func=func):
261-
self.assertRaisesRegex(sqlite.ProgrammingError,
262-
"Base Connection.__init__ not called",
263-
func)
264-
265-
266246
class OpenTests(unittest.TestCase):
267247
_sql = "create table test(id integer)"
268248

@@ -971,7 +951,6 @@ def suite():
971951
ModuleTests,
972952
SqliteOnConflictTests,
973953
ThreadTests,
974-
UninitialisedConnectionTests,
975954
]
976955
return unittest.TestSuite(
977956
[unittest.TestLoader().loadTestsFromTestCase(t) for t in tests]

Modules/_sqlite/connection.c

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
111111

112112
const char *database = PyBytes_AsString(database_obj);
113113

114+
self->initialized = 1;
115+
114116
self->begin_statement = NULL;
115117

116118
Py_CLEAR(self->statement_cache);
@@ -145,7 +147,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
145147
Py_INCREF(isolation_level);
146148
}
147149
Py_CLEAR(self->isolation_level);
148-
if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) != 0) {
150+
if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
149151
Py_DECREF(isolation_level);
150152
return -1;
151153
}
@@ -193,8 +195,6 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
193195
return -1;
194196
}
195197

196-
self->initialized = 1;
197-
198198
return 0;
199199
}
200200

@@ -371,13 +371,6 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
371371
return NULL;
372372
}
373373

374-
if (!self->initialized) {
375-
pysqlite_state *state = pysqlite_get_state(NULL);
376-
PyErr_SetString(state->ProgrammingError,
377-
"Base Connection.__init__ not called.");
378-
return NULL;
379-
}
380-
381374
pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
382375
connection_close(self);
383376

@@ -1265,9 +1258,6 @@ int pysqlite_check_thread(pysqlite_Connection* self)
12651258

12661259
static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
12671260
{
1268-
if (!pysqlite_check_connection(self)) {
1269-
return NULL;
1270-
}
12711261
return Py_NewRef(self->isolation_level);
12721262
}
12731263

@@ -1299,17 +1289,11 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
12991289
return -1;
13001290
}
13011291
if (isolation_level == Py_None) {
1302-
/* We might get called during connection init, so we cannot use
1303-
* pysqlite_connection_commit() here. */
1304-
if (self->db && !sqlite3_get_autocommit(self->db)) {
1305-
int rc;
1306-
Py_BEGIN_ALLOW_THREADS
1307-
rc = sqlite3_exec(self->db, "COMMIT", NULL, NULL, NULL);
1308-
Py_END_ALLOW_THREADS
1309-
if (rc != SQLITE_OK) {
1310-
return _pysqlite_seterror(self->db);
1311-
}
1292+
PyObject *res = pysqlite_connection_commit(self, NULL);
1293+
if (!res) {
1294+
return -1;
13121295
}
1296+
Py_DECREF(res);
13131297

13141298
self->begin_statement = NULL;
13151299
} else {

0 commit comments

Comments
 (0)