Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 1 | /* module.c - the module itself |
| 2 | * |
Florent Xicluna | c934f32 | 2010-09-03 23:47:32 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 79 | PyDoc_STRVAR(module_connect_doc, |
| 80 | "connect(database[, timeout, isolation_level, detect_types, factory])\n\ |
| 81 | \n\ |
| 82 | Opens a connection to the SQLite database file *database*. You can use\n\ |
| 83 | \":memory:\" to open a database connection to a database that resides in\n\ |
| 84 | RAM instead of on disk."); |
| 85 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | static PyObject* module_complete(PyObject* self, PyObject* args, PyObject* |
| 87 | kwargs) |
| 88 | { |
| 89 | static char *kwlist[] = {"statement", NULL, NULL}; |
| 90 | char* statement; |
| 91 | |
| 92 | PyObject* result; |
| 93 | |
| 94 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement)) |
| 95 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if (sqlite3_complete(statement)) { |
| 100 | result = Py_True; |
| 101 | } else { |
| 102 | result = Py_False; |
| 103 | } |
| 104 | |
| 105 | Py_INCREF(result); |
| 106 | |
| 107 | return result; |
| 108 | } |
| 109 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 110 | PyDoc_STRVAR(module_complete_doc, |
| 111 | "complete_statement(sql)\n\ |
| 112 | \n\ |
| 113 | Checks if a string contains a complete SQL statement. Non-standard."); |
| 114 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 115 | #ifdef HAVE_SHARED_CACHE |
| 116 | static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject* |
| 117 | kwargs) |
| 118 | { |
| 119 | static char *kwlist[] = {"do_enable", NULL, NULL}; |
| 120 | int do_enable; |
| 121 | int rc; |
| 122 | |
| 123 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable)) |
| 124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | rc = sqlite3_enable_shared_cache(do_enable); |
| 129 | |
| 130 | if (rc != SQLITE_OK) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 131 | PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | } else { |
| 134 | Py_INCREF(Py_None); |
| 135 | return Py_None; |
| 136 | } |
| 137 | } |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 138 | |
| 139 | PyDoc_STRVAR(module_enable_shared_cache_doc, |
| 140 | "enable_shared_cache(do_enable)\n\ |
| 141 | \n\ |
| 142 | Enable or disable shared cache mode for the calling thread.\n\ |
| 143 | Experimental/Non-standard."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 144 | #endif /* HAVE_SHARED_CACHE */ |
| 145 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 146 | static PyObject* module_register_adapter(PyObject* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 147 | { |
| 148 | PyTypeObject* type; |
| 149 | PyObject* caster; |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 150 | int rc; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 151 | |
| 152 | if (!PyArg_ParseTuple(args, "OO", &type, &caster)) { |
| 153 | return NULL; |
| 154 | } |
| 155 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 156 | /* a basic type is adapted; there's a performance optimization if that's not the case |
| 157 | * (99 % of all usages) */ |
| 158 | if (type == &PyLong_Type || type == &PyFloat_Type |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 159 | || type == &PyUnicode_Type || type == &PyByteArray_Type) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 160 | pysqlite_BaseTypeAdapted = 1; |
| 161 | } |
| 162 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 163 | rc = pysqlite_microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 164 | if (rc == -1) |
| 165 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 166 | |
| 167 | Py_INCREF(Py_None); |
| 168 | return Py_None; |
| 169 | } |
| 170 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 171 | PyDoc_STRVAR(module_register_adapter_doc, |
| 172 | "register_adapter(type, callable)\n\ |
| 173 | \n\ |
| 174 | Registers an adapter with pysqlite's adapter registry. Non-standard."); |
| 175 | |
| 176 | static PyObject* module_register_converter(PyObject* self, PyObject* args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 177 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 178 | PyObject* orig_name; |
| 179 | PyObject* name = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 180 | PyObject* callable; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 181 | PyObject* retval = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 182 | _Py_IDENTIFIER(upper); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 184 | if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 185 | return NULL; |
| 186 | } |
| 187 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 188 | /* convert the name to upper case */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 189 | name = _PyObject_CallMethodId(orig_name, &PyId_upper, ""); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 190 | if (!name) { |
| 191 | goto error; |
| 192 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 193 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 194 | if (PyDict_SetItem(converters, name, callable) != 0) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | Py_INCREF(Py_None); |
| 199 | retval = Py_None; |
| 200 | error: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 201 | Py_XDECREF(name); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 202 | return retval; |
| 203 | } |
| 204 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 205 | PyDoc_STRVAR(module_register_converter_doc, |
| 206 | "register_converter(typename, callable)\n\ |
| 207 | \n\ |
| 208 | Registers a converter with pysqlite. Non-standard."); |
| 209 | |
| 210 | static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 211 | { |
| 212 | if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | Py_INCREF(Py_None); |
| 217 | return Py_None; |
| 218 | } |
| 219 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 220 | PyDoc_STRVAR(enable_callback_tracebacks_doc, |
| 221 | "enable_callback_tracebacks(flag)\n\ |
| 222 | \n\ |
| 223 | Enable or disable callback functions throwing errors to stderr."); |
| 224 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 225 | static void converters_init(PyObject* dict) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 226 | { |
| 227 | converters = PyDict_New(); |
| 228 | if (!converters) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | PyDict_SetItemString(dict, "converters", converters); |
| 233 | } |
| 234 | |
| 235 | static PyMethodDef module_methods[] = { |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 236 | {"connect", (PyCFunction)module_connect, |
| 237 | METH_VARARGS | METH_KEYWORDS, module_connect_doc}, |
| 238 | {"complete_statement", (PyCFunction)module_complete, |
| 239 | METH_VARARGS | METH_KEYWORDS, module_complete_doc}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 240 | #ifdef HAVE_SHARED_CACHE |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 241 | {"enable_shared_cache", (PyCFunction)module_enable_shared_cache, |
| 242 | METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 243 | #endif |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 244 | {"register_adapter", (PyCFunction)module_register_adapter, |
| 245 | METH_VARARGS, module_register_adapter_doc}, |
| 246 | {"register_converter", (PyCFunction)module_register_converter, |
| 247 | METH_VARARGS, module_register_converter_doc}, |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 248 | {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS, |
| 249 | pysqlite_adapt_doc}, |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 250 | {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks, |
| 251 | METH_VARARGS, enable_callback_tracebacks_doc}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 252 | {NULL, NULL} |
| 253 | }; |
| 254 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 255 | struct _IntConstantPair { |
| 256 | char* constant_name; |
| 257 | int constant_value; |
| 258 | }; |
| 259 | |
| 260 | typedef struct _IntConstantPair IntConstantPair; |
| 261 | |
| 262 | static IntConstantPair _int_constants[] = { |
| 263 | {"PARSE_DECLTYPES", PARSE_DECLTYPES}, |
| 264 | {"PARSE_COLNAMES", PARSE_COLNAMES}, |
| 265 | |
| 266 | {"SQLITE_OK", SQLITE_OK}, |
| 267 | {"SQLITE_DENY", SQLITE_DENY}, |
| 268 | {"SQLITE_IGNORE", SQLITE_IGNORE}, |
| 269 | {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX}, |
| 270 | {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE}, |
| 271 | {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX}, |
| 272 | {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE}, |
| 273 | {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER}, |
| 274 | {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW}, |
| 275 | {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER}, |
| 276 | {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW}, |
| 277 | {"SQLITE_DELETE", SQLITE_DELETE}, |
| 278 | {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX}, |
| 279 | {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE}, |
| 280 | {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX}, |
| 281 | {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE}, |
| 282 | {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER}, |
| 283 | {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW}, |
| 284 | {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER}, |
| 285 | {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW}, |
| 286 | {"SQLITE_INSERT", SQLITE_INSERT}, |
| 287 | {"SQLITE_PRAGMA", SQLITE_PRAGMA}, |
| 288 | {"SQLITE_READ", SQLITE_READ}, |
| 289 | {"SQLITE_SELECT", SQLITE_SELECT}, |
| 290 | {"SQLITE_TRANSACTION", SQLITE_TRANSACTION}, |
| 291 | {"SQLITE_UPDATE", SQLITE_UPDATE}, |
| 292 | {"SQLITE_ATTACH", SQLITE_ATTACH}, |
| 293 | {"SQLITE_DETACH", SQLITE_DETACH}, |
| 294 | #if SQLITE_VERSION_NUMBER >= 3002001 |
| 295 | {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE}, |
| 296 | {"SQLITE_REINDEX", SQLITE_REINDEX}, |
| 297 | #endif |
| 298 | #if SQLITE_VERSION_NUMBER >= 3003000 |
| 299 | {"SQLITE_ANALYZE", SQLITE_ANALYZE}, |
| 300 | #endif |
| 301 | {(char*)NULL, 0} |
| 302 | }; |
| 303 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 304 | |
| 305 | static struct PyModuleDef _sqlite3module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | PyModuleDef_HEAD_INIT, |
| 307 | "_sqlite3", |
| 308 | NULL, |
| 309 | -1, |
| 310 | module_methods, |
| 311 | NULL, |
| 312 | NULL, |
| 313 | NULL, |
| 314 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | PyMODINIT_FUNC PyInit__sqlite3(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 318 | { |
| 319 | PyObject *module, *dict; |
| 320 | PyObject *tmp_obj; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 321 | int i; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 322 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 323 | module = PyModule_Create(&_sqlite3module); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 324 | |
| 325 | if (!module || |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 326 | (pysqlite_row_setup_types() < 0) || |
| 327 | (pysqlite_cursor_setup_types() < 0) || |
| 328 | (pysqlite_connection_setup_types() < 0) || |
| 329 | (pysqlite_cache_setup_types() < 0) || |
| 330 | (pysqlite_statement_setup_types() < 0) || |
| 331 | (pysqlite_prepare_protocol_setup_types() < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 332 | ) { |
Brett Cannon | e144507 | 2011-02-04 20:24:02 +0000 | [diff] [blame] | 333 | Py_XDECREF(module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 334 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 337 | Py_INCREF(&pysqlite_ConnectionType); |
| 338 | PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType); |
| 339 | Py_INCREF(&pysqlite_CursorType); |
| 340 | PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType); |
| 341 | Py_INCREF(&pysqlite_CacheType); |
| 342 | PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType); |
| 343 | Py_INCREF(&pysqlite_StatementType); |
| 344 | PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType); |
| 345 | Py_INCREF(&pysqlite_PrepareProtocolType); |
| 346 | PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType); |
| 347 | Py_INCREF(&pysqlite_RowType); |
| 348 | PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 349 | |
| 350 | if (!(dict = PyModule_GetDict(module))) { |
| 351 | goto error; |
| 352 | } |
| 353 | |
| 354 | /*** Create DB-API Exception hierarchy */ |
| 355 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 356 | if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 357 | goto error; |
| 358 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 359 | PyDict_SetItemString(dict, "Error", pysqlite_Error); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 361 | if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 362 | goto error; |
| 363 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 364 | PyDict_SetItemString(dict, "Warning", pysqlite_Warning); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 365 | |
| 366 | /* Error subclasses */ |
| 367 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 368 | if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | goto error; |
| 370 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 371 | PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 372 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 373 | if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 374 | goto error; |
| 375 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 376 | PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 377 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 378 | /* pysqlite_DatabaseError subclasses */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 379 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 380 | if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 381 | goto error; |
| 382 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 383 | PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 384 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 385 | if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 386 | goto error; |
| 387 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 388 | PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 389 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 390 | if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 391 | goto error; |
| 392 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 393 | PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 394 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 395 | if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 396 | goto error; |
| 397 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 398 | PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 399 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 400 | if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 401 | goto error; |
| 402 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 403 | PyDict_SetItemString(dict, "DataError", pysqlite_DataError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 404 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 405 | if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 406 | goto error; |
| 407 | } |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 408 | PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 409 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 410 | /* We just need "something" unique for pysqlite_OptimizedUnicode. It does not really |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 411 | * need to be a string subclass. Just anything that can act as a special |
| 412 | * marker for us. So I pulled PyCell_Type out of my magic hat. |
| 413 | */ |
| 414 | Py_INCREF((PyObject*)&PyCell_Type); |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 415 | pysqlite_OptimizedUnicode = (PyObject*)&PyCell_Type; |
| 416 | PyDict_SetItemString(dict, "OptimizedUnicode", pysqlite_OptimizedUnicode); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 417 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 418 | /* Set integer constants */ |
| 419 | for (i = 0; _int_constants[i].constant_name != 0; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 420 | tmp_obj = PyLong_FromLong(_int_constants[i].constant_value); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 421 | if (!tmp_obj) { |
| 422 | goto error; |
| 423 | } |
| 424 | PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj); |
| 425 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 426 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 427 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 428 | if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 429 | goto error; |
| 430 | } |
| 431 | PyDict_SetItemString(dict, "version", tmp_obj); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 432 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 433 | |
Neal Norwitz | efee9f5 | 2007-10-27 02:50:52 +0000 | [diff] [blame] | 434 | if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 435 | goto error; |
| 436 | } |
| 437 | PyDict_SetItemString(dict, "sqlite_version", tmp_obj); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 438 | Py_DECREF(tmp_obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 439 | |
| 440 | /* initialize microprotocols layer */ |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 441 | pysqlite_microprotocols_init(dict); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 442 | |
| 443 | /* initialize the default converters */ |
| 444 | converters_init(dict); |
| 445 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 446 | _enable_callback_tracebacks = 0; |
| 447 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 448 | pysqlite_BaseTypeAdapted = 0; |
| 449 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 450 | /* Original comment from _bsddb.c in the Python core. This is also still |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 451 | * needed nowadays for Python 2.3/2.4. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 453 | * PyEval_InitThreads is called here due to a quirk in python 1.5 |
| 454 | * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>: |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 455 | * The global interpreter lock is not initialized until the first |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 456 | * thread is created using thread.start_new_thread() or fork() is |
| 457 | * called. that would cause the ALLOW_THREADS here to segfault due |
| 458 | * to a null pointer reference if no threads or child processes |
| 459 | * have been created. This works around that and is a no-op if |
| 460 | * threads have already been initialized. |
| 461 | * (see pybsddb-users mailing list post on 2002-08-07) |
| 462 | */ |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 463 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 464 | PyEval_InitThreads(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 465 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 466 | |
| 467 | error: |
| 468 | if (PyErr_Occurred()) |
| 469 | { |
| 470 | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | Py_DECREF(module); |
| 472 | module = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 473 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 474 | return module; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 475 | } |