@@ -36,28 +36,18 @@ pysqlite_step(sqlite3_stmt *statement)
36
36
return rc ;
37
37
}
38
38
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 )
45
41
{
46
- PyObject * exc_class = NULL ;
47
- int errorcode = sqlite3_errcode (db );
48
-
49
- switch (errorcode )
50
- {
42
+ switch (errorcode ) {
51
43
case SQLITE_OK :
52
44
PyErr_Clear ();
53
- return errorcode ;
45
+ return NULL ;
54
46
case SQLITE_INTERNAL :
55
47
case SQLITE_NOTFOUND :
56
- exc_class = state -> InternalError ;
57
- break ;
48
+ return state -> InternalError ;
58
49
case SQLITE_NOMEM :
59
- (void )PyErr_NoMemory ();
60
- return errorcode ;
50
+ return PyErr_NoMemory ();
61
51
case SQLITE_ERROR :
62
52
case SQLITE_PERM :
63
53
case SQLITE_ABORT :
@@ -71,41 +61,36 @@ _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
71
61
case SQLITE_PROTOCOL :
72
62
case SQLITE_EMPTY :
73
63
case SQLITE_SCHEMA :
74
- exc_class = state -> OperationalError ;
75
- break ;
64
+ return state -> OperationalError ;
76
65
case SQLITE_CORRUPT :
77
- exc_class = state -> DatabaseError ;
78
- break ;
66
+ return state -> DatabaseError ;
79
67
case SQLITE_TOOBIG :
80
- exc_class = state -> DataError ;
81
- break ;
68
+ return state -> DataError ;
82
69
case SQLITE_CONSTRAINT :
83
70
case SQLITE_MISMATCH :
84
- exc_class = state -> IntegrityError ;
85
- break ;
71
+ return state -> IntegrityError ;
86
72
case SQLITE_MISUSE :
87
- exc_class = state -> ProgrammingError ;
88
- break ;
73
+ return state -> ProgrammingError ;
89
74
default :
90
- exc_class = state -> DatabaseError ;
91
- break ;
75
+ return state -> DatabaseError ;
92
76
}
93
- assert ( exc_class != NULL );
77
+ }
94
78
95
- /* Create and set the exception. */
79
+ static void
80
+ raise_exception (PyObject * type , int errcode , const char * errmsg )
81
+ {
96
82
PyObject * exc = NULL ;
97
- const char * error_msg = sqlite3_errmsg (db );
98
- PyObject * args [] = { PyUnicode_FromString (error_msg ), };
83
+ PyObject * args [] = { PyUnicode_FromString (errmsg ), };
99
84
if (args [0 ] == NULL ) {
100
85
goto exit ;
101
86
}
102
- exc = PyObject_Vectorcall (exc_class , args , 1 , NULL );
87
+ exc = PyObject_Vectorcall (type , args , 1 , NULL );
103
88
Py_DECREF (args [0 ]);
104
89
if (exc == NULL ) {
105
90
goto exit ;
106
91
}
107
92
108
- PyObject * code = PyLong_FromLong (errorcode );
93
+ PyObject * code = PyLong_FromLong (errcode );
109
94
if (code == NULL ) {
110
95
goto exit ;
111
96
}
@@ -115,7 +100,7 @@ _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
115
100
goto exit ;
116
101
}
117
102
118
- const char * error_name = pysqlite_error_name (errorcode );
103
+ const char * error_name = pysqlite_error_name (errcode );
119
104
PyObject * name ;
120
105
if (error_name ) {
121
106
name = PyUnicode_FromString (error_name );
@@ -132,10 +117,28 @@ _pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
132
117
goto exit ;
133
118
}
134
119
135
- PyErr_SetObject (exc_class , exc );
120
+ PyErr_SetObject (type , exc );
136
121
137
122
exit :
138
123
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 );
139
142
return errorcode ;
140
143
}
141
144
0 commit comments