blob: 7a7e86040abe10854430a2616bd1750ab7355e0a [file] [log] [blame]
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001/* module.c - the module itself
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Gerhard Häringe7ea7452008-03-29 00:45:29 +00004 *
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 Wouters49fd7fa2006-04-21 10:40:58 +000023
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 Woutersfc7bb8c2007-01-15 15:49:28 +000038PyObject* pysqlite_Error, *pysqlite_Warning, *pysqlite_InterfaceError, *pysqlite_DatabaseError,
39 *pysqlite_InternalError, *pysqlite_OperationalError, *pysqlite_ProgrammingError,
Petri Lehtinenbc35beb2012-02-09 21:09:03 +020040 *pysqlite_IntegrityError, *pysqlite_DataError, *pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041
42PyObject* converters;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043int _enable_callback_tracebacks;
Gerhard Häringe7ea7452008-03-29 00:45:29 +000044int pysqlite_BaseTypeAdapted;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045
46static 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
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010053 static char *kwlist[] = {
54 "database", "timeout", "detect_types", "isolation_level",
55 "check_same_thread", "factory", "cached_statements", "uri",
56 NULL
57 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 char* database;
59 int detect_types = 0;
60 PyObject* isolation_level;
61 PyObject* factory = NULL;
62 int check_same_thread = 1;
63 int cached_statements;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010064 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000065 double timeout = 5.0;
66
67 PyObject* result;
68
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010069 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist,
70 &database, &timeout, &detect_types,
71 &isolation_level, &check_same_thread,
72 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075 }
76
77 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000078 factory = (PyObject*)&pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000079 }
80
81 result = PyObject_Call(factory, args, kwargs);
82
83 return result;
84}
85
Benjamin Petersondcf97b92008-07-02 17:30:14 +000086PyDoc_STRVAR(module_connect_doc,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010087"connect(database[, timeout, detect_types, isolation_level,\n\
88 check_same_thread, factory, cached_statements, uri])\n\
Benjamin Petersondcf97b92008-07-02 17:30:14 +000089\n\
90Opens a connection to the SQLite database file *database*. You can use\n\
91\":memory:\" to open a database connection to a database that resides in\n\
92RAM instead of on disk.");
93
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094static PyObject* module_complete(PyObject* self, PyObject* args, PyObject*
95 kwargs)
96{
97 static char *kwlist[] = {"statement", NULL, NULL};
98 char* statement;
99
100 PyObject* result;
101
102 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement))
103 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105 }
106
107 if (sqlite3_complete(statement)) {
108 result = Py_True;
109 } else {
110 result = Py_False;
111 }
112
113 Py_INCREF(result);
114
115 return result;
116}
117
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000118PyDoc_STRVAR(module_complete_doc,
119"complete_statement(sql)\n\
120\n\
121Checks if a string contains a complete SQL statement. Non-standard.");
122
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123#ifdef HAVE_SHARED_CACHE
124static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject*
125 kwargs)
126{
127 static char *kwlist[] = {"do_enable", NULL, NULL};
128 int do_enable;
129 int rc;
130
131 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable))
132 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 }
135
136 rc = sqlite3_enable_shared_cache(do_enable);
137
138 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000139 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000140 return NULL;
141 } else {
142 Py_INCREF(Py_None);
143 return Py_None;
144 }
145}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000146
147PyDoc_STRVAR(module_enable_shared_cache_doc,
148"enable_shared_cache(do_enable)\n\
149\n\
150Enable or disable shared cache mode for the calling thread.\n\
151Experimental/Non-standard.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152#endif /* HAVE_SHARED_CACHE */
153
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000154static PyObject* module_register_adapter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155{
156 PyTypeObject* type;
157 PyObject* caster;
Georg Brandl3dbca812008-07-23 16:10:53 +0000158 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159
160 if (!PyArg_ParseTuple(args, "OO", &type, &caster)) {
161 return NULL;
162 }
163
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000164 /* a basic type is adapted; there's a performance optimization if that's not the case
165 * (99 % of all usages) */
166 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000167 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000168 pysqlite_BaseTypeAdapted = 1;
169 }
170
Benjamin Petersond7b03282008-09-13 15:58:53 +0000171 rc = pysqlite_microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000172 if (rc == -1)
173 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000174
175 Py_INCREF(Py_None);
176 return Py_None;
177}
178
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000179PyDoc_STRVAR(module_register_adapter_doc,
180"register_adapter(type, callable)\n\
181\n\
182Registers an adapter with pysqlite's adapter registry. Non-standard.");
183
184static PyObject* module_register_converter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000186 PyObject* orig_name;
187 PyObject* name = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188 PyObject* callable;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000189 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200190 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191
Guido van Rossum98297ee2007-11-06 21:34:58 +0000192 if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193 return NULL;
194 }
195
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000196 /* convert the name to upper case */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200197 name = _PyObject_CallMethodId(orig_name, &PyId_upper, "");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000198 if (!name) {
199 goto error;
200 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000201
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000202 if (PyDict_SetItem(converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203 goto error;
204 }
205
206 Py_INCREF(Py_None);
207 retval = Py_None;
208error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000209 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210 return retval;
211}
212
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000213PyDoc_STRVAR(module_register_converter_doc,
214"register_converter(typename, callable)\n\
215\n\
216Registers a converter with pysqlite. Non-standard.");
217
218static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000219{
220 if (!PyArg_ParseTuple(args, "i", &_enable_callback_tracebacks)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221 return NULL;
222 }
223
224 Py_INCREF(Py_None);
225 return Py_None;
226}
227
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000228PyDoc_STRVAR(enable_callback_tracebacks_doc,
229"enable_callback_tracebacks(flag)\n\
230\n\
231Enable or disable callback functions throwing errors to stderr.");
232
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000233static void converters_init(PyObject* dict)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000234{
235 converters = PyDict_New();
236 if (!converters) {
237 return;
238 }
239
240 PyDict_SetItemString(dict, "converters", converters);
241}
242
243static PyMethodDef module_methods[] = {
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000244 {"connect", (PyCFunction)module_connect,
245 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
246 {"complete_statement", (PyCFunction)module_complete,
247 METH_VARARGS | METH_KEYWORDS, module_complete_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248#ifdef HAVE_SHARED_CACHE
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000249 {"enable_shared_cache", (PyCFunction)module_enable_shared_cache,
250 METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251#endif
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000252 {"register_adapter", (PyCFunction)module_register_adapter,
253 METH_VARARGS, module_register_adapter_doc},
254 {"register_converter", (PyCFunction)module_register_converter,
255 METH_VARARGS, module_register_converter_doc},
Benjamin Petersond7b03282008-09-13 15:58:53 +0000256 {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS,
257 pysqlite_adapt_doc},
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000258 {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks,
259 METH_VARARGS, enable_callback_tracebacks_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260 {NULL, NULL}
261};
262
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263struct _IntConstantPair {
264 char* constant_name;
265 int constant_value;
266};
267
268typedef struct _IntConstantPair IntConstantPair;
269
270static IntConstantPair _int_constants[] = {
271 {"PARSE_DECLTYPES", PARSE_DECLTYPES},
272 {"PARSE_COLNAMES", PARSE_COLNAMES},
273
274 {"SQLITE_OK", SQLITE_OK},
275 {"SQLITE_DENY", SQLITE_DENY},
276 {"SQLITE_IGNORE", SQLITE_IGNORE},
277 {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX},
278 {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE},
279 {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX},
280 {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE},
281 {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER},
282 {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW},
283 {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER},
284 {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW},
285 {"SQLITE_DELETE", SQLITE_DELETE},
286 {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX},
287 {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE},
288 {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX},
289 {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE},
290 {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER},
291 {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW},
292 {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER},
293 {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW},
294 {"SQLITE_INSERT", SQLITE_INSERT},
295 {"SQLITE_PRAGMA", SQLITE_PRAGMA},
296 {"SQLITE_READ", SQLITE_READ},
297 {"SQLITE_SELECT", SQLITE_SELECT},
298 {"SQLITE_TRANSACTION", SQLITE_TRANSACTION},
299 {"SQLITE_UPDATE", SQLITE_UPDATE},
300 {"SQLITE_ATTACH", SQLITE_ATTACH},
301 {"SQLITE_DETACH", SQLITE_DETACH},
302#if SQLITE_VERSION_NUMBER >= 3002001
303 {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE},
304 {"SQLITE_REINDEX", SQLITE_REINDEX},
305#endif
306#if SQLITE_VERSION_NUMBER >= 3003000
307 {"SQLITE_ANALYZE", SQLITE_ANALYZE},
308#endif
309 {(char*)NULL, 0}
310};
311
Martin v. Löwis1a214512008-06-11 05:26:20 +0000312
313static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 PyModuleDef_HEAD_INIT,
315 "_sqlite3",
316 NULL,
317 -1,
318 module_methods,
319 NULL,
320 NULL,
321 NULL,
322 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000323};
324
325PyMODINIT_FUNC PyInit__sqlite3(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326{
327 PyObject *module, *dict;
328 PyObject *tmp_obj;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000329 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330
Martin v. Löwis1a214512008-06-11 05:26:20 +0000331 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332
333 if (!module ||
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000334 (pysqlite_row_setup_types() < 0) ||
335 (pysqlite_cursor_setup_types() < 0) ||
336 (pysqlite_connection_setup_types() < 0) ||
337 (pysqlite_cache_setup_types() < 0) ||
338 (pysqlite_statement_setup_types() < 0) ||
339 (pysqlite_prepare_protocol_setup_types() < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340 ) {
Brett Cannone1445072011-02-04 20:24:02 +0000341 Py_XDECREF(module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000342 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 }
344
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000345 Py_INCREF(&pysqlite_ConnectionType);
346 PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType);
347 Py_INCREF(&pysqlite_CursorType);
348 PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType);
349 Py_INCREF(&pysqlite_CacheType);
350 PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType);
351 Py_INCREF(&pysqlite_StatementType);
352 PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType);
353 Py_INCREF(&pysqlite_PrepareProtocolType);
354 PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType);
355 Py_INCREF(&pysqlite_RowType);
356 PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000357
358 if (!(dict = PyModule_GetDict(module))) {
359 goto error;
360 }
361
362 /*** Create DB-API Exception hierarchy */
363
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000364 if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365 goto error;
366 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000367 PyDict_SetItemString(dict, "Error", pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000369 if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 goto error;
371 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000372 PyDict_SetItemString(dict, "Warning", pysqlite_Warning);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373
374 /* Error subclasses */
375
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000376 if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377 goto error;
378 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000379 PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000381 if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 goto error;
383 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000384 PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000386 /* pysqlite_DatabaseError subclasses */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000387
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000388 if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000389 goto error;
390 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000391 PyDict_SetItemString(dict, "InternalError", pysqlite_InternalError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000393 if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 goto error;
395 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000396 PyDict_SetItemString(dict, "OperationalError", pysqlite_OperationalError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000398 if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399 goto error;
400 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000401 PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000403 if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 goto error;
405 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000406 PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000408 if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 goto error;
410 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000411 PyDict_SetItemString(dict, "DataError", pysqlite_DataError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000413 if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414 goto error;
415 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000416 PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200418 /* In Python 2.x, setting Connection.text_factory to
419 OptimizedUnicode caused Unicode objects to be returned for
420 non-ASCII data and bytestrings to be returned for ASCII data.
421 Now OptimizedUnicode is an alias for str, so it has no
422 effect. */
423 Py_INCREF((PyObject*)&PyUnicode_Type);
424 PyDict_SetItemString(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000426 /* Set integer constants */
427 for (i = 0; _int_constants[i].constant_name != 0; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000428 tmp_obj = PyLong_FromLong(_int_constants[i].constant_value);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000429 if (!tmp_obj) {
430 goto error;
431 }
432 PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj);
433 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435
Neal Norwitzefee9f52007-10-27 02:50:52 +0000436 if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 goto error;
438 }
439 PyDict_SetItemString(dict, "version", tmp_obj);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000440 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441
Neal Norwitzefee9f52007-10-27 02:50:52 +0000442 if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443 goto error;
444 }
445 PyDict_SetItemString(dict, "sqlite_version", tmp_obj);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000446 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000447
448 /* initialize microprotocols layer */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000449 pysqlite_microprotocols_init(dict);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000450
451 /* initialize the default converters */
452 converters_init(dict);
453
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000454 _enable_callback_tracebacks = 0;
455
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000456 pysqlite_BaseTypeAdapted = 0;
457
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000458 /* Original comment from _bsddb.c in the Python core. This is also still
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459 * needed nowadays for Python 2.3/2.4.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 * PyEval_InitThreads is called here due to a quirk in python 1.5
462 * - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>:
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000463 * The global interpreter lock is not initialized until the first
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 * thread is created using thread.start_new_thread() or fork() is
465 * called. that would cause the ALLOW_THREADS here to segfault due
466 * to a null pointer reference if no threads or child processes
467 * have been created. This works around that and is a no-op if
468 * threads have already been initialized.
469 * (see pybsddb-users mailing list post on 2002-08-07)
470 */
Georg Brandldfd73442009-04-05 11:47:34 +0000471#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000472 PyEval_InitThreads();
Georg Brandldfd73442009-04-05 11:47:34 +0000473#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474
475error:
476 if (PyErr_Occurred())
477 {
478 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 Py_DECREF(module);
480 module = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000481 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000482 return module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483}