gh-129928: Rework sqlite3 error helpers (#129929)

Add a helper for raising DB-API compatible exceptions based on the
result code of SQLite C APIs. Some APIs do not store the error indicator
on the database pointer, so we need to be able to deduce the DB-API
compatible exception directly from the error code.

- rename _pysqlite_seterror() as set_error_from_db()
- introduce set_error_from_code()
This commit is contained in:
Erlend E. Aasland 2025-02-11 08:49:25 +01:00 committed by GitHub
parent 3a2e7aacf6
commit 3b366a4a4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 41 additions and 30 deletions

View file

@ -505,7 +505,7 @@ begin_transaction(pysqlite_Connection *self)
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
set_error_from_db(self->state, self->db);
return -1;
}
@ -715,7 +715,7 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
if (rc != SQLITE_OK) {
PyObject *exc = PyErr_GetRaisedException();
sqlite3 *db = sqlite3_db_handle(self->st);
_pysqlite_seterror(state, db);
set_error_from_db(state, db);
_PyErr_ChainExceptions1(exc);
return;
}
@ -764,7 +764,7 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
if (rc != SQLITE_OK) {
PyObject *exc = PyErr_GetRaisedException();
sqlite3 *db = sqlite3_db_handle(self->st);
_pysqlite_seterror(state, db);
set_error_from_db(state, db);
_PyErr_ChainExceptions1(exc);
return;
}
@ -896,7 +896,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
PyErr_Clear();
}
}
_pysqlite_seterror(state, self->connection->db);
set_error_from_db(state, self->connection->db);
goto error;
}
@ -1087,7 +1087,7 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
return Py_NewRef((PyObject *)self);
error:
_pysqlite_seterror(self->connection->state, db);
set_error_from_db(self->connection->state, db);
return NULL;
}
@ -1122,8 +1122,7 @@ pysqlite_cursor_iternext(PyObject *op)
Py_CLEAR(self->statement);
}
else if (rc != SQLITE_ROW) {
(void)_pysqlite_seterror(self->connection->state,
self->connection->db);
set_error_from_db(self->connection->state, self->connection->db);
(void)stmt_reset(self->statement);
Py_CLEAR(self->statement);
Py_DECREF(row);