blob: 0297e2fab292e5779de597898914a57f6883d51d [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
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +020032#if SQLITE_VERSION_NUMBER < 3007003
33#error "SQLite 3.7.3 or higher required"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#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) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +020085 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 +0000134static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyObject*
135 kwargs)
136{
Alex Henrie188bb5b2020-01-29 21:12:53 -0700137 static char *kwlist[] = {"do_enable", NULL};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 int do_enable;
139 int rc;
140
141 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable))
142 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 }
145
146 rc = sqlite3_enable_shared_cache(do_enable);
147
148 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000149 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 return NULL;
151 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +0300152 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000153 }
154}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000155
156PyDoc_STRVAR(module_enable_shared_cache_doc,
157"enable_shared_cache(do_enable)\n\
158\n\
159Enable or disable shared cache mode for the calling thread.\n\
160Experimental/Non-standard.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000162static PyObject* module_register_adapter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163{
164 PyTypeObject* type;
165 PyObject* caster;
Georg Brandl3dbca812008-07-23 16:10:53 +0000166 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167
168 if (!PyArg_ParseTuple(args, "OO", &type, &caster)) {
169 return NULL;
170 }
171
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000172 /* a basic type is adapted; there's a performance optimization if that's not the case
173 * (99 % of all usages) */
174 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000175 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000176 pysqlite_BaseTypeAdapted = 1;
177 }
178
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200179 rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000180 if (rc == -1)
181 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182
Berker Peksagfe21de92016-04-09 07:34:39 +0300183 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184}
185
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000186PyDoc_STRVAR(module_register_adapter_doc,
187"register_adapter(type, callable)\n\
188\n\
189Registers an adapter with pysqlite's adapter registry. Non-standard.");
190
191static PyObject* module_register_converter(PyObject* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000193 PyObject* orig_name;
194 PyObject* name = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195 PyObject* callable;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000196 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200197 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198
Guido van Rossum98297ee2007-11-06 21:34:58 +0000199 if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200 return NULL;
201 }
202
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000203 /* convert the name to upper case */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200204 name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000205 if (!name) {
206 goto error;
207 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700209 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210 goto error;
211 }
212
213 Py_INCREF(Py_None);
214 retval = Py_None;
215error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000216 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000217 return retval;
218}
219
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000220PyDoc_STRVAR(module_register_converter_doc,
221"register_converter(typename, callable)\n\
222\n\
223Registers a converter with pysqlite. Non-standard.");
224
225static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700227 if (!PyArg_ParseTuple(args, "i", &_pysqlite_enable_callback_tracebacks)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000228 return NULL;
229 }
230
Berker Peksagfe21de92016-04-09 07:34:39 +0300231 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232}
233
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000234PyDoc_STRVAR(enable_callback_tracebacks_doc,
235"enable_callback_tracebacks(flag)\n\
236\n\
237Enable or disable callback functions throwing errors to stderr.");
238
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200239static void converters_init(PyObject* module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700241 _pysqlite_converters = PyDict_New();
242 if (!_pysqlite_converters) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243 return;
244 }
245
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200246 if (PyModule_AddObject(module, "converters", _pysqlite_converters) < 0) {
247 Py_DECREF(_pysqlite_converters);
248 }
249 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250}
251
252static PyMethodDef module_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200253 {"connect", (PyCFunction)(void(*)(void))module_connect,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000254 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200255 {"complete_statement", (PyCFunction)(void(*)(void))module_complete,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000256 METH_VARARGS | METH_KEYWORDS, module_complete_doc},
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},
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000259 {"register_adapter", (PyCFunction)module_register_adapter,
260 METH_VARARGS, module_register_adapter_doc},
261 {"register_converter", (PyCFunction)module_register_converter,
262 METH_VARARGS, module_register_converter_doc},
Benjamin Petersond7b03282008-09-13 15:58:53 +0000263 {"adapt", (PyCFunction)pysqlite_adapt, METH_VARARGS,
264 pysqlite_adapt_doc},
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000265 {"enable_callback_tracebacks", (PyCFunction)enable_callback_tracebacks,
266 METH_VARARGS, enable_callback_tracebacks_doc},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267 {NULL, NULL}
268};
269
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200270static int add_integer_constants(PyObject *module) {
271 int ret = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000272
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200273 ret += PyModule_AddIntMacro(module, PARSE_DECLTYPES);
274 ret += PyModule_AddIntMacro(module, PARSE_COLNAMES);
275 ret += PyModule_AddIntMacro(module, SQLITE_OK);
276 ret += PyModule_AddIntMacro(module, SQLITE_DENY);
277 ret += PyModule_AddIntMacro(module, SQLITE_IGNORE);
278 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_INDEX);
279 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TABLE);
280 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_INDEX);
281 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TABLE);
282 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TRIGGER);
283 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_VIEW);
284 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TRIGGER);
285 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VIEW);
286 ret += PyModule_AddIntMacro(module, SQLITE_DELETE);
287 ret += PyModule_AddIntMacro(module, SQLITE_DROP_INDEX);
288 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TABLE);
289 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_INDEX);
290 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TABLE);
291 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TRIGGER);
292 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_VIEW);
293 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TRIGGER);
294 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VIEW);
295 ret += PyModule_AddIntMacro(module, SQLITE_INSERT);
296 ret += PyModule_AddIntMacro(module, SQLITE_PRAGMA);
297 ret += PyModule_AddIntMacro(module, SQLITE_READ);
298 ret += PyModule_AddIntMacro(module, SQLITE_SELECT);
299 ret += PyModule_AddIntMacro(module, SQLITE_TRANSACTION);
300 ret += PyModule_AddIntMacro(module, SQLITE_UPDATE);
301 ret += PyModule_AddIntMacro(module, SQLITE_ATTACH);
302 ret += PyModule_AddIntMacro(module, SQLITE_DETACH);
303 ret += PyModule_AddIntMacro(module, SQLITE_ALTER_TABLE);
304 ret += PyModule_AddIntMacro(module, SQLITE_REINDEX);
305 ret += PyModule_AddIntMacro(module, SQLITE_ANALYZE);
306 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VTABLE);
307 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VTABLE);
308 ret += PyModule_AddIntMacro(module, SQLITE_FUNCTION);
309 ret += PyModule_AddIntMacro(module, SQLITE_SAVEPOINT);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300310#if SQLITE_VERSION_NUMBER >= 3008003
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200311 ret += PyModule_AddIntMacro(module, SQLITE_RECURSIVE);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300312#endif
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200313 ret += PyModule_AddIntMacro(module, SQLITE_DONE);
314 return ret;
315}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000316
317static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyModuleDef_HEAD_INIT,
319 "_sqlite3",
320 NULL,
321 -1,
322 module_methods,
323 NULL,
324 NULL,
325 NULL,
326 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000327};
328
Erlend Egeberg Aasland5eb45d72020-05-26 14:18:19 +0200329#define ADD_TYPE(module, type) \
330do { \
331 if (PyModule_AddType(module, &type) < 0) { \
332 Py_DECREF(module); \
333 return NULL; \
334 } \
335} while (0)
336
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200337#define ADD_EXCEPTION(module, name, exc, base) \
338do { \
339 exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \
340 if (!exc) { \
341 goto error; \
342 } \
343 if (PyModule_AddObject(module, name, exc) < 0) { \
344 Py_DECREF(exc); \
345 goto error; \
346 } \
347} while (0)
348
Martin v. Löwis1a214512008-06-11 05:26:20 +0000349PyMODINIT_FUNC PyInit__sqlite3(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000350{
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200351 PyObject *module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200353 if (sqlite3_libversion_number() < 3007003) {
354 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.3 or higher required");
355 return NULL;
356 }
357
Martin v. Löwis1a214512008-06-11 05:26:20 +0000358 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359
360 if (!module ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200361 (pysqlite_row_setup_types(module) < 0) ||
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200362 (pysqlite_cursor_setup_types(module) < 0) ||
363 (pysqlite_connection_setup_types(module) < 0) ||
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200364 (pysqlite_cache_setup_types(module) < 0) ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200365 (pysqlite_statement_setup_types(module) < 0) ||
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200366 (pysqlite_prepare_protocol_setup_types(module) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 ) {
Brett Cannone1445072011-02-04 20:24:02 +0000368 Py_XDECREF(module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000369 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 }
371
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200372 ADD_TYPE(module, *pysqlite_ConnectionType);
373 ADD_TYPE(module, *pysqlite_CursorType);
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200374 ADD_TYPE(module, *pysqlite_PrepareProtocolType);
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200375 ADD_TYPE(module, *pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000376
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377 /*** Create DB-API Exception hierarchy */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200378 ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception);
379 ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380
381 /* Error subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200382 ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error);
383 ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000385 /* pysqlite_DatabaseError subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200386 ADD_EXCEPTION(module, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError);
387 ADD_EXCEPTION(module, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError);
388 ADD_EXCEPTION(module, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError);
389 ADD_EXCEPTION(module, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError);
390 ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError);
391 ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200393 /* In Python 2.x, setting Connection.text_factory to
394 OptimizedUnicode caused Unicode objects to be returned for
395 non-ASCII data and bytestrings to be returned for ASCII data.
396 Now OptimizedUnicode is an alias for str, so it has no
397 effect. */
398 Py_INCREF((PyObject*)&PyUnicode_Type);
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200399 if (PyModule_AddObject(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type) < 0) {
400 Py_DECREF((PyObject*)&PyUnicode_Type);
401 goto error;
402 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000403
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000404 /* Set integer constants */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200405 if (add_integer_constants(module) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 goto error;
407 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200409 if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410 goto error;
411 }
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200412
413 if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) {
414 goto error;
415 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416
417 /* initialize microprotocols layer */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200418 if (pysqlite_microprotocols_init(module) < 0) {
419 goto error;
420 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000421
422 /* initialize the default converters */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200423 converters_init(module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425error:
426 if (PyErr_Occurred())
427 {
428 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 Py_DECREF(module);
430 module = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000432 return module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433}