Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1 | /* module.c - the module itself |
| 2 | * |
| 3 | * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de> |
| 4 | * |
| 5 | * This file is part of pysqlite. |
| 6 | * |
| 7 | * This software is provided 'as-is', without any express or implied |
| 8 | * warranty. In no event will the authors be held liable for any damages |
| 9 | * arising from the use of this software. |
| 10 | * |
| 11 | * Permission is granted to anyone to use this software for any purpose, |
| 12 | * including commercial applications, and to alter it and redistribute it |
| 13 | * freely, subject to the following restrictions: |
| 14 | * |
| 15 | * 1. The origin of this software must not be misrepresented; you must not |
| 16 | * claim that you wrote the original software. If you use this software |
| 17 | * in a product, an acknowledgment in the product documentation would be |
| 18 | * appreciated but is not required. |
| 19 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 20 | * misrepresented as being the original software. |
| 21 | * 3. This notice may not be removed or altered from any source distribution. |
| 22 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 23 | |
| 24 | #include "connection.h" |
| 25 | #include "statement.h" |
| 26 | #include "cursor.h" |
| 27 | #include "cache.h" |
| 28 | #include "prepare_protocol.h" |
| 29 | #include "microprotocols.h" |
| 30 | #include "row.h" |
| 31 | |
| 32 | #if SQLITE_VERSION_NUMBER >= 3003003 |
| 33 | #define HAVE_SHARED_CACHE |
| 34 | #endif |
| 35 | |
| 36 | /* static objects at module-level */ |
| 37 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 38 | PyObject* pysqlite_Error, *pysqlite_Warning, *pysqlite_InterfaceError, *pysqlite_DatabaseError, |
| 39 | *pysqlite_InternalError, *pysqlite_OperationalError, *pysqlite_ProgrammingError, |
| 40 | *pysqlite_IntegrityError, *pysqlite_DataError, *pysqlite_NotSupportedError, *pysqlite_OptimizedUnicode; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | |
| 42 | PyObject* converters; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 43 | int _enable_callback_tracebacks; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 44 | int pysqlite_BaseTypeAdapted; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | |
| 46 | static PyObject* module_connect(PyObject* self, PyObject* args, PyObject* |
| 47 | kwargs) |
| 48 | { |
| 49 | /* Python seems to have no way of extracting a single keyword-arg at |
| 50 | * C-level, so this code is redundant with the one in connection_init in |
| 51 | * connection.c and must always be copied from there ... */ |
| 52 | |
| 53 | static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL}; |
| 54 | char* database; |
| 55 | int detect_types = 0; |
| 56 | PyObject* isolation_level; |
| 57 | PyObject* factory = NULL; |
| 58 | int check_same_thread = 1; |
| 59 | int cached_statements; |
| 60 | double timeout = 5.0; |
| 61 | |
| 62 | PyObject* result; |
| 63 | |
| 64 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist, |
| 65 | &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements)) |
| 66 | { |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | if (factory == NULL) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 71 | factory = (PyObject*)&pysqlite_ConnectionType; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | result = PyObject_Call(factory, args, kwargs); |
| 75 | |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | static PyObject* module_complete(PyObject* self, PyObject* args, PyObject* |
| 80 | kwargs) |
| 81 | { |
| 82 | static char *kwlist[] = {"statement", NULL, NULL}; |
| 83 | char* statement; |
| 84 | |
| 85 | PyObject* result; |
| 86 | |
| 87 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement)) |
| 88 | { |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | if (sqlite3_complete(statement)) { |
| 93 | result = Py_True; |
| 94 | } else { |
| 95 | result = Py_False; |
| 96 | } |
| 97 | |
| 98 | Py_INCREF(result); |
| 99 | |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | #ifdef HAVE_SHARED_CACHE |
| 104 | static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject* |
| 105 | kwargs) |
| 106 | { |
| 107 | static char *kwlist[] = {"do_enable", NULL, NULL}; |
| 108 | int do_enable; |
| 109 | int rc; |
| 110 | |
| 111 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable)) |
| 112 | { |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | rc = sqlite3_enable_shared_cache(do_enable); |
| 117 | |
| 118 | if (rc != SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 119 | PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 120 | return NULL; |
| 121 | } else { |
| 122 | Py_INCREF(Py_None); |
| 123 | return Py_None; |
| 124 | } |
| 125 | } |
| 126 | #endif /* HAVE_SHARED_CACHE */ |
| 127 | |
| 128 | static PyObject* module_register_adapter(PyObject* self, PyObject* args, PyObject* kwargs) |
| 129 | { |
| 130 | PyTypeObject* type; |
| 131 | PyObject* caster; |
| 132 | |
| 133 | if (!PyArg_ParseTuple(args, "OO", &type, &caster)) { |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 137 | /* a basic type is adapted; there's a performance optimization if that's not the case |
| 138 | * (99 % of all usages) */ |
| 139 | if (type == &PyLong_Type || type == &PyFloat_Type |
| 140 | || type == &PyUnicode_Type || type == &PyBytes_Type) { |
| 141 | pysqlite_BaseTypeAdapted = 1; |
| 142 | } |
| 143 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 144 | microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 145 | |
| 146 | Py_INCREF(Py_None); |
| 147 | return Py_None; |
| 148 | } |
| 149 | |
| 150 | static PyObject* module_register_converter(PyObject* self, PyObject* args, PyObject* kwargs) |
| 151 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 152 | PyObject* orig_name; |
| 153 | PyObject* name = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 154 | PyObject* callable; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 155 | PyObject* retval = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 157 | if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
| 160 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 161 | /* convert the name to upper case */ |
| 162 | name = PyObject_CallMethod(orig_name, "upper", ""); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 163 | if (!name) { |
| 164 | goto error; |
| 165 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 166 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 167 | if (PyDict_SetItem(converters, name, callable) != 0) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | Py_INCREF(Py_None); |
| 172 | retval = Py_None; |
| 173 | error: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 174 | Py_XDECREF(name); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 175 | return retval; |
| 176 | } |
| 177 | |
| 178 | static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args, PyObject* kwargs) |
| 179 | { |
| 180 | if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | Py_INCREF(Py_None); |
| 185 | return Py_None; |
| 186 | } |
| 187 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 188 | static void converters_init(PyObject* dict) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 189 | { |
| 190 | converters = PyDict_New(); |
| 191 | if (!converters) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | PyDict_SetItemString(dict, "converters", converters); |
| 196 | } |
| 197 | |
| 198 | static PyMethodDef module_methods[] = { |
| 199 | {"connect", (PyCFunction)module_connect, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Creates a connection.")}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 200 | {"complete_statement", (PyCFunction)module_complete, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Checks if a string contains a complete SQL statement. Non-standard.")}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | #ifdef HAVE_SHARED_CACHE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 202 | {"enable_shared_cache", (PyCFunction)module_enable_shared_cache, METH_VARARGS|METH_KEYWORDS, PyDoc_STR("Enable or disable shared cache mode for the calling thread. Experimental/Non-standard.")}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 203 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 204 | {"register_adapter", (PyCFunction)module_register_adapter, METH_VARARGS, PyDoc_STR("Registers an adapter with pysqlite's adapter registry. Non-standard.")}, |
| 205 | {"register_converter", (PyCFunction)module_register_converter, METH_VARARGS, PyDoc_STR("Registers a converter with pysqlite. Non-standard.")}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 206 | {"adapt", (PyCFunction)psyco_microprotocols_adapt, METH_VARARGS, psyco_microprotocols_adapt_doc}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 207 | {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks, METH_VARARGS, PyDoc_STR("Enable or disable callback functions throwing errors to stderr.")}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 208 | {NULL, NULL} |
| 209 | }; |
| 210 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 211 | struct _IntConstantPair { |
| 212 | char* constant_name; |
| 213 | int constant_value; |
| 214 | }; |
| 215 | |
| 216 | typedef struct _IntConstantPair IntConstantPair; |
| 217 | |
| 218 | static IntConstantPair _int_constants[] = { |
| 219 | {"PARSE_DECLTYPES", PARSE_DECLTYPES}, |
| 220 | {"PARSE_COLNAMES", PARSE_COLNAMES}, |
| 221 | |
| 222 | {"SQLITE_OK", SQLITE_OK}, |
| 223 | {"SQLITE_DENY", SQLITE_DENY}, |
| 224 | {"SQLITE_IGNORE", SQLITE_IGNORE}, |
| 225 | {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX}, |
| 226 | {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE}, |
| 227 | {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX}, |
| 228 | {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE}, |
| 229 | {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER}, |
| 230 | {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW}, |
| 231 | {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER}, |
| 232 | {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW}, |
| 233 | {"SQLITE_DELETE", SQLITE_DELETE}, |
| 234 | {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX}, |
| 235 | {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE}, |
| 236 | {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX}, |
| 237 | {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE}, |
| 238 | {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER}, |
| 239 | {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW}, |
| 240 | {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER}, |
| 241 | {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW}, |
| 242 | {"SQLITE_INSERT", SQLITE_INSERT}, |
| 243 | {"SQLITE_PRAGMA", SQLITE_PRAGMA}, |
| 244 | {"SQLITE_READ", SQLITE_READ}, |
| 245 | {"SQLITE_SELECT", SQLITE_SELECT}, |
| 246 | {"SQLITE_TRANSACTION", SQLITE_TRANSACTION}, |
| 247 | {"SQLITE_UPDATE", SQLITE_UPDATE}, |
| 248 | {"SQLITE_ATTACH", SQLITE_ATTACH}, |
| 249 | {"SQLITE_DETACH", SQLITE_DETACH}, |
| 250 | #if SQLITE_VERSION_NUMBER >= 3002001 |
| 251 | {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE}, |
| 252 | {"SQLITE_REINDEX", SQLITE_REINDEX}, |
| 253 | #endif |
| 254 | #if SQLITE_VERSION_NUMBER >= 3003000 |
| 255 | {"SQLITE_ANALYZE", SQLITE_ANALYZE}, |
| 256 | #endif |
| 257 | {(char*)NULL, 0} |
| 258 | }; |
| 259 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | PyMODINIT_FUNC init_sqlite3(void) |
| 261 | { |
| 262 | PyObject *module, *dict; |
| 263 | PyObject *tmp_obj; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 264 | int i; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 265 | |
| 266 | module = Py_InitModule("_sqlite3", module_methods); |
| 267 | |
| 268 | if (!module || |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 269 | (pysqlite_row_setup_types() < 0) || |
| 270 | (pysqlite_cursor_setup_types() < 0) || |
| 271 | (pysqlite_connection_setup_types() < 0) || |
| 272 | (pysqlite_cache_setup_types() < 0) || |
| 273 | (pysqlite_statement_setup_types() < 0) || |
| 274 | (pysqlite_prepare_protocol_setup_types() < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 275 | ) { |
| 276 | return; |
| 277 | } |
| 278 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 279 | Py_INCREF(&pysqlite_ConnectionType); |
| 280 | PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType); |
| 281 | Py_INCREF(&pysqlite_CursorType); |
| 282 | PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType); |
| 283 | Py_INCREF(&pysqlite_CacheType); |
| 284 | PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType); |
| 285 | Py_INCREF(&pysqlite_StatementType); |
| 286 | PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType); |
| 287 | Py_INCREF(&pysqlite_PrepareProtocolType); |
| 288 | PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType); |
| 289 | Py_INCREF(&pysqlite_RowType); |
| 290 | PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 291 | |
| 292 | if (!(dict = PyModule_GetDict(module))) { |
| 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | /*** Create DB-API Exception hierarchy */ |
| 297 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 298 | if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 299 | goto error; |
| 300 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 301 | PyDict_SetItemString(dict, "Error", pysqlite_Error); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 303 | if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 304 | goto error; |
| 305 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 306 | PyDict_SetItemString(dict, "Warning", pysqlite_Warning); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 307 | |
| 308 | /* Error subclasses */ |
| 309 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 310 | if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 311 | goto error; |
| 312 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 313 | PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 314 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 315 | if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 316 | goto error; |
| 317 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 318 | PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 319 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 320 | /* pysqlite_DatabaseError subclasses */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 321 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 322 | if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 323 | goto error; |
| 324 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 325 | PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 326 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 327 | if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 328 | goto error; |
| 329 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 330 | PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 331 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 332 | if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 333 | goto error; |
| 334 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 335 | PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 336 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 337 | if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 338 | goto error; |
| 339 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 340 | PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 341 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 342 | if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | goto error; |
| 344 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 345 | PyDict_SetItemString(dict, "DataError", pysqlite_DataError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 347 | if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 348 | goto error; |
| 349 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 350 | PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 351 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 352 | /* We just need "something" unique for pysqlite_OptimizedUnicode. It does not really |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 353 | * need to be a string subclass. Just anything that can act as a special |
| 354 | * marker for us. So I pulled PyCell_Type out of my magic hat. |
| 355 | */ |
| 356 | Py_INCREF((PyObject*)&PyCell_Type); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 357 | pysqlite_OptimizedUnicode = (PyObject*)&PyCell_Type; |
| 358 | PyDict_SetItemString(dict, "OptimizedUnicode", pysqlite_OptimizedUnicode); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 359 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 360 | /* Set integer constants */ |
| 361 | for (i = 0; _int_constants[i].constant_name != 0; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 362 | tmp_obj = PyLong_FromLong(_int_constants[i].constant_value); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 363 | if (!tmp_obj) { |
| 364 | goto error; |
| 365 | } |
| 366 | PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj); |
| 367 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 368 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 370 | if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 371 | goto error; |
| 372 | } |
| 373 | PyDict_SetItemString(dict, "version", tmp_obj); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 374 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 376 | if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 377 | goto error; |
| 378 | } |
| 379 | PyDict_SetItemString(dict, "sqlite_version", tmp_obj); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 380 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | |
| 382 | /* initialize microprotocols layer */ |
| 383 | microprotocols_init(dict); |
| 384 | |
| 385 | /* initialize the default converters */ |
| 386 | converters_init(dict); |
| 387 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 388 | _enable_callback_tracebacks = 0; |
| 389 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 390 | pysqlite_BaseTypeAdapted = 0; |
| 391 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 392 | /* Original comment form _bsddb.c in the Python core. This is also still |
| 393 | * needed nowadays for Python 2.3/2.4. |
| 394 | * |
| 395 | * PyEval_InitThreads is called here due to a quirk in python 1.5 |
| 396 | * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>: |
| 397 | * The global interepreter lock is not initialized until the first |
| 398 | * thread is created using thread.start_new_thread() or fork() is |
| 399 | * called. that would cause the ALLOW_THREADS here to segfault due |
| 400 | * to a null pointer reference if no threads or child processes |
| 401 | * have been created. This works around that and is a no-op if |
| 402 | * threads have already been initialized. |
| 403 | * (see pybsddb-users mailing list post on 2002-08-07) |
| 404 | */ |
| 405 | PyEval_InitThreads(); |
| 406 | |
| 407 | error: |
| 408 | if (PyErr_Occurred()) |
| 409 | { |
| 410 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed"); |
| 411 | } |
| 412 | } |