| 1 | n/a | /* module.c - the module itself |
|---|
| 2 | n/a | * |
|---|
| 3 | n/a | * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
|---|
| 4 | n/a | * |
|---|
| 5 | n/a | * This file is part of pysqlite. |
|---|
| 6 | n/a | * |
|---|
| 7 | n/a | * This software is provided 'as-is', without any express or implied |
|---|
| 8 | n/a | * warranty. In no event will the authors be held liable for any damages |
|---|
| 9 | n/a | * arising from the use of this software. |
|---|
| 10 | n/a | * |
|---|
| 11 | n/a | * Permission is granted to anyone to use this software for any purpose, |
|---|
| 12 | n/a | * including commercial applications, and to alter it and redistribute it |
|---|
| 13 | n/a | * freely, subject to the following restrictions: |
|---|
| 14 | n/a | * |
|---|
| 15 | n/a | * 1. The origin of this software must not be misrepresented; you must not |
|---|
| 16 | n/a | * claim that you wrote the original software. If you use this software |
|---|
| 17 | n/a | * in a product, an acknowledgment in the product documentation would be |
|---|
| 18 | n/a | * appreciated but is not required. |
|---|
| 19 | n/a | * 2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 20 | n/a | * misrepresented as being the original software. |
|---|
| 21 | n/a | * 3. This notice may not be removed or altered from any source distribution. |
|---|
| 22 | n/a | */ |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | #include "connection.h" |
|---|
| 25 | n/a | #include "statement.h" |
|---|
| 26 | n/a | #include "cursor.h" |
|---|
| 27 | n/a | #include "cache.h" |
|---|
| 28 | n/a | #include "prepare_protocol.h" |
|---|
| 29 | n/a | #include "microprotocols.h" |
|---|
| 30 | n/a | #include "row.h" |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | #if SQLITE_VERSION_NUMBER >= 3003003 |
|---|
| 33 | n/a | #define HAVE_SHARED_CACHE |
|---|
| 34 | n/a | #endif |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | /* static objects at module-level */ |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | PyObject* pysqlite_Error, *pysqlite_Warning, *pysqlite_InterfaceError, *pysqlite_DatabaseError, |
|---|
| 39 | n/a | *pysqlite_InternalError, *pysqlite_OperationalError, *pysqlite_ProgrammingError, |
|---|
| 40 | n/a | *pysqlite_IntegrityError, *pysqlite_DataError, *pysqlite_NotSupportedError; |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | PyObject* converters; |
|---|
| 43 | n/a | int _enable_callback_tracebacks; |
|---|
| 44 | n/a | int pysqlite_BaseTypeAdapted; |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | static PyObject* module_connect(PyObject* self, PyObject* args, PyObject* |
|---|
| 47 | n/a | kwargs) |
|---|
| 48 | n/a | { |
|---|
| 49 | n/a | /* Python seems to have no way of extracting a single keyword-arg at |
|---|
| 50 | n/a | * C-level, so this code is redundant with the one in connection_init in |
|---|
| 51 | n/a | * connection.c and must always be copied from there ... */ |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | static char *kwlist[] = { |
|---|
| 54 | n/a | "database", "timeout", "detect_types", "isolation_level", |
|---|
| 55 | n/a | "check_same_thread", "factory", "cached_statements", "uri", |
|---|
| 56 | n/a | NULL |
|---|
| 57 | n/a | }; |
|---|
| 58 | n/a | char* database; |
|---|
| 59 | n/a | int detect_types = 0; |
|---|
| 60 | n/a | PyObject* isolation_level; |
|---|
| 61 | n/a | PyObject* factory = NULL; |
|---|
| 62 | n/a | int check_same_thread = 1; |
|---|
| 63 | n/a | int cached_statements; |
|---|
| 64 | n/a | int uri = 0; |
|---|
| 65 | n/a | double timeout = 5.0; |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | PyObject* result; |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist, |
|---|
| 70 | n/a | &database, &timeout, &detect_types, |
|---|
| 71 | n/a | &isolation_level, &check_same_thread, |
|---|
| 72 | n/a | &factory, &cached_statements, &uri)) |
|---|
| 73 | n/a | { |
|---|
| 74 | n/a | return NULL; |
|---|
| 75 | n/a | } |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | if (factory == NULL) { |
|---|
| 78 | n/a | factory = (PyObject*)&pysqlite_ConnectionType; |
|---|
| 79 | n/a | } |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | result = PyObject_Call(factory, args, kwargs); |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | return result; |
|---|
| 84 | n/a | } |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | PyDoc_STRVAR(module_connect_doc, |
|---|
| 87 | n/a | "connect(database[, timeout, detect_types, isolation_level,\n\ |
|---|
| 88 | n/a | check_same_thread, factory, cached_statements, uri])\n\ |
|---|
| 89 | n/a | \n\ |
|---|
| 90 | n/a | Opens a connection to the SQLite database file *database*. You can use\n\ |
|---|
| 91 | n/a | \":memory:\" to open a database connection to a database that resides in\n\ |
|---|
| 92 | n/a | RAM instead of on disk."); |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | static PyObject* module_complete(PyObject* self, PyObject* args, PyObject* |
|---|
| 95 | n/a | kwargs) |
|---|
| 96 | n/a | { |
|---|
| 97 | n/a | static char *kwlist[] = {"statement", NULL, NULL}; |
|---|
| 98 | n/a | char* statement; |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | PyObject* result; |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement)) |
|---|
| 103 | n/a | { |
|---|
| 104 | n/a | return NULL; |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | if (sqlite3_complete(statement)) { |
|---|
| 108 | n/a | result = Py_True; |
|---|
| 109 | n/a | } else { |
|---|
| 110 | n/a | result = Py_False; |
|---|
| 111 | n/a | } |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | Py_INCREF(result); |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | return result; |
|---|
| 116 | n/a | } |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | PyDoc_STRVAR(module_complete_doc, |
|---|
| 119 | n/a | "complete_statement(sql)\n\ |
|---|
| 120 | n/a | \n\ |
|---|
| 121 | n/a | Checks if a string contains a complete SQL statement. Non-standard."); |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | #ifdef HAVE_SHARED_CACHE |
|---|
| 124 | n/a | static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject* |
|---|
| 125 | n/a | kwargs) |
|---|
| 126 | n/a | { |
|---|
| 127 | n/a | static char *kwlist[] = {"do_enable", NULL, NULL}; |
|---|
| 128 | n/a | int do_enable; |
|---|
| 129 | n/a | int rc; |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable)) |
|---|
| 132 | n/a | { |
|---|
| 133 | n/a | return NULL; |
|---|
| 134 | n/a | } |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | rc = sqlite3_enable_shared_cache(do_enable); |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | if (rc != SQLITE_OK) { |
|---|
| 139 | n/a | PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed"); |
|---|
| 140 | n/a | return NULL; |
|---|
| 141 | n/a | } else { |
|---|
| 142 | n/a | Py_RETURN_NONE; |
|---|
| 143 | n/a | } |
|---|
| 144 | n/a | } |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | PyDoc_STRVAR(module_enable_shared_cache_doc, |
|---|
| 147 | n/a | "enable_shared_cache(do_enable)\n\ |
|---|
| 148 | n/a | \n\ |
|---|
| 149 | n/a | Enable or disable shared cache mode for the calling thread.\n\ |
|---|
| 150 | n/a | Experimental/Non-standard."); |
|---|
| 151 | n/a | #endif /* HAVE_SHARED_CACHE */ |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | static PyObject* module_register_adapter(PyObject* self, PyObject* args) |
|---|
| 154 | n/a | { |
|---|
| 155 | n/a | PyTypeObject* type; |
|---|
| 156 | n/a | PyObject* caster; |
|---|
| 157 | n/a | int rc; |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | if (!PyArg_ParseTuple(args, "OO", &type, &caster)) { |
|---|
| 160 | n/a | return NULL; |
|---|
| 161 | n/a | } |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | /* a basic type is adapted; there's a performance optimization if that's not the case |
|---|
| 164 | n/a | * (99 % of all usages) */ |
|---|
| 165 | n/a | if (type == &PyLong_Type || type == &PyFloat_Type |
|---|
| 166 | n/a | || type == &PyUnicode_Type || type == &PyByteArray_Type) { |
|---|
| 167 | n/a | pysqlite_BaseTypeAdapted = 1; |
|---|
| 168 | n/a | } |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | rc = pysqlite_microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster); |
|---|
| 171 | n/a | if (rc == -1) |
|---|
| 172 | n/a | return NULL; |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | Py_RETURN_NONE; |
|---|
| 175 | n/a | } |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | PyDoc_STRVAR(module_register_adapter_doc, |
|---|
| 178 | n/a | "register_adapter(type, callable)\n\ |
|---|
| 179 | n/a | \n\ |
|---|
| 180 | n/a | Registers an adapter with pysqlite's adapter registry. Non-standard."); |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | static PyObject* module_register_converter(PyObject* self, PyObject* args) |
|---|
| 183 | n/a | { |
|---|
| 184 | n/a | PyObject* orig_name; |
|---|
| 185 | n/a | PyObject* name = NULL; |
|---|
| 186 | n/a | PyObject* callable; |
|---|
| 187 | n/a | PyObject* retval = NULL; |
|---|
| 188 | n/a | _Py_IDENTIFIER(upper); |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) { |
|---|
| 191 | n/a | return NULL; |
|---|
| 192 | n/a | } |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | /* convert the name to upper case */ |
|---|
| 195 | n/a | name = _PyObject_CallMethodId(orig_name, &PyId_upper, NULL); |
|---|
| 196 | n/a | if (!name) { |
|---|
| 197 | n/a | goto error; |
|---|
| 198 | n/a | } |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | if (PyDict_SetItem(converters, name, callable) != 0) { |
|---|
| 201 | n/a | goto error; |
|---|
| 202 | n/a | } |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | Py_INCREF(Py_None); |
|---|
| 205 | n/a | retval = Py_None; |
|---|
| 206 | n/a | error: |
|---|
| 207 | n/a | Py_XDECREF(name); |
|---|
| 208 | n/a | return retval; |
|---|
| 209 | n/a | } |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | PyDoc_STRVAR(module_register_converter_doc, |
|---|
| 212 | n/a | "register_converter(typename, callable)\n\ |
|---|
| 213 | n/a | \n\ |
|---|
| 214 | n/a | Registers a converter with pysqlite. Non-standard."); |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args) |
|---|
| 217 | n/a | { |
|---|
| 218 | n/a | if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) { |
|---|
| 219 | n/a | return NULL; |
|---|
| 220 | n/a | } |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | Py_RETURN_NONE; |
|---|
| 223 | n/a | } |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | PyDoc_STRVAR(enable_callback_tracebacks_doc, |
|---|
| 226 | n/a | "enable_callback_tracebacks(flag)\n\ |
|---|
| 227 | n/a | \n\ |
|---|
| 228 | n/a | Enable or disable callback functions throwing errors to stderr."); |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | static void converters_init(PyObject* dict) |
|---|
| 231 | n/a | { |
|---|
| 232 | n/a | converters = PyDict_New(); |
|---|
| 233 | n/a | if (!converters) { |
|---|
| 234 | n/a | return; |
|---|
| 235 | n/a | } |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | PyDict_SetItemString(dict, "converters", converters); |
|---|
| 238 | n/a | } |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | static PyMethodDef module_methods[] = { |
|---|
| 241 | n/a | {"connect", (PyCFunction)module_connect, |
|---|
| 242 | n/a | METH_VARARGS | METH_KEYWORDS, module_connect_doc}, |
|---|
| 243 | n/a | {"complete_statement", (PyCFunction)module_complete, |
|---|
| 244 | n/a | METH_VARARGS | METH_KEYWORDS, module_complete_doc}, |
|---|
| 245 | n/a | #ifdef HAVE_SHARED_CACHE |
|---|
| 246 | n/a | {"enable_shared_cache", (PyCFunction)module_enable_shared_cache, |
|---|
| 247 | n/a | METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc}, |
|---|
| 248 | n/a | #endif |
|---|
| 249 | n/a | {"register_adapter", (PyCFunction)module_register_adapter, |
|---|
| 250 | n/a | METH_VARARGS, module_register_adapter_doc}, |
|---|
| 251 | n/a | {"register_converter", (PyCFunction)module_register_converter, |
|---|
| 252 | n/a | METH_VARARGS, module_register_converter_doc}, |
|---|
| 253 | n/a | {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS, |
|---|
| 254 | n/a | pysqlite_adapt_doc}, |
|---|
| 255 | n/a | {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks, |
|---|
| 256 | n/a | METH_VARARGS, enable_callback_tracebacks_doc}, |
|---|
| 257 | n/a | {NULL, NULL} |
|---|
| 258 | n/a | }; |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | struct _IntConstantPair { |
|---|
| 261 | n/a | const char *constant_name; |
|---|
| 262 | n/a | int constant_value; |
|---|
| 263 | n/a | }; |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | typedef struct _IntConstantPair IntConstantPair; |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | static const IntConstantPair _int_constants[] = { |
|---|
| 268 | n/a | {"PARSE_DECLTYPES", PARSE_DECLTYPES}, |
|---|
| 269 | n/a | {"PARSE_COLNAMES", PARSE_COLNAMES}, |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | {"SQLITE_OK", SQLITE_OK}, |
|---|
| 272 | n/a | {"SQLITE_DENY", SQLITE_DENY}, |
|---|
| 273 | n/a | {"SQLITE_IGNORE", SQLITE_IGNORE}, |
|---|
| 274 | n/a | {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX}, |
|---|
| 275 | n/a | {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE}, |
|---|
| 276 | n/a | {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX}, |
|---|
| 277 | n/a | {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE}, |
|---|
| 278 | n/a | {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER}, |
|---|
| 279 | n/a | {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW}, |
|---|
| 280 | n/a | {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER}, |
|---|
| 281 | n/a | {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW}, |
|---|
| 282 | n/a | {"SQLITE_DELETE", SQLITE_DELETE}, |
|---|
| 283 | n/a | {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX}, |
|---|
| 284 | n/a | {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE}, |
|---|
| 285 | n/a | {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX}, |
|---|
| 286 | n/a | {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE}, |
|---|
| 287 | n/a | {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER}, |
|---|
| 288 | n/a | {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW}, |
|---|
| 289 | n/a | {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER}, |
|---|
| 290 | n/a | {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW}, |
|---|
| 291 | n/a | {"SQLITE_INSERT", SQLITE_INSERT}, |
|---|
| 292 | n/a | {"SQLITE_PRAGMA", SQLITE_PRAGMA}, |
|---|
| 293 | n/a | {"SQLITE_READ", SQLITE_READ}, |
|---|
| 294 | n/a | {"SQLITE_SELECT", SQLITE_SELECT}, |
|---|
| 295 | n/a | {"SQLITE_TRANSACTION", SQLITE_TRANSACTION}, |
|---|
| 296 | n/a | {"SQLITE_UPDATE", SQLITE_UPDATE}, |
|---|
| 297 | n/a | {"SQLITE_ATTACH", SQLITE_ATTACH}, |
|---|
| 298 | n/a | {"SQLITE_DETACH", SQLITE_DETACH}, |
|---|
| 299 | n/a | #if SQLITE_VERSION_NUMBER >= 3002001 |
|---|
| 300 | n/a | {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE}, |
|---|
| 301 | n/a | {"SQLITE_REINDEX", SQLITE_REINDEX}, |
|---|
| 302 | n/a | #endif |
|---|
| 303 | n/a | #if SQLITE_VERSION_NUMBER >= 3003000 |
|---|
| 304 | n/a | {"SQLITE_ANALYZE", SQLITE_ANALYZE}, |
|---|
| 305 | n/a | #endif |
|---|
| 306 | n/a | #if SQLITE_VERSION_NUMBER >= 3003007 |
|---|
| 307 | n/a | {"SQLITE_CREATE_VTABLE", SQLITE_CREATE_VTABLE}, |
|---|
| 308 | n/a | {"SQLITE_DROP_VTABLE", SQLITE_DROP_VTABLE}, |
|---|
| 309 | n/a | #endif |
|---|
| 310 | n/a | #if SQLITE_VERSION_NUMBER >= 3003008 |
|---|
| 311 | n/a | {"SQLITE_FUNCTION", SQLITE_FUNCTION}, |
|---|
| 312 | n/a | #endif |
|---|
| 313 | n/a | #if SQLITE_VERSION_NUMBER >= 3006008 |
|---|
| 314 | n/a | {"SQLITE_SAVEPOINT", SQLITE_SAVEPOINT}, |
|---|
| 315 | n/a | #endif |
|---|
| 316 | n/a | #if SQLITE_VERSION_NUMBER >= 3008003 |
|---|
| 317 | n/a | {"SQLITE_RECURSIVE", SQLITE_RECURSIVE}, |
|---|
| 318 | n/a | #endif |
|---|
| 319 | n/a | {(char*)NULL, 0} |
|---|
| 320 | n/a | }; |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | |
|---|
| 323 | n/a | static struct PyModuleDef _sqlite3module = { |
|---|
| 324 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 325 | n/a | "_sqlite3", |
|---|
| 326 | n/a | NULL, |
|---|
| 327 | n/a | -1, |
|---|
| 328 | n/a | module_methods, |
|---|
| 329 | n/a | NULL, |
|---|
| 330 | n/a | NULL, |
|---|
| 331 | n/a | NULL, |
|---|
| 332 | n/a | NULL |
|---|
| 333 | n/a | }; |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | PyMODINIT_FUNC PyInit__sqlite3(void) |
|---|
| 336 | n/a | { |
|---|
| 337 | n/a | PyObject *module, *dict; |
|---|
| 338 | n/a | PyObject *tmp_obj; |
|---|
| 339 | n/a | int i; |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | module = PyModule_Create(&_sqlite3module); |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | if (!module || |
|---|
| 344 | n/a | (pysqlite_row_setup_types() < 0) || |
|---|
| 345 | n/a | (pysqlite_cursor_setup_types() < 0) || |
|---|
| 346 | n/a | (pysqlite_connection_setup_types() < 0) || |
|---|
| 347 | n/a | (pysqlite_cache_setup_types() < 0) || |
|---|
| 348 | n/a | (pysqlite_statement_setup_types() < 0) || |
|---|
| 349 | n/a | (pysqlite_prepare_protocol_setup_types() < 0) |
|---|
| 350 | n/a | ) { |
|---|
| 351 | n/a | Py_XDECREF(module); |
|---|
| 352 | n/a | return NULL; |
|---|
| 353 | n/a | } |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | Py_INCREF(&pysqlite_ConnectionType); |
|---|
| 356 | n/a | PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType); |
|---|
| 357 | n/a | Py_INCREF(&pysqlite_CursorType); |
|---|
| 358 | n/a | PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType); |
|---|
| 359 | n/a | Py_INCREF(&pysqlite_CacheType); |
|---|
| 360 | n/a | PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType); |
|---|
| 361 | n/a | Py_INCREF(&pysqlite_StatementType); |
|---|
| 362 | n/a | PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType); |
|---|
| 363 | n/a | Py_INCREF(&pysqlite_PrepareProtocolType); |
|---|
| 364 | n/a | PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType); |
|---|
| 365 | n/a | Py_INCREF(&pysqlite_RowType); |
|---|
| 366 | n/a | PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType); |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | if (!(dict = PyModule_GetDict(module))) { |
|---|
| 369 | n/a | goto error; |
|---|
| 370 | n/a | } |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | /*** Create DB-API Exception hierarchy */ |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) { |
|---|
| 375 | n/a | goto error; |
|---|
| 376 | n/a | } |
|---|
| 377 | n/a | PyDict_SetItemString(dict, "Error", pysqlite_Error); |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) { |
|---|
| 380 | n/a | goto error; |
|---|
| 381 | n/a | } |
|---|
| 382 | n/a | PyDict_SetItemString(dict, "Warning", pysqlite_Warning); |
|---|
| 383 | n/a | |
|---|
| 384 | n/a | /* Error subclasses */ |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) { |
|---|
| 387 | n/a | goto error; |
|---|
| 388 | n/a | } |
|---|
| 389 | n/a | PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError); |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) { |
|---|
| 392 | n/a | goto error; |
|---|
| 393 | n/a | } |
|---|
| 394 | n/a | PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError); |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | /* pysqlite_DatabaseError subclasses */ |
|---|
| 397 | n/a | |
|---|
| 398 | n/a | if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) { |
|---|
| 399 | n/a | goto error; |
|---|
| 400 | n/a | } |
|---|
| 401 | n/a | PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError); |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) { |
|---|
| 404 | n/a | goto error; |
|---|
| 405 | n/a | } |
|---|
| 406 | n/a | PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError); |
|---|
| 407 | n/a | |
|---|
| 408 | n/a | if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) { |
|---|
| 409 | n/a | goto error; |
|---|
| 410 | n/a | } |
|---|
| 411 | n/a | PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError); |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) { |
|---|
| 414 | n/a | goto error; |
|---|
| 415 | n/a | } |
|---|
| 416 | n/a | PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError); |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) { |
|---|
| 419 | n/a | goto error; |
|---|
| 420 | n/a | } |
|---|
| 421 | n/a | PyDict_SetItemString(dict, "DataError", pysqlite_DataError); |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) { |
|---|
| 424 | n/a | goto error; |
|---|
| 425 | n/a | } |
|---|
| 426 | n/a | PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError); |
|---|
| 427 | n/a | |
|---|
| 428 | n/a | /* In Python 2.x, setting Connection.text_factory to |
|---|
| 429 | n/a | OptimizedUnicode caused Unicode objects to be returned for |
|---|
| 430 | n/a | non-ASCII data and bytestrings to be returned for ASCII data. |
|---|
| 431 | n/a | Now OptimizedUnicode is an alias for str, so it has no |
|---|
| 432 | n/a | effect. */ |
|---|
| 433 | n/a | Py_INCREF((PyObject*)&PyUnicode_Type); |
|---|
| 434 | n/a | PyDict_SetItemString(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type); |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | /* Set integer constants */ |
|---|
| 437 | n/a | for (i = 0; _int_constants[i].constant_name != 0; i++) { |
|---|
| 438 | n/a | tmp_obj = PyLong_FromLong(_int_constants[i].constant_value); |
|---|
| 439 | n/a | if (!tmp_obj) { |
|---|
| 440 | n/a | goto error; |
|---|
| 441 | n/a | } |
|---|
| 442 | n/a | PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj); |
|---|
| 443 | n/a | Py_DECREF(tmp_obj); |
|---|
| 444 | n/a | } |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) { |
|---|
| 447 | n/a | goto error; |
|---|
| 448 | n/a | } |
|---|
| 449 | n/a | PyDict_SetItemString(dict, "version", tmp_obj); |
|---|
| 450 | n/a | Py_DECREF(tmp_obj); |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) { |
|---|
| 453 | n/a | goto error; |
|---|
| 454 | n/a | } |
|---|
| 455 | n/a | PyDict_SetItemString(dict, "sqlite_version", tmp_obj); |
|---|
| 456 | n/a | Py_DECREF(tmp_obj); |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | /* initialize microprotocols layer */ |
|---|
| 459 | n/a | pysqlite_microprotocols_init(dict); |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | /* initialize the default converters */ |
|---|
| 462 | n/a | converters_init(dict); |
|---|
| 463 | n/a | |
|---|
| 464 | n/a | _enable_callback_tracebacks = 0; |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | pysqlite_BaseTypeAdapted = 0; |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | /* Original comment from _bsddb.c in the Python core. This is also still |
|---|
| 469 | n/a | * needed nowadays for Python 2.3/2.4. |
|---|
| 470 | n/a | * |
|---|
| 471 | n/a | * PyEval_InitThreads is called here due to a quirk in python 1.5 |
|---|
| 472 | n/a | * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>: |
|---|
| 473 | n/a | * The global interpreter lock is not initialized until the first |
|---|
| 474 | n/a | * thread is created using thread.start_new_thread() or fork() is |
|---|
| 475 | n/a | * called. that would cause the ALLOW_THREADS here to segfault due |
|---|
| 476 | n/a | * to a null pointer reference if no threads or child processes |
|---|
| 477 | n/a | * have been created. This works around that and is a no-op if |
|---|
| 478 | n/a | * threads have already been initialized. |
|---|
| 479 | n/a | * (see pybsddb-users mailing list post on 2002-08-07) |
|---|
| 480 | n/a | */ |
|---|
| 481 | n/a | #ifdef WITH_THREAD |
|---|
| 482 | n/a | PyEval_InitThreads(); |
|---|
| 483 | n/a | #endif |
|---|
| 484 | n/a | |
|---|
| 485 | n/a | error: |
|---|
| 486 | n/a | if (PyErr_Occurred()) |
|---|
| 487 | n/a | { |
|---|
| 488 | n/a | PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed"); |
|---|
| 489 | n/a | Py_DECREF(module); |
|---|
| 490 | n/a | module = NULL; |
|---|
| 491 | n/a | } |
|---|
| 492 | n/a | return module; |
|---|
| 493 | n/a | } |
|---|