Skip to content

Commit 8a49659

Browse files
author
Erlend E. Aasland
committed
Refactor _pysqlite_seterror
1 parent a3646c9 commit 8a49659

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

Modules/_sqlite/util.c

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,18 @@ pysqlite_step(sqlite3_stmt *statement)
3636
return rc;
3737
}
3838

39-
/**
40-
* Checks the SQLite error code and sets the appropriate DB-API exception.
41-
* Returns the error code (0 means no error occurred).
42-
*/
43-
int
44-
_pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
39+
static PyObject *
40+
get_exception_class(pysqlite_state *state, int errorcode)
4541
{
46-
PyObject *exc_class = NULL;
47-
int errorcode = sqlite3_errcode(db);
48-
49-
switch (errorcode)
50-
{
42+
switch (errorcode) {
5143
case SQLITE_OK:
5244
PyErr_Clear();
53-
return errorcode;
45+
return NULL;
5446
case SQLITE_INTERNAL:
5547
case SQLITE_NOTFOUND:
56-
exc_class = state->InternalError;
57-
break;
48+
return state->InternalError;
5849
case SQLITE_NOMEM:
59-
(void)PyErr_NoMemory();
60-
return errorcode;
50+
return PyErr_NoMemory();
6151
case SQLITE_ERROR:
6252
case SQLITE_PERM:
6353
case SQLITE_ABORT:
@@ -71,41 +61,36 @@ _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
7161
case SQLITE_PROTOCOL:
7262
case SQLITE_EMPTY:
7363
case SQLITE_SCHEMA:
74-
exc_class = state->OperationalError;
75-
break;
64+
return state->OperationalError;
7665
case SQLITE_CORRUPT:
77-
exc_class = state->DatabaseError;
78-
break;
66+
return state->DatabaseError;
7967
case SQLITE_TOOBIG:
80-
exc_class = state->DataError;
81-
break;
68+
return state->DataError;
8269
case SQLITE_CONSTRAINT:
8370
case SQLITE_MISMATCH:
84-
exc_class = state->IntegrityError;
85-
break;
71+
return state->IntegrityError;
8672
case SQLITE_MISUSE:
87-
exc_class = state->ProgrammingError;
88-
break;
73+
return state->ProgrammingError;
8974
default:
90-
exc_class = state->DatabaseError;
91-
break;
75+
return state->DatabaseError;
9276
}
93-
assert(exc_class != NULL);
77+
}
9478

95-
/* Create and set the exception. */
79+
static void
80+
raise_exception(PyObject *type, int errcode, const char *errmsg)
81+
{
9682
PyObject *exc = NULL;
97-
const char *error_msg = sqlite3_errmsg(db);
98-
PyObject *args[] = { PyUnicode_FromString(error_msg), };
83+
PyObject *args[] = { PyUnicode_FromString(errmsg), };
9984
if (args[0] == NULL) {
10085
goto exit;
10186
}
102-
exc = PyObject_Vectorcall(exc_class, args, 1, NULL);
87+
exc = PyObject_Vectorcall(type, args, 1, NULL);
10388
Py_DECREF(args[0]);
10489
if (exc == NULL) {
10590
goto exit;
10691
}
10792

108-
PyObject *code = PyLong_FromLong(errorcode);
93+
PyObject *code = PyLong_FromLong(errcode);
10994
if (code == NULL) {
11095
goto exit;
11196
}
@@ -115,7 +100,7 @@ _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
115100
goto exit;
116101
}
117102

118-
const char *error_name = pysqlite_error_name(errorcode);
103+
const char *error_name = pysqlite_error_name(errcode);
119104
PyObject *name;
120105
if (error_name) {
121106
name = PyUnicode_FromString(error_name);
@@ -132,10 +117,28 @@ _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
132117
goto exit;
133118
}
134119

135-
PyErr_SetObject(exc_class, exc);
120+
PyErr_SetObject(type, exc);
136121

137122
exit:
138123
Py_XDECREF(exc);
124+
}
125+
126+
/**
127+
* Checks the SQLite error code and sets the appropriate DB-API exception.
128+
* Returns the error code (0 means no error occurred).
129+
*/
130+
int
131+
_pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
132+
{
133+
int errorcode = sqlite3_errcode(db);
134+
PyObject *exc_class = get_exception_class(state, errorcode);
135+
if (exc_class == NULL) {
136+
return errorcode;
137+
}
138+
139+
/* Create and set the exception. */
140+
const char *errmsg = sqlite3_errmsg(db);
141+
raise_exception(exc_class, errorcode, errmsg);
139142
return errorcode;
140143
}
141144

0 commit comments

Comments
 (0)