bpo-42064: Move sqlite3 exceptions to global state, part 2 of 2 (GH-26884)

Automerge-Triggered-By: GH:encukou
This commit is contained in:
Erlend Egeberg Aasland 2021-07-14 13:26:44 +02:00 committed by GitHub
parent e5862f79c1
commit 05162993fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 90 additions and 92 deletions

View file

@ -351,10 +351,12 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
colname , text);
error_msg = PyUnicode_Decode(buf, strlen(buf), "ascii", "replace");
PyObject *exc = self->connection->OperationalError;
if (!error_msg) {
PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
PyErr_SetString(exc, "Could not decode to UTF-8");
} else {
PyErr_SetObject(pysqlite_OperationalError, error_msg);
PyErr_SetObject(exc, error_msg);
Py_DECREF(error_msg);
}
}
@ -401,18 +403,23 @@ error:
*/
static int check_cursor(pysqlite_Cursor* cur)
{
pysqlite_state *state = pysqlite_get_state(NULL);
if (!cur->initialized) {
PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
PyErr_SetString(state->ProgrammingError,
"Base Cursor.__init__ not called.");
return 0;
}
if (cur->closed) {
PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
PyErr_SetString(state->ProgrammingError,
"Cannot operate on a closed cursor.");
return 0;
}
if (cur->locked) {
PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
PyErr_SetString(state->ProgrammingError,
"Recursive use of cursors not allowed.");
return 0;
}
@ -588,7 +595,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
if (pysqlite_build_row_cast_map(self) != 0) {
_PyErr_FormatFromCause(pysqlite_OperationalError, "Error while building row_cast_map");
_PyErr_FormatFromCause(self->connection->OperationalError,
"Error while building row_cast_map");
goto error;
}
@ -641,7 +649,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
if (rc == SQLITE_ROW) {
if (multiple) {
PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
PyErr_SetString(self->connection->ProgrammingError,
"executemany() can only execute DML "
"statements.");
goto error;
}
@ -745,7 +755,8 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
int max_length = sqlite3_limit(self->connection->db,
SQLITE_LIMIT_LENGTH, -1);
if (sql_len >= max_length) {
PyErr_SetString(pysqlite_DataError, "query string is too large");
PyErr_SetString(self->connection->DataError,
"query string is too large");
return NULL;
}
} else {
@ -1018,7 +1029,8 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self)
/*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
{
if (!self->connection) {
PyErr_SetString(pysqlite_ProgrammingError,
pysqlite_state *state = pysqlite_get_state(NULL);
PyErr_SetString(state->ProgrammingError,
"Base Cursor.__init__ not called.");
return NULL;
}