blob: 2f323fcd00141ffec41a1431a29f1f40163939be [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 Aaslandcf0b2392021-01-06 01:02:43 +010032#if SQLITE_VERSION_NUMBER < 3007015
33#error "SQLite 3.7.15 or higher required"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034#endif
35
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +010036#include "clinic/module.c.h"
37/*[clinic input]
38module _sqlite3
39[clinic start generated code]*/
40/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81e330492d57488e]*/
41
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042/* static objects at module-level */
43
Benjamin Peterson0a37a302017-12-31 10:04:13 -080044PyObject *pysqlite_Error = NULL;
45PyObject *pysqlite_Warning = NULL;
46PyObject *pysqlite_InterfaceError = NULL;
47PyObject *pysqlite_DatabaseError = NULL;
48PyObject *pysqlite_InternalError = NULL;
49PyObject *pysqlite_OperationalError = NULL;
50PyObject *pysqlite_ProgrammingError = NULL;
51PyObject *pysqlite_IntegrityError = NULL;
52PyObject *pysqlite_DataError = NULL;
53PyObject *pysqlite_NotSupportedError = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054
Benjamin Peterson7762e4d2018-07-09 21:20:23 -070055PyObject* _pysqlite_converters = NULL;
56int _pysqlite_enable_callback_tracebacks = 0;
Benjamin Peterson0a37a302017-12-31 10:04:13 -080057int pysqlite_BaseTypeAdapted = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058
59static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
60 kwargs)
61{
62 /* Python seems to have no way of extracting a single keyword-arg at
63 * C-level, so this code is redundant with the one in connection_init in
64 * connection.c and must always be copied from there ... */
65
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010066 static char *kwlist[] = {
67 "database", "timeout", "detect_types", "isolation_level",
68 "check_same_thread", "factory", "cached_statements", "uri",
69 NULL
70 };
Anders Lorentsena22a1272017-11-07 01:47:43 +010071 PyObject* database;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072 int detect_types = 0;
73 PyObject* isolation_level;
74 PyObject* factory = NULL;
75 int check_same_thread = 1;
76 int cached_statements;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010077 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 double timeout = 5.0;
79
80 PyObject* result;
81
Anders Lorentsena22a1272017-11-07 01:47:43 +010082 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010083 &database, &timeout, &detect_types,
84 &isolation_level, &check_same_thread,
85 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 }
89
90 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +020091 factory = (PyObject*)pysqlite_ConnectionType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 }
93
Steve Dower60419a72019-06-24 08:42:54 -070094 if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
95 return NULL;
96 }
97
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 result = PyObject_Call(factory, args, kwargs);
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +020099 if (result == NULL) {
100 return NULL;
101 }
102
103 if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
104 Py_DECREF(result);
105 return NULL;
106 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107
108 return result;
109}
110
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000111PyDoc_STRVAR(module_connect_doc,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100112"connect(database[, timeout, detect_types, isolation_level,\n\
113 check_same_thread, factory, cached_statements, uri])\n\
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000114\n\
115Opens a connection to the SQLite database file *database*. You can use\n\
116\":memory:\" to open a database connection to a database that resides in\n\
117RAM instead of on disk.");
118
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100119/*[clinic input]
120_sqlite3.complete_statement as pysqlite_complete_statement
121
122 statement: str
123
124Checks if a string contains a complete SQL statement. Non-standard.
125[clinic start generated code]*/
126
127static PyObject *
128pysqlite_complete_statement_impl(PyObject *module, const char *statement)
129/*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 if (sqlite3_complete(statement)) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100132 return Py_NewRef(Py_True);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 } else {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100134 return Py_NewRef(Py_False);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136}
137
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100138/*[clinic input]
139_sqlite3.enable_shared_cache as pysqlite_enable_shared_cache
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000140
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100141 do_enable: int
142
143Enable or disable shared cache mode for the calling thread.
144
145Experimental/Non-standard.
146[clinic start generated code]*/
147
148static PyObject *
149pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable)
150/*[clinic end generated code: output=259c74eedee1516b input=8400e41bc58b6b24]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 int rc;
153
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154 rc = sqlite3_enable_shared_cache(do_enable);
155
156 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158 return NULL;
159 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +0300160 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161 }
162}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000163
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100164/*[clinic input]
165_sqlite3.register_adapter as pysqlite_register_adapter
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100167 type: object(type='PyTypeObject *')
168 caster: object
169 /
170
171Registers an adapter with pysqlite's adapter registry. Non-standard.
172[clinic start generated code]*/
173
174static PyObject *
175pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
176 PyObject *caster)
177/*[clinic end generated code: output=a287e8db18e8af23 input=839dad90e2492725]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178{
Georg Brandl3dbca812008-07-23 16:10:53 +0000179 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000181 /* a basic type is adapted; there's a performance optimization if that's not the case
182 * (99 % of all usages) */
183 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000184 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000185 pysqlite_BaseTypeAdapted = 1;
186 }
187
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200188 rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000189 if (rc == -1)
190 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191
Berker Peksagfe21de92016-04-09 07:34:39 +0300192 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193}
194
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100195/*[clinic input]
196_sqlite3.register_converter as pysqlite_register_converter
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000197
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100198 name as orig_name: unicode
199 converter as callable: object
200 /
201
202Registers a converter with pysqlite. Non-standard.
203[clinic start generated code]*/
204
205static PyObject *
206pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
207 PyObject *callable)
208/*[clinic end generated code: output=a2f2bfeed7230062 input=e074cf7f4890544f]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000210 PyObject* name = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200212 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000214 /* convert the name to upper case */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200215 name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000216 if (!name) {
217 goto error;
218 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000219
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700220 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221 goto error;
222 }
223
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100224 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000225error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000226 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000227 return retval;
228}
229
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100230/*[clinic input]
231_sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000232
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100233 enable: int
234 /
235
236Enable or disable callback functions throwing errors to stderr.
237[clinic start generated code]*/
238
239static PyObject *
240pysqlite_enable_callback_trace_impl(PyObject *module, int enable)
241/*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000242{
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100243 _pysqlite_enable_callback_tracebacks = enable;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244
Berker Peksagfe21de92016-04-09 07:34:39 +0300245 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246}
247
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100248/*[clinic input]
249_sqlite3.adapt as pysqlite_adapt
250
251 obj: object
252 proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType
253 alt: object = NULL
254 /
255
256Adapt given object to given protocol. Non-standard.
257[clinic start generated code]*/
258
259static PyObject *
260pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
261 PyObject *alt)
262/*[clinic end generated code: output=0c3927c5fcd23dd9 input=a58ab77fb5ae22dd]*/
263{
264 return pysqlite_microprotocols_adapt(obj, proto, alt);
265}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000266
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100267static int converters_init(PyObject* module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000268{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700269 _pysqlite_converters = PyDict_New();
270 if (!_pysqlite_converters) {
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100271 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000272 }
273
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100274 int res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters);
275 Py_DECREF(_pysqlite_converters);
276
277 return res;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278}
279
280static PyMethodDef module_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200281 {"connect", (PyCFunction)(void(*)(void))module_connect,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000282 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100283 PYSQLITE_ADAPT_METHODDEF
284 PYSQLITE_COMPLETE_STATEMENT_METHODDEF
285 PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF
286 PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
287 PYSQLITE_REGISTER_ADAPTER_METHODDEF
288 PYSQLITE_REGISTER_CONVERTER_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289 {NULL, NULL}
290};
291
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200292static int add_integer_constants(PyObject *module) {
293 int ret = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200295 ret += PyModule_AddIntMacro(module, PARSE_DECLTYPES);
296 ret += PyModule_AddIntMacro(module, PARSE_COLNAMES);
297 ret += PyModule_AddIntMacro(module, SQLITE_OK);
298 ret += PyModule_AddIntMacro(module, SQLITE_DENY);
299 ret += PyModule_AddIntMacro(module, SQLITE_IGNORE);
300 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_INDEX);
301 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TABLE);
302 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_INDEX);
303 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TABLE);
304 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TRIGGER);
305 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_VIEW);
306 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TRIGGER);
307 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VIEW);
308 ret += PyModule_AddIntMacro(module, SQLITE_DELETE);
309 ret += PyModule_AddIntMacro(module, SQLITE_DROP_INDEX);
310 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TABLE);
311 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_INDEX);
312 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TABLE);
313 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TRIGGER);
314 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_VIEW);
315 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TRIGGER);
316 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VIEW);
317 ret += PyModule_AddIntMacro(module, SQLITE_INSERT);
318 ret += PyModule_AddIntMacro(module, SQLITE_PRAGMA);
319 ret += PyModule_AddIntMacro(module, SQLITE_READ);
320 ret += PyModule_AddIntMacro(module, SQLITE_SELECT);
321 ret += PyModule_AddIntMacro(module, SQLITE_TRANSACTION);
322 ret += PyModule_AddIntMacro(module, SQLITE_UPDATE);
323 ret += PyModule_AddIntMacro(module, SQLITE_ATTACH);
324 ret += PyModule_AddIntMacro(module, SQLITE_DETACH);
325 ret += PyModule_AddIntMacro(module, SQLITE_ALTER_TABLE);
326 ret += PyModule_AddIntMacro(module, SQLITE_REINDEX);
327 ret += PyModule_AddIntMacro(module, SQLITE_ANALYZE);
328 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VTABLE);
329 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VTABLE);
330 ret += PyModule_AddIntMacro(module, SQLITE_FUNCTION);
331 ret += PyModule_AddIntMacro(module, SQLITE_SAVEPOINT);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300332#if SQLITE_VERSION_NUMBER >= 3008003
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200333 ret += PyModule_AddIntMacro(module, SQLITE_RECURSIVE);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300334#endif
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200335 ret += PyModule_AddIntMacro(module, SQLITE_DONE);
336 return ret;
337}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000338
339static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 PyModuleDef_HEAD_INIT,
341 "_sqlite3",
342 NULL,
343 -1,
344 module_methods,
345 NULL,
346 NULL,
347 NULL,
348 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000349};
350
Erlend Egeberg Aasland5eb45d72020-05-26 14:18:19 +0200351#define ADD_TYPE(module, type) \
352do { \
353 if (PyModule_AddType(module, &type) < 0) { \
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200354 goto error; \
Erlend Egeberg Aasland5eb45d72020-05-26 14:18:19 +0200355 } \
356} while (0)
357
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200358#define ADD_EXCEPTION(module, name, exc, base) \
359do { \
360 exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \
361 if (!exc) { \
362 goto error; \
363 } \
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100364 int res = PyModule_AddObjectRef(module, name, exc); \
365 Py_DECREF(exc); \
366 if (res < 0) { \
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200367 goto error; \
368 } \
369} while (0)
370
Martin v. Löwis1a214512008-06-11 05:26:20 +0000371PyMODINIT_FUNC PyInit__sqlite3(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372{
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200373 PyObject *module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100375 if (sqlite3_libversion_number() < 3007015) {
376 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required");
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200377 return NULL;
378 }
379
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200380 int rc = sqlite3_initialize();
381 if (rc != SQLITE_OK) {
382 PyErr_SetString(PyExc_ImportError, sqlite3_errstr(rc));
383 return NULL;
384 }
385
Martin v. Löwis1a214512008-06-11 05:26:20 +0000386 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000387
388 if (!module ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200389 (pysqlite_row_setup_types(module) < 0) ||
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200390 (pysqlite_cursor_setup_types(module) < 0) ||
391 (pysqlite_connection_setup_types(module) < 0) ||
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200392 (pysqlite_cache_setup_types(module) < 0) ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200393 (pysqlite_statement_setup_types(module) < 0) ||
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200394 (pysqlite_prepare_protocol_setup_types(module) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395 ) {
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200396 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000397 }
398
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200399 ADD_TYPE(module, *pysqlite_ConnectionType);
400 ADD_TYPE(module, *pysqlite_CursorType);
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200401 ADD_TYPE(module, *pysqlite_PrepareProtocolType);
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200402 ADD_TYPE(module, *pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000403
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 /*** Create DB-API Exception hierarchy */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200405 ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception);
406 ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407
408 /* Error subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200409 ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error);
410 ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000412 /* pysqlite_DatabaseError subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200413 ADD_EXCEPTION(module, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError);
414 ADD_EXCEPTION(module, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError);
415 ADD_EXCEPTION(module, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError);
416 ADD_EXCEPTION(module, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError);
417 ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError);
418 ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000420 /* Set integer constants */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200421 if (add_integer_constants(module) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422 goto error;
423 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200425 if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 goto error;
427 }
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200428
429 if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) {
430 goto error;
431 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432
433 /* initialize microprotocols layer */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200434 if (pysqlite_microprotocols_init(module) < 0) {
435 goto error;
436 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437
438 /* initialize the default converters */
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100439 if (converters_init(module) < 0) {
440 goto error;
441 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000442
Martin v. Löwis1a214512008-06-11 05:26:20 +0000443 return module;
Erlend Egeberg Aaslanddef91932021-04-14 16:50:16 +0200444
445error:
446 sqlite3_shutdown();
447 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
448 Py_XDECREF(module);
449 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000450}