blob: 9fdf51417ed8830c3a1d1ba1dd88744cb67acfeb [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
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 PyObject* result;
124
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125 if (sqlite3_complete(statement)) {
126 result = Py_True;
127 } else {
128 result = Py_False;
129 }
130
131 Py_INCREF(result);
132
133 return result;
134}
135
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100136/*[clinic input]
137_sqlite3.enable_shared_cache as pysqlite_enable_shared_cache
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000138
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100139 do_enable: int
140
141Enable or disable shared cache mode for the calling thread.
142
143Experimental/Non-standard.
144[clinic start generated code]*/
145
146static PyObject *
147pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable)
148/*[clinic end generated code: output=259c74eedee1516b input=8400e41bc58b6b24]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 int rc;
151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 rc = sqlite3_enable_shared_cache(do_enable);
153
154 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000155 PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156 return NULL;
157 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +0300158 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 }
160}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000161
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100162/*[clinic input]
163_sqlite3.register_adapter as pysqlite_register_adapter
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000164
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100165 type: object(type='PyTypeObject *')
166 caster: object
167 /
168
169Registers an adapter with pysqlite's adapter registry. Non-standard.
170[clinic start generated code]*/
171
172static PyObject *
173pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
174 PyObject *caster)
175/*[clinic end generated code: output=a287e8db18e8af23 input=839dad90e2492725]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000176{
Georg Brandl3dbca812008-07-23 16:10:53 +0000177 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000179 /* a basic type is adapted; there's a performance optimization if that's not the case
180 * (99 % of all usages) */
181 if (type == &PyLong_Type || type == &PyFloat_Type
Christian Heimes9c4756e2008-05-26 13:22:05 +0000182 || type == &PyUnicode_Type || type == &PyByteArray_Type) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000183 pysqlite_BaseTypeAdapted = 1;
184 }
185
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200186 rc = pysqlite_microprotocols_add(type, (PyObject*)pysqlite_PrepareProtocolType, caster);
Georg Brandl3dbca812008-07-23 16:10:53 +0000187 if (rc == -1)
188 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189
Berker Peksagfe21de92016-04-09 07:34:39 +0300190 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191}
192
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100193/*[clinic input]
194_sqlite3.register_converter as pysqlite_register_converter
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000195
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100196 name as orig_name: unicode
197 converter as callable: object
198 /
199
200Registers a converter with pysqlite. Non-standard.
201[clinic start generated code]*/
202
203static PyObject *
204pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
205 PyObject *callable)
206/*[clinic end generated code: output=a2f2bfeed7230062 input=e074cf7f4890544f]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000208 PyObject* name = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000209 PyObject* retval = NULL;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200210 _Py_IDENTIFIER(upper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000212 /* convert the name to upper case */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200213 name = _PyObject_CallMethodIdNoArgs(orig_name, &PyId_upper);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000214 if (!name) {
215 goto error;
216 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000217
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700218 if (PyDict_SetItem(_pysqlite_converters, name, callable) != 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000219 goto error;
220 }
221
222 Py_INCREF(Py_None);
223 retval = Py_None;
224error:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000225 Py_XDECREF(name);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226 return retval;
227}
228
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100229/*[clinic input]
230_sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000231
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100232 enable: int
233 /
234
235Enable or disable callback functions throwing errors to stderr.
236[clinic start generated code]*/
237
238static PyObject *
239pysqlite_enable_callback_trace_impl(PyObject *module, int enable)
240/*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000241{
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100242 _pysqlite_enable_callback_tracebacks = enable;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243
Berker Peksagfe21de92016-04-09 07:34:39 +0300244 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245}
246
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100247/*[clinic input]
248_sqlite3.adapt as pysqlite_adapt
249
250 obj: object
251 proto: object(c_default='(PyObject*)pysqlite_PrepareProtocolType') = PrepareProtocolType
252 alt: object = NULL
253 /
254
255Adapt given object to given protocol. Non-standard.
256[clinic start generated code]*/
257
258static PyObject *
259pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
260 PyObject *alt)
261/*[clinic end generated code: output=0c3927c5fcd23dd9 input=a58ab77fb5ae22dd]*/
262{
263 return pysqlite_microprotocols_adapt(obj, proto, alt);
264}
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000265
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100266static int converters_init(PyObject* module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267{
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700268 _pysqlite_converters = PyDict_New();
269 if (!_pysqlite_converters) {
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100270 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271 }
272
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100273 int res = PyModule_AddObjectRef(module, "converters", _pysqlite_converters);
274 Py_DECREF(_pysqlite_converters);
275
276 return res;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000277}
278
279static PyMethodDef module_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200280 {"connect", (PyCFunction)(void(*)(void))module_connect,
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000281 METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Erlend Egeberg Aasland7d210272020-10-31 07:07:44 +0100282 PYSQLITE_ADAPT_METHODDEF
283 PYSQLITE_COMPLETE_STATEMENT_METHODDEF
284 PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF
285 PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF
286 PYSQLITE_REGISTER_ADAPTER_METHODDEF
287 PYSQLITE_REGISTER_CONVERTER_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000288 {NULL, NULL}
289};
290
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200291static int add_integer_constants(PyObject *module) {
292 int ret = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000293
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200294 ret += PyModule_AddIntMacro(module, PARSE_DECLTYPES);
295 ret += PyModule_AddIntMacro(module, PARSE_COLNAMES);
296 ret += PyModule_AddIntMacro(module, SQLITE_OK);
297 ret += PyModule_AddIntMacro(module, SQLITE_DENY);
298 ret += PyModule_AddIntMacro(module, SQLITE_IGNORE);
299 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_INDEX);
300 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TABLE);
301 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_INDEX);
302 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TABLE);
303 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_TRIGGER);
304 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TEMP_VIEW);
305 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_TRIGGER);
306 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VIEW);
307 ret += PyModule_AddIntMacro(module, SQLITE_DELETE);
308 ret += PyModule_AddIntMacro(module, SQLITE_DROP_INDEX);
309 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TABLE);
310 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_INDEX);
311 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TABLE);
312 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_TRIGGER);
313 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TEMP_VIEW);
314 ret += PyModule_AddIntMacro(module, SQLITE_DROP_TRIGGER);
315 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VIEW);
316 ret += PyModule_AddIntMacro(module, SQLITE_INSERT);
317 ret += PyModule_AddIntMacro(module, SQLITE_PRAGMA);
318 ret += PyModule_AddIntMacro(module, SQLITE_READ);
319 ret += PyModule_AddIntMacro(module, SQLITE_SELECT);
320 ret += PyModule_AddIntMacro(module, SQLITE_TRANSACTION);
321 ret += PyModule_AddIntMacro(module, SQLITE_UPDATE);
322 ret += PyModule_AddIntMacro(module, SQLITE_ATTACH);
323 ret += PyModule_AddIntMacro(module, SQLITE_DETACH);
324 ret += PyModule_AddIntMacro(module, SQLITE_ALTER_TABLE);
325 ret += PyModule_AddIntMacro(module, SQLITE_REINDEX);
326 ret += PyModule_AddIntMacro(module, SQLITE_ANALYZE);
327 ret += PyModule_AddIntMacro(module, SQLITE_CREATE_VTABLE);
328 ret += PyModule_AddIntMacro(module, SQLITE_DROP_VTABLE);
329 ret += PyModule_AddIntMacro(module, SQLITE_FUNCTION);
330 ret += PyModule_AddIntMacro(module, SQLITE_SAVEPOINT);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300331#if SQLITE_VERSION_NUMBER >= 3008003
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200332 ret += PyModule_AddIntMacro(module, SQLITE_RECURSIVE);
Berker Peksag00b1ae02017-01-02 06:38:10 +0300333#endif
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200334 ret += PyModule_AddIntMacro(module, SQLITE_DONE);
335 return ret;
336}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000337
338static struct PyModuleDef _sqlite3module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 PyModuleDef_HEAD_INIT,
340 "_sqlite3",
341 NULL,
342 -1,
343 module_methods,
344 NULL,
345 NULL,
346 NULL,
347 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000348};
349
Erlend Egeberg Aasland5eb45d72020-05-26 14:18:19 +0200350#define ADD_TYPE(module, type) \
351do { \
352 if (PyModule_AddType(module, &type) < 0) { \
353 Py_DECREF(module); \
354 return NULL; \
355 } \
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 Aasland207c3212020-09-07 23:26:54 +0200375 if (sqlite3_libversion_number() < 3007003) {
376 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.3 or higher required");
377 return NULL;
378 }
379
Martin v. Löwis1a214512008-06-11 05:26:20 +0000380 module = PyModule_Create(&_sqlite3module);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381
382 if (!module ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200383 (pysqlite_row_setup_types(module) < 0) ||
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200384 (pysqlite_cursor_setup_types(module) < 0) ||
385 (pysqlite_connection_setup_types(module) < 0) ||
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200386 (pysqlite_cache_setup_types(module) < 0) ||
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200387 (pysqlite_statement_setup_types(module) < 0) ||
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200388 (pysqlite_prepare_protocol_setup_types(module) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000389 ) {
Brett Cannone1445072011-02-04 20:24:02 +0000390 Py_XDECREF(module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000391 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 }
393
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200394 ADD_TYPE(module, *pysqlite_ConnectionType);
395 ADD_TYPE(module, *pysqlite_CursorType);
Erlend Egeberg Aaslandcb6db8b2020-09-29 00:05:04 +0200396 ADD_TYPE(module, *pysqlite_PrepareProtocolType);
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200397 ADD_TYPE(module, *pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399 /*** Create DB-API Exception hierarchy */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200400 ADD_EXCEPTION(module, "Error", pysqlite_Error, PyExc_Exception);
401 ADD_EXCEPTION(module, "Warning", pysqlite_Warning, PyExc_Exception);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402
403 /* Error subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200404 ADD_EXCEPTION(module, "InterfaceError", pysqlite_InterfaceError, pysqlite_Error);
405 ADD_EXCEPTION(module, "DatabaseError", pysqlite_DatabaseError, pysqlite_Error);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000407 /* pysqlite_DatabaseError subclasses */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200408 ADD_EXCEPTION(module, "InternalError", pysqlite_InternalError, pysqlite_DatabaseError);
409 ADD_EXCEPTION(module, "OperationalError", pysqlite_OperationalError, pysqlite_DatabaseError);
410 ADD_EXCEPTION(module, "ProgrammingError", pysqlite_ProgrammingError, pysqlite_DatabaseError);
411 ADD_EXCEPTION(module, "IntegrityError", pysqlite_IntegrityError, pysqlite_DatabaseError);
412 ADD_EXCEPTION(module, "DataError", pysqlite_DataError, pysqlite_DatabaseError);
413 ADD_EXCEPTION(module, "NotSupportedError", pysqlite_NotSupportedError, pysqlite_DatabaseError);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414
Petri Lehtinenbc35beb2012-02-09 21:09:03 +0200415 /* In Python 2.x, setting Connection.text_factory to
416 OptimizedUnicode caused Unicode objects to be returned for
417 non-ASCII data and bytestrings to be returned for ASCII data.
418 Now OptimizedUnicode is an alias for str, so it has no
419 effect. */
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100420 if (PyModule_AddObjectRef(module, "OptimizedUnicode", (PyObject*)&PyUnicode_Type) < 0) {
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200421 goto error;
422 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000424 /* Set integer constants */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200425 if (add_integer_constants(module) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426 goto error;
427 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000428
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200429 if (PyModule_AddStringConstant(module, "version", PYSQLITE_VERSION) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 goto error;
431 }
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200432
433 if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) {
434 goto error;
435 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000436
437 /* initialize microprotocols layer */
Erlend Egeberg Aasland644e9422020-10-15 14:20:15 +0200438 if (pysqlite_microprotocols_init(module) < 0) {
439 goto error;
440 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441
442 /* initialize the default converters */
Erlend Egeberg Aasland789359f2020-11-04 20:31:51 +0100443 if (converters_init(module) < 0) {
444 goto error;
445 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000447error:
448 if (PyErr_Occurred())
449 {
450 PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_DECREF(module);
452 module = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000454 return module;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455}