blob: 6bfb1b73f823911c7abfe0d055d017e3794c4f6c [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);
99
100 return result;
101}
102
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000103PyDoc_STRVAR(module_connect_doc,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100104"connect(database[, timeout, detect_types, isolation_level,\n\
105 check_same_thread, factory, cached_statements, uri])\n\
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000106\n\
107Opens a connection to the SQLite database file *database*. You can use\n\
108\":memory:\" to open a database connection to a database that resides in\n\
109RAM instead of on disk.");
110
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100111/*[clinic input]
112_sqlite3.complete_statement as pysqlite_complete_statement
113
114 statement: str
115
116Checks if a string contains a complete SQL statement. Non-standard.
117[clinic start generated code]*/
118
119static PyObject *
120pysqlite_complete_statement_impl(PyObject *module, const char *statement)
121/*[clinic end generated code: output=e55f1ff1952df558 input=f6b24996b31c5c33]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000122{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 if (sqlite3_complete(statement)) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100124 return Py_NewRef(Py_True);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125 } else {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100126 return Py_NewRef(Py_False);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128}
129
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100130/*[clinic input]
131_sqlite3.enable_shared_cache as pysqlite_enable_shared_cache
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000132
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100133 do_enable: int
134
135Enable or disable shared cache mode for the calling thread.
136
137Experimental/Non-standard.
138[clinic start generated code]*/
139
140static PyObject *
141pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable)
142/*[clinic end generated code: output=259c74eedee1516b input=8400e41bc58b6b24]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000144 int rc;
145
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146 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
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100156/*[clinic input]
157_sqlite3.register_adapter as pysqlite_register_adapter
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100159 type: object(type='PyTypeObject *')
160 caster: object
161 /
162
163Registers an adapter with pysqlite's adapter registry. Non-standard.
164[clinic start generated code]*/
165
166static PyObject *
167pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
168 PyObject *caster)
169/*[clinic end generated code: output=a287e8db18e8af23 input=839dad90e2492725]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000170{
Georg Brandl3dbca812008-07-23 16:10:53 +0000171 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000173 /* a basic type is adapted; there's a performance optimization if that's not the case
174 * (99 % of all usages) */
175 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000176 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000177 pysqlite_BaseTypeAdapted = 1;
178 }
179
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200180 rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000181 if (rc == -1)
182 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183
Berker Peksagfe21de92016-04-09 07:34:39 +0300184 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185}
186
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100187/*[clinic input]
188_sqlite3.register_converter as pysqlite_register_converter
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000189
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100190 name as orig_name: unicode
191 converter as callable: object
192 /
193
194Registers a converter with pysqlite. Non-standard.
195[clinic start generated code]*/
196
197static PyObject *
198pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
199 PyObject *callable)
200/*[clinic end generated code: output=a2f2bfeed7230062 input=e074cf7f4890544f]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000202 PyObject* name = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200204 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000206 /* convert the name to upper case */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200207 name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208 if (!name) {
209 goto error;
210 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700212 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000213 goto error;
214 }
215
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100216 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000217error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000218 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000219 return retval;
220}
221
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100222/*[clinic input]
223_sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000224
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100225 enable: int
226 /
227
228Enable or disable callback functions throwing errors to stderr.
229[clinic start generated code]*/
230
231static PyObject *
232pysqlite_enable_callback_trace_impl(PyObject *module, int enable)
233/*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000234{
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100235 _pysqlite_enable_callback_tracebacks = enable;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000236
Berker Peksagfe21de92016-04-09 07:34:39 +0300237 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238}
239
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100240/*[clinic input]
241_sqlite3.adapt as pysqlite_adapt
242
243 obj: object
244 proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType
245 alt: object = NULL
246 /
247
248Adapt given object to given protocol. Non-standard.
249[clinic start generated code]*/
250
251static PyObject *
252pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
253 PyObject *alt)
254/*[clinic end generated code: output=0c3927c5fcd23dd9 input=a58ab77fb5ae22dd]*/
255{
256 return pysqlite_microprotocols_adapt(obj, proto, alt);
257}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000258
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100259static int converters_init(PyObject* module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700261 _pysqlite_converters = PyDict_New();
262 if (!_pysqlite_converters) {
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100263 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 }
265
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100266 int res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters);
267 Py_DECREF(_pysqlite_converters);
268
269 return res;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000270}
271
272static PyMethodDef module_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200273 {"connect", (PyCFunction)(void(*)(void))module_connect,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000274 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100275 PYSQLITE_ADAPT_METHODDEF
276 PYSQLITE_COMPLETE_STATEMENT_METHODDEF
277 PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF
278 PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
279 PYSQLITE_REGISTER_ADAPTER_METHODDEF
280 PYSQLITE_REGISTER_CONVERTER_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281 {NULL, NULL}
282};
283
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200284static int add_integer_constants(PyObject *module) {
285 int ret = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200287 ret += PyModule_AddIntMacro(module, PARSE_DECLTYPES);
288 ret += PyModule_AddIntMacro(module, PARSE_COLNAMES);
289 ret += PyModule_AddIntMacro(module, SQLITE_OK);
290 ret += PyModule_AddIntMacro(module, SQLITE_DENY);
291 ret += PyModule_AddIntMacro(module, SQLITE_IGNORE);
292 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_INDEX);
293 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TABLE);
294 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_INDEX);
295 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TABLE);
296 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TRIGGER);
297 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_VIEW);
298 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TRIGGER);
299 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VIEW);
300 ret += PyModule_AddIntMacro(module, SQLITE_DELETE);
301 ret += PyModule_AddIntMacro(module, SQLITE_DROP_INDEX);
302 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TABLE);
303 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_INDEX);
304 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TABLE);
305 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TRIGGER);
306 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_VIEW);
307 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TRIGGER);
308 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VIEW);
309 ret += PyModule_AddIntMacro(module, SQLITE_INSERT);
310 ret += PyModule_AddIntMacro(module, SQLITE_PRAGMA);
311 ret += PyModule_AddIntMacro(module, SQLITE_READ);
312 ret += PyModule_AddIntMacro(module, SQLITE_SELECT);
313 ret += PyModule_AddIntMacro(module, SQLITE_TRANSACTION);
314 ret += PyModule_AddIntMacro(module, SQLITE_UPDATE);
315 ret += PyModule_AddIntMacro(module, SQLITE_ATTACH);
316 ret += PyModule_AddIntMacro(module, SQLITE_DETACH);
317 ret += PyModule_AddIntMacro(module, SQLITE_ALTER_TABLE);
318 ret += PyModule_AddIntMacro(module, SQLITE_REINDEX);
319 ret += PyModule_AddIntMacro(module, SQLITE_ANALYZE);
320 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VTABLE);
321 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VTABLE);
322 ret += PyModule_AddIntMacro(module, SQLITE_FUNCTION);
323 ret += PyModule_AddIntMacro(module, SQLITE_SAVEPOINT);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300324#if SQLITE_VERSION_NUMBER >= 3008003
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200325 ret += PyModule_AddIntMacro(module, SQLITE_RECURSIVE);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300326#endif
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200327 ret += PyModule_AddIntMacro(module, SQLITE_DONE);
328 return ret;
329}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000330
331static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyModuleDef_HEAD_INIT,
333 "_sqlite3",
334 NULL,
335 -1,
336 module_methods,
337 NULL,
338 NULL,
339 NULL,
340 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000341};
342
Erlend Egeberg Aasland5eb45d72020-05-26 14:18:19 +0200343#define ADD_TYPE(module, type) \
344do { \
345 if (PyModule_AddType(module, &type) < 0) { \
346 Py_DECREF(module); \
347 return NULL; \
348 } \
349} while (0)
350
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200351#define ADD_EXCEPTION(module, name, exc, base) \
352do { \
353 exc = PyErr_NewException(MODULE_NAME "." name, base, NULL); \
354 if (!exc) { \
355 goto error; \
356 } \
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100357 int res = PyModule_AddObjectRef(module, name, exc); \
358 Py_DECREF(exc); \
359 if (res < 0) { \
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200360 goto error; \
361 } \
362} while (0)
363
Martin v. Löwis1a214512008-06-11 05:26:20 +0000364PyMODINIT_FUNC PyInit__sqlite3(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000365{
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200366 PyObject *module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100368 if (sqlite3_libversion_number() < 3007015) {
369 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required");
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200370 return NULL;
371 }
372
Martin v. Löwis1a214512008-06-11 05:26:20 +0000373 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000374
375 if (!module ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200376 (pysqlite_row_setup_types(module) < 0) ||
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200377 (pysqlite_cursor_setup_types(module) < 0) ||
378 (pysqlite_connection_setup_types(module) < 0) ||
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200379 (pysqlite_cache_setup_types(module) < 0) ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200380 (pysqlite_statement_setup_types(module) < 0) ||
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200381 (pysqlite_prepare_protocol_setup_types(module) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 ) {
Brett Cannone1445072011-02-04 20:24:02 +0000383 Py_XDECREF(module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000384 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 }
386
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200387 ADD_TYPE(module, *pysqlite_ConnectionType);
388 ADD_TYPE(module, *pysqlite_CursorType);
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200389 ADD_TYPE(module, *pysqlite_PrepareProtocolType);
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200390 ADD_TYPE(module, *pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 /*** Create DB-API Exception hierarchy */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200393 ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception);
394 ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395
396 /* Error subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200397 ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error);
398 ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000400 /* pysqlite_DatabaseError subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200401 ADD_EXCEPTION(module, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError);
402 ADD_EXCEPTION(module, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError);
403 ADD_EXCEPTION(module, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError);
404 ADD_EXCEPTION(module, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError);
405 ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError);
406 ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000408 /* Set integer constants */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200409 if (add_integer_constants(module) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410 goto error;
411 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200413 if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414 goto error;
415 }
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200416
417 if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) {
418 goto error;
419 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420
421 /* initialize microprotocols layer */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200422 if (pysqlite_microprotocols_init(module) < 0) {
423 goto error;
424 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425
426 /* initialize the default converters */
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100427 if (converters_init(module) < 0) {
428 goto error;
429 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431error:
432 if (PyErr_Occurred())
433 {
434 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 Py_DECREF(module);
436 module = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000438 return module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439}