blob: 4d9d3d41c7b71b9ab539bd3a8bf76d65455a9a0e [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
Steve Dower60419a72019-06-24 08:42:54 -070088 if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
89 return NULL;
90 }
91
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 result = PyObject_Call(factory, args, kwargs);
93
94 return result;
95}
96
Benjamin Petersondcf97b92008-07-02 17:30:14 +000097PyDoc_STRVAR(module_connect_doc,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010098"connect(database[, timeout, detect_types, isolation_level,\n\
99 check_same_thread, factory, cached_statements, uri])\n\
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000100\n\
101Opens a connection to the SQLite database file *database*. You can use\n\
102\":memory:\" to open a database connection to a database that resides in\n\
103RAM instead of on disk.");
104
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105static PyObject* module_complete(PyObject* self, PyObject* args, PyObject*
106 kwargs)
107{
Alex Henrie188bb5b2020-01-29 21:12:53 -0700108 static char *kwlist[] = {"statement", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109 char* statement;
110
111 PyObject* result;
112
113 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement))
114 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 }
117
118 if (sqlite3_complete(statement)) {
119 result = Py_True;
120 } else {
121 result = Py_False;
122 }
123
124 Py_INCREF(result);
125
126 return result;
127}
128
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000129PyDoc_STRVAR(module_complete_doc,
130"complete_statement(sql)\n\
131\n\
132Checks if a string contains a complete SQL statement. Non-standard.");
133
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134#ifdef HAVE_SHARED_CACHE
135static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject*
136 kwargs)
137{
Alex Henrie188bb5b2020-01-29 21:12:53 -0700138 static char *kwlist[] = {"do_enable", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 int do_enable;
140 int rc;
141
142 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable))
143 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 }
146
147 rc = sqlite3_enable_shared_cache(do_enable);
148
149 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000150 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151 return NULL;
152 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +0300153 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154 }
155}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000156
157PyDoc_STRVAR(module_enable_shared_cache_doc,
158"enable_shared_cache(do_enable)\n\
159\n\
160Enable or disable shared cache mode for the calling thread.\n\
161Experimental/Non-standard.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000162#endif /* HAVE_SHARED_CACHE */
163
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000164static PyObject* module_register_adapter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165{
166 PyTypeObject* type;
167 PyObject* caster;
Georg Brandl3dbca812008-07-23 16:10:53 +0000168 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169
170 if (!PyArg_ParseTuple(args, "OO", &type, &caster)) {
171 return NULL;
172 }
173
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000174 /* a basic type is adapted; there's a performance optimization if that's not the case
175 * (99 % of all usages) */
176 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000177 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000178 pysqlite_BaseTypeAdapted = 1;
179 }
180
Benjamin Petersond7b03282008-09-13 15:58:53 +0000181 rc = pysqlite_microprotocols_add(type, (PyObject*)&pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000182 if (rc == -1)
183 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184
Berker Peksagfe21de92016-04-09 07:34:39 +0300185 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000186}
187
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000188PyDoc_STRVAR(module_register_adapter_doc,
189"register_adapter(type, callable)\n\
190\n\
191Registers an adapter with pysqlite's adapter registry. Non-standard.");
192
193static PyObject* module_register_converter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000195 PyObject* orig_name;
196 PyObject* name = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197 PyObject* callable;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000198 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200199 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200
Guido van Rossum98297ee2007-11-06 21:34:58 +0000201 if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202 return NULL;
203 }
204
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000205 /* convert the name to upper case */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200206 name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000207 if (!name) {
208 goto error;
209 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700211 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000212 goto error;
213 }
214
215 Py_INCREF(Py_None);
216 retval = Py_None;
217error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000218 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000219 return retval;
220}
221
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000222PyDoc_STRVAR(module_register_converter_doc,
223"register_converter(typename, callable)\n\
224\n\
225Registers a converter with pysqlite. Non-standard.");
226
227static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000228{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700229 if (!PyArg_ParseTuple(args, "i", &_pysqlite_enable_callback_tracebacks)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 return NULL;
231 }
232
Berker Peksagfe21de92016-04-09 07:34:39 +0300233 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000234}
235
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000236PyDoc_STRVAR(enable_callback_tracebacks_doc,
237"enable_callback_tracebacks(flag)\n\
238\n\
239Enable or disable callback functions throwing errors to stderr.");
240
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000241static void converters_init(PyObject* dict)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700243 _pysqlite_converters = PyDict_New();
244 if (!_pysqlite_converters) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245 return;
246 }
247
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700248 PyDict_SetItemString(dict, "converters", _pysqlite_converters);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000249}
250
251static PyMethodDef module_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200252 {"connect", (PyCFunction)(void(*)(void))module_connect,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000253 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200254 {"complete_statement", (PyCFunction)(void(*)(void))module_complete,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000255 METH_VARARGS | METH_KEYWORDS, module_complete_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256#ifdef HAVE_SHARED_CACHE
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200257 {"enable_shared_cache", (PyCFunction)(void(*)(void))module_enable_shared_cache,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000258 METH_VARARGS | METH_KEYWORDS, module_enable_shared_cache_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259#endif
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000260 {"register_adapter", (PyCFunction)module_register_adapter,
261 METH_VARARGS, module_register_adapter_doc},
262 {"register_converter", (PyCFunction)module_register_converter,
263 METH_VARARGS, module_register_converter_doc},
Benjamin Petersond7b03282008-09-13 15:58:53 +0000264 {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS,
265 pysqlite_adapt_doc},
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000266 {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks,
267 METH_VARARGS, enable_callback_tracebacks_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000268 {NULL, NULL}
269};
270
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271struct _IntConstantPair {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200272 const char *constant_name;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273 int constant_value;
274};
275
276typedef struct _IntConstantPair IntConstantPair;
277
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200278static const IntConstantPair _int_constants[] = {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279 {"PARSE_DECLTYPES", PARSE_DECLTYPES},
280 {"PARSE_COLNAMES", PARSE_COLNAMES},
281
282 {"SQLITE_OK", SQLITE_OK},
283 {"SQLITE_DENY", SQLITE_DENY},
284 {"SQLITE_IGNORE", SQLITE_IGNORE},
285 {"SQLITE_CREATE_INDEX", SQLITE_CREATE_INDEX},
286 {"SQLITE_CREATE_TABLE", SQLITE_CREATE_TABLE},
287 {"SQLITE_CREATE_TEMP_INDEX", SQLITE_CREATE_TEMP_INDEX},
288 {"SQLITE_CREATE_TEMP_TABLE", SQLITE_CREATE_TEMP_TABLE},
289 {"SQLITE_CREATE_TEMP_TRIGGER", SQLITE_CREATE_TEMP_TRIGGER},
290 {"SQLITE_CREATE_TEMP_VIEW", SQLITE_CREATE_TEMP_VIEW},
291 {"SQLITE_CREATE_TRIGGER", SQLITE_CREATE_TRIGGER},
292 {"SQLITE_CREATE_VIEW", SQLITE_CREATE_VIEW},
293 {"SQLITE_DELETE", SQLITE_DELETE},
294 {"SQLITE_DROP_INDEX", SQLITE_DROP_INDEX},
295 {"SQLITE_DROP_TABLE", SQLITE_DROP_TABLE},
296 {"SQLITE_DROP_TEMP_INDEX", SQLITE_DROP_TEMP_INDEX},
297 {"SQLITE_DROP_TEMP_TABLE", SQLITE_DROP_TEMP_TABLE},
298 {"SQLITE_DROP_TEMP_TRIGGER", SQLITE_DROP_TEMP_TRIGGER},
299 {"SQLITE_DROP_TEMP_VIEW", SQLITE_DROP_TEMP_VIEW},
300 {"SQLITE_DROP_TRIGGER", SQLITE_DROP_TRIGGER},
301 {"SQLITE_DROP_VIEW", SQLITE_DROP_VIEW},
302 {"SQLITE_INSERT", SQLITE_INSERT},
303 {"SQLITE_PRAGMA", SQLITE_PRAGMA},
304 {"SQLITE_READ", SQLITE_READ},
305 {"SQLITE_SELECT", SQLITE_SELECT},
306 {"SQLITE_TRANSACTION", SQLITE_TRANSACTION},
307 {"SQLITE_UPDATE", SQLITE_UPDATE},
308 {"SQLITE_ATTACH", SQLITE_ATTACH},
309 {"SQLITE_DETACH", SQLITE_DETACH},
310#if SQLITE_VERSION_NUMBER >= 3002001
311 {"SQLITE_ALTER_TABLE", SQLITE_ALTER_TABLE},
312 {"SQLITE_REINDEX", SQLITE_REINDEX},
313#endif
314#if SQLITE_VERSION_NUMBER >= 3003000
315 {"SQLITE_ANALYZE", SQLITE_ANALYZE},
316#endif
Berker Peksag00b1ae02017-01-02 06:38:10 +0300317#if SQLITE_VERSION_NUMBER >= 3003007
318 {"SQLITE_CREATE_VTABLE", SQLITE_CREATE_VTABLE},
319 {"SQLITE_DROP_VTABLE", SQLITE_DROP_VTABLE},
320#endif
321#if SQLITE_VERSION_NUMBER >= 3003008
322 {"SQLITE_FUNCTION", SQLITE_FUNCTION},
323#endif
324#if SQLITE_VERSION_NUMBER >= 3006008
325 {"SQLITE_SAVEPOINT", SQLITE_SAVEPOINT},
326#endif
327#if SQLITE_VERSION_NUMBER >= 3008003
328 {"SQLITE_RECURSIVE", SQLITE_RECURSIVE},
329#endif
Emanuele Gaifasd7aed412018-03-10 23:08:31 +0100330#if SQLITE_VERSION_NUMBER >= 3006011
331 {"SQLITE_DONE", SQLITE_DONE},
332#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000333 {(char*)NULL, 0}
334};
335
Martin v. Löwis1a214512008-06-11 05:26:20 +0000336
337static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 PyModuleDef_HEAD_INIT,
339 "_sqlite3",
340 NULL,
341 -1,
342 module_methods,
343 NULL,
344 NULL,
345 NULL,
346 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000347};
348
349PyMODINIT_FUNC PyInit__sqlite3(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000350{
351 PyObject *module, *dict;
352 PyObject *tmp_obj;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000353 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000354
Martin v. Löwis1a214512008-06-11 05:26:20 +0000355 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356
357 if (!module ||
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000358 (pysqlite_row_setup_types() < 0) ||
359 (pysqlite_cursor_setup_types() < 0) ||
360 (pysqlite_connection_setup_types() < 0) ||
361 (pysqlite_cache_setup_types() < 0) ||
362 (pysqlite_statement_setup_types() < 0) ||
363 (pysqlite_prepare_protocol_setup_types() < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 ) {
Brett Cannone1445072011-02-04 20:24:02 +0000365 Py_XDECREF(module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000366 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 }
368
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000369 Py_INCREF(&pysqlite_ConnectionType);
370 PyModule_AddObject(module, "Connection", (PyObject*) &pysqlite_ConnectionType);
371 Py_INCREF(&pysqlite_CursorType);
372 PyModule_AddObject(module, "Cursor", (PyObject*) &pysqlite_CursorType);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000373 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}