blob: 274ee13c375eeb4ac8caac44a6fa0c564f874821 [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
Benjamin Peterson0a37a302017-12-31 10:04:13 -080038PyObject *pysqlite_Error = NULL;
39PyObject *pysqlite_Warning = NULL;
40PyObject *pysqlite_InterfaceError = NULL;
41PyObject *pysqlite_DatabaseError = NULL;
42PyObject *pysqlite_InternalError = NULL;
43PyObject *pysqlite_OperationalError = NULL;
44PyObject *pysqlite_ProgrammingError = NULL;
45PyObject *pysqlite_IntegrityError = NULL;
46PyObject *pysqlite_DataError = NULL;
47PyObject *pysqlite_NotSupportedError = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048
Benjamin Peterson7762e4d2018-07-09 21:20:23 -070049PyObject* _pysqlite_converters = NULL;
50int _pysqlite_enable_callback_tracebacks = 0;
Benjamin Peterson0a37a302017-12-31 10:04:13 -080051int pysqlite_BaseTypeAdapted = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052
53static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
54 kwargs)
55{
56 /* Python seems to have no way of extracting a single keyword-arg at
57 * C-level, so this code is redundant with the one in connection_init in
58 * connection.c and must always be copied from there ... */
59
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010060 static char *kwlist[] = {
61 "database", "timeout", "detect_types", "isolation_level",
62 "check_same_thread", "factory", "cached_statements", "uri",
63 NULL
64 };
Anders Lorentsena22a1272017-11-07 01:47:43 +010065 PyObject* database;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066 int detect_types = 0;
67 PyObject* isolation_level;
68 PyObject* factory = NULL;
69 int check_same_thread = 1;
70 int cached_statements;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010071 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072 double timeout = 5.0;
73
74 PyObject* result;
75
Anders Lorentsena22a1272017-11-07 01:47:43 +010076 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010077 &database, &timeout, &detect_types,
78 &isolation_level, &check_same_thread,
79 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 }
83
84 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000085 factory = (PyObject*)&pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 }
87
88 result = PyObject_Call(factory, args, kwargs);
89
90 return result;
91}
92
Benjamin Petersondcf97b92008-07-02 17:30:14 +000093PyDoc_STRVAR(module_connect_doc,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010094"connect(database[, timeout, detect_types, isolation_level,\n\
95 check_same_thread, factory, cached_statements, uri])\n\
Benjamin Petersondcf97b92008-07-02 17:30:14 +000096\n\
97Opens a connection to the SQLite database file *database*. You can use\n\
98\":memory:\" to open a database connection to a database that resides in\n\
99RAM instead of on disk.");
100
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101static PyObject* module_complete(PyObject* self, PyObject* args, PyObject*
102 kwargs)
103{
104 static char *kwlist[] = {"statement", NULL, NULL};
105 char* statement;
106
107 PyObject* result;
108
109 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement))
110 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112 }
113
114 if (sqlite3_complete(statement)) {
115 result = Py_True;
116 } else {
117 result = Py_False;
118 }
119
120 Py_INCREF(result);
121
122 return result;
123}
124
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000125PyDoc_STRVAR(module_complete_doc,
126"complete_statement(sql)\n\
127\n\
128Checks if a string contains a complete SQL statement. Non-standard.");
129
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130#ifdef HAVE_SHARED_CACHE
131static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject*
132 kwargs)
133{
134 static char *kwlist[] = {"do_enable", NULL, NULL};
135 int do_enable;
136 int rc;
137
138 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable))
139 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141 }
142
143 rc = sqlite3_enable_shared_cache(do_enable);
144
145 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000146 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147 return NULL;
148 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +0300149 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 }
151}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000152
153PyDoc_STRVAR(module_enable_shared_cache_doc,
154"enable_shared_cache(do_enable)\n\
155\n\
156Enable or disable shared cache mode for the calling thread.\n\
157Experimental/Non-standard.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158#endif /* HAVE_SHARED_CACHE */
159
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000160static PyObject* module_register_adapter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161{
162 PyTypeObject* type;
163 PyObject* caster;
Georg Brandl3dbca812008-07-23 16:10:53 +0000164 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165
166 if (!PyArg_ParseTuple(args, "OO", &type, &caster)) {
167 return NULL;
168 }
169
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000170 /* a basic type is adapted; there's a performance optimization if that's not the case
171 * (99 % of all usages) */
172 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000173 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000174 pysqlite_BaseTypeAdapted = 1;
175 }
176
Benjamin Petersond7b03282008-09-13 15:58:53 +0000177 rc = pysqlite_microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000178 if (rc == -1)
179 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180
Berker Peksagfe21de92016-04-09 07:34:39 +0300181 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182}
183
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000184PyDoc_STRVAR(module_register_adapter_doc,
185"register_adapter(type, callable)\n\
186\n\
187Registers an adapter with pysqlite's adapter registry. Non-standard.");
188
189static PyObject* module_register_converter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000191 PyObject* orig_name;
192 PyObject* name = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193 PyObject* callable;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000194 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200195 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196
Guido van Rossum98297ee2007-11-06 21:34:58 +0000197 if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 return NULL;
199 }
200
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000201 /* convert the name to upper case */
Victor Stinner3466bde2016-09-05 18:16:01 -0700202 name = _PyObject_CallMethodId(orig_name, &PyId_upper, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203 if (!name) {
204 goto error;
205 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000206
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700207 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208 goto error;
209 }
210
211 Py_INCREF(Py_None);
212 retval = Py_None;
213error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000214 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000215 return retval;
216}
217
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000218PyDoc_STRVAR(module_register_converter_doc,
219"register_converter(typename, callable)\n\
220\n\
221Registers a converter with pysqlite. Non-standard.");
222
223static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000224{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700225 if (!PyArg_ParseTuple(args, "i", &_pysqlite_enable_callback_tracebacks)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226 return NULL;
227 }
228
Berker Peksagfe21de92016-04-09 07:34:39 +0300229 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230}
231
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000232PyDoc_STRVAR(enable_callback_tracebacks_doc,
233"enable_callback_tracebacks(flag)\n\
234\n\
235Enable or disable callback functions throwing errors to stderr.");
236
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000237static void converters_init(PyObject* dict)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700239 _pysqlite_converters = PyDict_New();
240 if (!_pysqlite_converters) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241 return;
242 }
243
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700244 PyDict_SetItemString(dict, "converters", _pysqlite_converters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245}
246
247static PyMethodDef module_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200248 {"connect", (PyCFunction)(void(*)(void))module_connect,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000249 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200250 {"complete_statement", (PyCFunction)(void(*)(void))module_complete,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000251 METH_VARARGS | METH_KEYWORDS, module_complete_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252#ifdef HAVE_SHARED_CACHE
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200253 {"enable_shared_cache", (PyCFunction)(void(*)(void))module_enable_shared_cache,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000254 METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255#endif
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000256 {"register_adapter", (PyCFunction)module_register_adapter,
257 METH_VARARGS, module_register_adapter_doc},
258 {"register_converter", (PyCFunction)module_register_converter,
259 METH_VARARGS, module_register_converter_doc},
Benjamin Petersond7b03282008-09-13 15:58:53 +0000260 {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS,
261 pysqlite_adapt_doc},
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000262 {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks,
263 METH_VARARGS, enable_callback_tracebacks_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 {NULL, NULL}
265};
266
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267struct _IntConstantPair {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200268 const char *constant_name;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000269 int constant_value;
270};
271
272typedef struct _IntConstantPair IntConstantPair;
273
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200274static const IntConstantPair _int_constants[] = {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275 {"PARSE_DECLTYPES", PARSE_DECLTYPES},
276 {"PARSE_COLNAMES", PARSE_COLNAMES},
277
278 {"SQLITE_OK", SQLITE_OK},
279 {"SQLITE_DENY", SQLITE_DENY},
280 {"SQLITE_IGNORE", SQLITE_IGNORE},
281 {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX},
282 {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE},
283 {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX},
284 {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE},
285 {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER},
286 {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW},
287 {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER},
288 {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW},
289 {"SQLITE_DELETE", SQLITE_DELETE},
290 {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX},
291 {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE},
292 {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX},
293 {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE},
294 {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER},
295 {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW},
296 {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER},
297 {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW},
298 {"SQLITE_INSERT", SQLITE_INSERT},
299 {"SQLITE_PRAGMA", SQLITE_PRAGMA},
300 {"SQLITE_READ", SQLITE_READ},
301 {"SQLITE_SELECT", SQLITE_SELECT},
302 {"SQLITE_TRANSACTION", SQLITE_TRANSACTION},
303 {"SQLITE_UPDATE", SQLITE_UPDATE},
304 {"SQLITE_ATTACH", SQLITE_ATTACH},
305 {"SQLITE_DETACH", SQLITE_DETACH},
306#if SQLITE_VERSION_NUMBER >= 3002001
307 {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE},
308 {"SQLITE_REINDEX", SQLITE_REINDEX},
309#endif
310#if SQLITE_VERSION_NUMBER >= 3003000
311 {"SQLITE_ANALYZE", SQLITE_ANALYZE},
312#endif
Berker Peksag00b1ae02017-01-02 06:38:10 +0300313#if SQLITE_VERSION_NUMBER >= 3003007
314 {"SQLITE_CREATE_VTABLE", SQLITE_CREATE_VTABLE},
315 {"SQLITE_DROP_VTABLE", SQLITE_DROP_VTABLE},
316#endif
317#if SQLITE_VERSION_NUMBER >= 3003008
318 {"SQLITE_FUNCTION", SQLITE_FUNCTION},
319#endif
320#if SQLITE_VERSION_NUMBER >= 3006008
321 {"SQLITE_SAVEPOINT", SQLITE_SAVEPOINT},
322#endif
323#if SQLITE_VERSION_NUMBER >= 3008003
324 {"SQLITE_RECURSIVE", SQLITE_RECURSIVE},
325#endif
Emanuele Gaifasd7aed412018-03-10 23:08:31 +0100326#if SQLITE_VERSION_NUMBER >= 3006011
327 {"SQLITE_DONE", SQLITE_DONE},
328#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000329 {(char*)NULL, 0}
330};
331
Martin v. Löwis1a214512008-06-11 05:26:20 +0000332
333static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 PyModuleDef_HEAD_INIT,
335 "_sqlite3",
336 NULL,
337 -1,
338 module_methods,
339 NULL,
340 NULL,
341 NULL,
342 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000343};
344
345PyMODINIT_FUNC PyInit__sqlite3(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346{
347 PyObject *module, *dict;
348 PyObject *tmp_obj;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000349 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000350
Martin v. Löwis1a214512008-06-11 05:26:20 +0000351 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352
353 if (!module ||
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000354 (pysqlite_row_setup_types() < 0) ||
355 (pysqlite_cursor_setup_types() < 0) ||
356 (pysqlite_connection_setup_types() < 0) ||
357 (pysqlite_cache_setup_types() < 0) ||
358 (pysqlite_statement_setup_types() < 0) ||
359 (pysqlite_prepare_protocol_setup_types() < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 ) {
Brett Cannone1445072011-02-04 20:24:02 +0000361 Py_XDECREF(module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000362 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 }
364
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000365 Py_INCREF(&pysqlite_ConnectionType);
366 PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType);
367 Py_INCREF(&pysqlite_CursorType);
368 PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType);
369 Py_INCREF(&pysqlite_CacheType);
370 PyModule_AddObject(module, "Statement", (PyObject*)&pysqlite_StatementType);
371 Py_INCREF(&pysqlite_StatementType);
372 PyModule_AddObject(module, "Cache", (PyObject*) &pysqlite_CacheType);
373 Py_INCREF(&pysqlite_PrepareProtocolType);
374 PyModule_AddObject(module, "PrepareProtocol", (PyObject*) &pysqlite_PrepareProtocolType);
375 Py_INCREF(&pysqlite_RowType);
376 PyModule_AddObject(module, "Row", (PyObject*) &pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377
378 if (!(dict = PyModule_GetDict(module))) {
379 goto error;
380 }
381
382 /*** Create DB-API Exception hierarchy */
383
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000384 if (!(pysqlite_Error = PyErr_NewException(MODULE_NAME ".Error", PyExc_Exception, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 goto error;
386 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000387 PyDict_SetItemString(dict, "Error", pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000389 if (!(pysqlite_Warning = PyErr_NewException(MODULE_NAME ".Warning", PyExc_Exception, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 goto error;
391 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000392 PyDict_SetItemString(dict, "Warning", pysqlite_Warning);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393
394 /* Error subclasses */
395
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000396 if (!(pysqlite_InterfaceError = PyErr_NewException(MODULE_NAME ".InterfaceError", pysqlite_Error, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397 goto error;
398 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000399 PyDict_SetItemString(dict, "InterfaceError", pysqlite_InterfaceError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000401 if (!(pysqlite_DatabaseError = PyErr_NewException(MODULE_NAME ".DatabaseError", pysqlite_Error, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402 goto error;
403 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000404 PyDict_SetItemString(dict, "DatabaseError", pysqlite_DatabaseError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000406 /* pysqlite_DatabaseError subclasses */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000408 if (!(pysqlite_InternalError = PyErr_NewException(MODULE_NAME ".InternalError", 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, "InternalError", pysqlite_InternalError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000413 if (!(pysqlite_OperationalError = PyErr_NewException(MODULE_NAME ".OperationalError", 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, "OperationalError", pysqlite_OperationalError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000418 if (!(pysqlite_ProgrammingError = PyErr_NewException(MODULE_NAME ".ProgrammingError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 goto error;
420 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000421 PyDict_SetItemString(dict, "ProgrammingError", pysqlite_ProgrammingError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000423 if (!(pysqlite_IntegrityError = PyErr_NewException(MODULE_NAME ".IntegrityError", pysqlite_DatabaseError,NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 goto error;
425 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000426 PyDict_SetItemString(dict, "IntegrityError", pysqlite_IntegrityError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000427
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000428 if (!(pysqlite_DataError = PyErr_NewException(MODULE_NAME ".DataError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000429 goto error;
430 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000431 PyDict_SetItemString(dict, "DataError", pysqlite_DataError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000433 if (!(pysqlite_NotSupportedError = PyErr_NewException(MODULE_NAME ".NotSupportedError", pysqlite_DatabaseError, NULL))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434 goto error;
435 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000436 PyDict_SetItemString(dict, "NotSupportedError", pysqlite_NotSupportedError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200438 /* In Python 2.x, setting Connection.text_factory to
439 OptimizedUnicode caused Unicode objects to be returned for
440 non-ASCII data and bytestrings to be returned for ASCII data.
441 Now OptimizedUnicode is an alias for str, so it has no
442 effect. */
443 Py_INCREF((PyObject*)&PyUnicode_Type);
444 PyDict_SetItemString(dict, "OptimizedUnicode", (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000446 /* Set integer constants */
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200447 for (i = 0; _int_constants[i].constant_name != NULL; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000448 tmp_obj = PyLong_FromLong(_int_constants[i].constant_value);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449 if (!tmp_obj) {
450 goto error;
451 }
452 PyDict_SetItemString(dict, _int_constants[i].constant_name, tmp_obj);
453 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455
Neal Norwitzefee9f52007-10-27 02:50:52 +0000456 if (!(tmp_obj = PyUnicode_FromString(PYSQLITE_VERSION))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457 goto error;
458 }
459 PyDict_SetItemString(dict, "version", tmp_obj);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000460 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461
Neal Norwitzefee9f52007-10-27 02:50:52 +0000462 if (!(tmp_obj = PyUnicode_FromString(sqlite3_libversion()))) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 goto error;
464 }
465 PyDict_SetItemString(dict, "sqlite_version", tmp_obj);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000466 Py_DECREF(tmp_obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000467
468 /* initialize microprotocols layer */
Benjamin Petersond7b03282008-09-13 15:58:53 +0000469 pysqlite_microprotocols_init(dict);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470
471 /* initialize the default converters */
472 converters_init(dict);
473
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474error:
475 if (PyErr_Occurred())
476 {
477 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 Py_DECREF(module);
479 module = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000481 return module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000482}