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

@ -188,7 +188,7 @@ connection_exec_stmt(pysqlite_Connection *self, const char *sql)
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;
}
return 0;
@ -274,7 +274,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
if (rc != SQLITE_OK) {
_pysqlite_seterror(state, db);
set_error_from_db(state, db);
goto error;
}
@ -607,11 +607,11 @@ blobopen_impl(pysqlite_Connection *self, const char *table, const char *col,
Py_END_ALLOW_THREADS
if (rc == SQLITE_MISUSE) {
PyErr_Format(self->state->InterfaceError, sqlite3_errstr(rc));
set_error_from_code(self->state, rc);
return NULL;
}
else if (rc != SQLITE_OK) {
_pysqlite_seterror(self->state, self->db);
set_error_from_db(self->state, self->db);
return NULL;
}
@ -1352,9 +1352,9 @@ create_window_function_impl(pysqlite_Connection *self, PyTypeObject *cls,
}
if (rc != SQLITE_OK) {
// Errors are not set on the database connection, so we cannot
// use _pysqlite_seterror().
PyErr_SetString(self->ProgrammingError, sqlite3_errstr(rc));
/* Errors are not set on the database connection; use result code
* instead. */
set_error_from_code(self->state, rc);
return NULL;
}
Py_RETURN_NONE;
@ -2112,7 +2112,7 @@ pysqlite_connection_backup_impl(pysqlite_Connection *self,
Py_END_ALLOW_THREADS
if (bck_handle == NULL) {
_pysqlite_seterror(self->state, bck_conn);
set_error_from_db(self->state, bck_conn);
return NULL;
}
@ -2150,7 +2150,7 @@ pysqlite_connection_backup_impl(pysqlite_Connection *self,
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->state, bck_conn);
set_error_from_db(self->state, bck_conn);
return NULL;
}
@ -2208,7 +2208,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
if (callable != Py_None) {
free_callback_context(ctx);
}
_pysqlite_seterror(self->state, self->db);
set_error_from_db(self->state, self->db);
return NULL;
}
@ -2326,7 +2326,7 @@ deserialize_impl(pysqlite_Connection *self, Py_buffer *data,
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
set_error_from_db(self->state, self->db);
return NULL;
}
Py_RETURN_NONE;
@ -2521,7 +2521,7 @@ setconfig_impl(pysqlite_Connection *self, int op, int enable)
int actual;
int rc = sqlite3_db_config(self->db, op, enable, &actual);
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
set_error_from_db(self->state, self->db);
return NULL;
}
if (enable != actual) {
@ -2556,7 +2556,7 @@ getconfig_impl(pysqlite_Connection *self, int op)
int current;
int rc = sqlite3_db_config(self->db, op, -1, &current);
if (rc != SQLITE_OK) {
(void)_pysqlite_seterror(self->state, self->db);
set_error_from_db(self->state, self->db);
return -1;
}
return current;