bpo-42064: Move `sqlite3` exceptions to global state, part 2 of 2 (GH… · python/cpython@0516299

@@ -187,12 +187,12 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,

187187

self->Error = state->Error;

188188

self->InterfaceError = state->InterfaceError;

189189

self->DatabaseError = state->DatabaseError;

190-

self->DataError = pysqlite_DataError;

191-

self->OperationalError = pysqlite_OperationalError;

192-

self->IntegrityError = pysqlite_IntegrityError;

190+

self->DataError = state->DataError;

191+

self->OperationalError = state->OperationalError;

192+

self->IntegrityError = state->IntegrityError;

193193

self->InternalError = state->InternalError;

194-

self->ProgrammingError = pysqlite_ProgrammingError;

195-

self->NotSupportedError = pysqlite_NotSupportedError;

194+

self->ProgrammingError = state->ProgrammingError;

195+

self->NotSupportedError = state->NotSupportedError;

196196197197

if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {

198198

return -1;

@@ -390,13 +390,16 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)

390390

*/

391391

int pysqlite_check_connection(pysqlite_Connection* con)

392392

{

393+

pysqlite_state *state = pysqlite_get_state(NULL);

393394

if (!con->initialized) {

394-

PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");

395+

PyErr_SetString(state->ProgrammingError,

396+

"Base Connection.__init__ not called.");

395397

return 0;

396398

}

397399398400

if (!con->db) {

399-

PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");

401+

PyErr_SetString(state->ProgrammingError,

402+

"Cannot operate on a closed database.");

400403

return 0;

401404

} else {

402405

return 1;

@@ -858,12 +861,12 @@ pysqlite_connection_create_function_impl(pysqlite_Connection *self,

858861859862

if (deterministic) {

860863

#if SQLITE_VERSION_NUMBER < 3008003

861-

PyErr_SetString(pysqlite_NotSupportedError,

864+

PyErr_SetString(self->NotSupportedError,

862865

"deterministic=True requires SQLite 3.8.3 or higher");

863866

return NULL;

864867

#else

865868

if (sqlite3_libversion_number() < 3008003) {

866-

PyErr_SetString(pysqlite_NotSupportedError,

869+

PyErr_SetString(self->NotSupportedError,

867870

"deterministic=True requires SQLite 3.8.3 or higher");

868871

return NULL;

869872

}

@@ -882,7 +885,7 @@ pysqlite_connection_create_function_impl(pysqlite_Connection *self,

882885883886

if (rc != SQLITE_OK) {

884887

/* Workaround for SQLite bug: no error code or string is available here */

885-

PyErr_SetString(pysqlite_OperationalError, "Error creating function");

888+

PyErr_SetString(self->OperationalError, "Error creating function");

886889

return NULL;

887890

}

888891

Py_RETURN_NONE;

@@ -921,7 +924,7 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,

921924

&_destructor); // will decref func

922925

if (rc != SQLITE_OK) {

923926

/* Workaround for SQLite bug: no error code or string is available here */

924-

PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");

927+

PyErr_SetString(self->OperationalError, "Error creating aggregate");

925928

return NULL;

926929

}

927930

Py_RETURN_NONE;

@@ -1068,7 +1071,8 @@ pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,

10681071

rc = sqlite3_set_authorizer(self->db, _authorizer_callback, authorizer_cb);

10691072

}

10701073

if (rc != SQLITE_OK) {

1071-

PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");

1074+

PyErr_SetString(self->OperationalError,

1075+

"Error setting authorizer callback");

10721076

Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);

10731077

return NULL;

10741078

}

@@ -1181,7 +1185,8 @@ pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,

11811185

rc = sqlite3_enable_load_extension(self->db, onoff);

1182118611831187

if (rc != SQLITE_OK) {

1184-

PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");

1188+

PyErr_SetString(self->OperationalError,

1189+

"Error enabling load extension");

11851190

return NULL;

11861191

} else {

11871192

Py_RETURN_NONE;

@@ -1215,7 +1220,7 @@ pysqlite_connection_load_extension_impl(pysqlite_Connection *self,

1215122012161221

rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);

12171222

if (rc != 0) {

1218-

PyErr_SetString(pysqlite_OperationalError, errmsg);

1223+

PyErr_SetString(self->OperationalError, errmsg);

12191224

return NULL;

12201225

} else {

12211226

Py_RETURN_NONE;

@@ -1227,7 +1232,7 @@ int pysqlite_check_thread(pysqlite_Connection* self)

12271232

{

12281233

if (self->check_same_thread) {

12291234

if (PyThread_get_thread_ident() != self->thread_ident) {

1230-

PyErr_Format(pysqlite_ProgrammingError,

1235+

PyErr_Format(self->ProgrammingError,

12311236

"SQLite objects created in a thread can only be used in that same thread. "

12321237

"The object was created in thread id %lu and this is thread id %lu.",

12331238

self->thread_ident, PyThread_get_thread_ident());

@@ -1579,7 +1584,7 @@ pysqlite_connection_iterdump_impl(pysqlite_Connection *self)

15791584

pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);

15801585

if (!pyfn_iterdump) {

15811586

if (!PyErr_Occurred()) {

1582-

PyErr_SetString(pysqlite_OperationalError,

1587+

PyErr_SetString(self->OperationalError,

15831588

"Failed to obtain _iterdump() reference");

15841589

}

15851590

goto finally;

@@ -1634,7 +1639,7 @@ pysqlite_connection_backup_impl(pysqlite_Connection *self,

16341639

/* Since 3.8.8 this is already done, per commit

16351640

https://www.sqlite.org/src/info/169b5505498c0a7e */

16361641

if (!sqlite3_get_autocommit(target->db)) {

1637-

PyErr_SetString(pysqlite_OperationalError, "target is in transaction");

1642+

PyErr_SetString(self->OperationalError, "target is in transaction");

16381643

return NULL;

16391644

}

16401645

#endif

@@ -1746,7 +1751,8 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,

17461751

{

17471752

continue;

17481753

} else {

1749-

PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");

1754+

PyErr_SetString(self->ProgrammingError,

1755+

"invalid character in collation name");

17501756

goto finally;

17511757

}

17521758

}