module.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* module.c - the module itself
  2. *
  3. * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
  4. *
  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. */
  23. #include "connection.h"
  24. #include "statement.h"
  25. #include "cursor.h"
  26. #include "prepare_protocol.h"
  27. #include "microprotocols.h"
  28. #include "row.h"
  29. #include "blob.h"
  30. #if SQLITE_VERSION_NUMBER < 3007015
  31. #error "SQLite 3.7.15 or higher required"
  32. #endif
  33. #define clinic_state() (pysqlite_get_state(module))
  34. #include "clinic/module.c.h"
  35. #undef clinic_state
  36. /*[clinic input]
  37. module _sqlite3
  38. [clinic start generated code]*/
  39. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=81e330492d57488e]*/
  40. // NB: This needs to be in sync with the Connection.__init__ docstring.
  41. PyDoc_STRVAR(module_connect_doc,
  42. "connect($module, /, database, timeout=5.0, detect_types=0,\n"
  43. " isolation_level='', check_same_thread=True,\n"
  44. " factory=ConnectionType, cached_statements=128, uri=False, *,\n"
  45. " autocommit=sqlite3.LEGACY_TRANSACTION_CONTROL)\n"
  46. "--\n"
  47. "\n"
  48. "Opens a connection to the SQLite database file database.\n"
  49. "\n"
  50. "You can use \":memory:\" to open a database connection to a database that resides\n"
  51. "in RAM instead of on disk.");
  52. #define PYSQLITE_CONNECT_METHODDEF \
  53. {"connect", _PyCFunction_CAST(module_connect), METH_FASTCALL|METH_KEYWORDS, module_connect_doc},
  54. static PyObject *
  55. module_connect(PyObject *module, PyObject *const *args, Py_ssize_t nargsf,
  56. PyObject *kwnames)
  57. {
  58. pysqlite_state *state = pysqlite_get_state(module);
  59. PyObject *factory = (PyObject *)state->ConnectionType;
  60. static const int FACTORY_POS = 5;
  61. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  62. if (nargs > FACTORY_POS) {
  63. factory = args[FACTORY_POS];
  64. }
  65. else if (kwnames != NULL) {
  66. for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
  67. PyObject *item = PyTuple_GET_ITEM(kwnames, i); // borrowed ref.
  68. if (PyUnicode_CompareWithASCIIString(item, "factory") == 0) {
  69. factory = args[nargs + i];
  70. break;
  71. }
  72. }
  73. }
  74. return PyObject_Vectorcall(factory, args, nargsf, kwnames);
  75. }
  76. /*[clinic input]
  77. _sqlite3.complete_statement as pysqlite_complete_statement
  78. statement: str
  79. Checks if a string contains a complete SQL statement.
  80. [clinic start generated code]*/
  81. static PyObject *
  82. pysqlite_complete_statement_impl(PyObject *module, const char *statement)
  83. /*[clinic end generated code: output=e55f1ff1952df558 input=ac45d257375bb828]*/
  84. {
  85. if (sqlite3_complete(statement)) {
  86. return Py_NewRef(Py_True);
  87. } else {
  88. return Py_NewRef(Py_False);
  89. }
  90. }
  91. /*[clinic input]
  92. _sqlite3.register_adapter as pysqlite_register_adapter
  93. type: object(type='PyTypeObject *')
  94. adapter as caster: object
  95. /
  96. Register a function to adapt Python objects to SQLite values.
  97. [clinic start generated code]*/
  98. static PyObject *
  99. pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
  100. PyObject *caster)
  101. /*[clinic end generated code: output=a287e8db18e8af23 input=29a5e0f213030242]*/
  102. {
  103. int rc;
  104. /* a basic type is adapted; there's a performance optimization if that's not the case
  105. * (99 % of all usages) */
  106. if (type == &PyLong_Type || type == &PyFloat_Type
  107. || type == &PyUnicode_Type || type == &PyByteArray_Type) {
  108. pysqlite_state *state = pysqlite_get_state(module);
  109. state->BaseTypeAdapted = 1;
  110. }
  111. pysqlite_state *state = pysqlite_get_state(module);
  112. PyObject *protocol = (PyObject *)state->PrepareProtocolType;
  113. rc = pysqlite_microprotocols_add(state, type, protocol, caster);
  114. if (rc == -1) {
  115. return NULL;
  116. }
  117. Py_RETURN_NONE;
  118. }
  119. /*[clinic input]
  120. _sqlite3.register_converter as pysqlite_register_converter
  121. typename as orig_name: unicode
  122. converter as callable: object
  123. /
  124. Register a function to convert SQLite values to Python objects.
  125. [clinic start generated code]*/
  126. static PyObject *
  127. pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
  128. PyObject *callable)
  129. /*[clinic end generated code: output=a2f2bfeed7230062 input=159a444971b40378]*/
  130. {
  131. PyObject* name = NULL;
  132. PyObject* retval = NULL;
  133. /* convert the name to upper case */
  134. pysqlite_state *state = pysqlite_get_state(module);
  135. name = PyObject_CallMethodNoArgs(orig_name, state->str_upper);
  136. if (!name) {
  137. goto error;
  138. }
  139. if (PyDict_SetItem(state->converters, name, callable) != 0) {
  140. goto error;
  141. }
  142. retval = Py_NewRef(Py_None);
  143. error:
  144. Py_XDECREF(name);
  145. return retval;
  146. }
  147. /*[clinic input]
  148. _sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace
  149. enable: int
  150. /
  151. Enable or disable callback functions throwing errors to stderr.
  152. [clinic start generated code]*/
  153. static PyObject *
  154. pysqlite_enable_callback_trace_impl(PyObject *module, int enable)
  155. /*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/
  156. {
  157. pysqlite_state *state = pysqlite_get_state(module);
  158. state->enable_callback_tracebacks = enable;
  159. Py_RETURN_NONE;
  160. }
  161. /*[clinic input]
  162. _sqlite3.adapt as pysqlite_adapt
  163. obj: object
  164. proto: object(c_default='(PyObject *)clinic_state()->PrepareProtocolType') = PrepareProtocolType
  165. alt: object = NULL
  166. /
  167. Adapt given object to given protocol.
  168. [clinic start generated code]*/
  169. static PyObject *
  170. pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
  171. PyObject *alt)
  172. /*[clinic end generated code: output=0c3927c5fcd23dd9 input=a53dc9993e81e15f]*/
  173. {
  174. pysqlite_state *state = pysqlite_get_state(module);
  175. return pysqlite_microprotocols_adapt(state, obj, proto, alt);
  176. }
  177. static int converters_init(PyObject* module)
  178. {
  179. pysqlite_state *state = pysqlite_get_state(module);
  180. state->converters = PyDict_New();
  181. if (state->converters == NULL) {
  182. return -1;
  183. }
  184. return PyModule_AddObjectRef(module, "converters", state->converters);
  185. }
  186. static int
  187. load_functools_lru_cache(PyObject *module)
  188. {
  189. pysqlite_state *state = pysqlite_get_state(module);
  190. state->lru_cache = _PyImport_GetModuleAttrString("functools", "lru_cache");
  191. if (state->lru_cache == NULL) {
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. static PyMethodDef module_methods[] = {
  197. PYSQLITE_ADAPT_METHODDEF
  198. PYSQLITE_COMPLETE_STATEMENT_METHODDEF
  199. PYSQLITE_CONNECT_METHODDEF
  200. PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF
  201. PYSQLITE_REGISTER_ADAPTER_METHODDEF
  202. PYSQLITE_REGISTER_CONVERTER_METHODDEF
  203. {NULL, NULL}
  204. };
  205. /* SQLite C API result codes. See also:
  206. * - https://www.sqlite.org/c3ref/c_abort_rollback.html
  207. * - https://sqlite.org/changes.html#version_3_3_8
  208. * - https://sqlite.org/changes.html#version_3_7_16
  209. * - https://sqlite.org/changes.html#version_3_7_17
  210. * - https://sqlite.org/changes.html#version_3_8_0
  211. * - https://sqlite.org/changes.html#version_3_8_3
  212. * - https://sqlite.org/changes.html#version_3_14
  213. *
  214. * Note: the SQLite changelogs rarely mention new result codes, so in order to
  215. * keep the 'error_codes' table in sync with SQLite, we must manually inspect
  216. * sqlite3.h for every release.
  217. *
  218. * We keep the SQLITE_VERSION_NUMBER checks in order to easily declutter the
  219. * code when we adjust the SQLite version requirement.
  220. */
  221. static const struct {
  222. const char *name;
  223. long value;
  224. } error_codes[] = {
  225. #define DECLARE_ERROR_CODE(code) {#code, code}
  226. // Primary result code list
  227. DECLARE_ERROR_CODE(SQLITE_ABORT),
  228. DECLARE_ERROR_CODE(SQLITE_AUTH),
  229. DECLARE_ERROR_CODE(SQLITE_BUSY),
  230. DECLARE_ERROR_CODE(SQLITE_CANTOPEN),
  231. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT),
  232. DECLARE_ERROR_CODE(SQLITE_CORRUPT),
  233. DECLARE_ERROR_CODE(SQLITE_DONE),
  234. DECLARE_ERROR_CODE(SQLITE_EMPTY),
  235. DECLARE_ERROR_CODE(SQLITE_ERROR),
  236. DECLARE_ERROR_CODE(SQLITE_FORMAT),
  237. DECLARE_ERROR_CODE(SQLITE_FULL),
  238. DECLARE_ERROR_CODE(SQLITE_INTERNAL),
  239. DECLARE_ERROR_CODE(SQLITE_INTERRUPT),
  240. DECLARE_ERROR_CODE(SQLITE_IOERR),
  241. DECLARE_ERROR_CODE(SQLITE_LOCKED),
  242. DECLARE_ERROR_CODE(SQLITE_MISMATCH),
  243. DECLARE_ERROR_CODE(SQLITE_MISUSE),
  244. DECLARE_ERROR_CODE(SQLITE_NOLFS),
  245. DECLARE_ERROR_CODE(SQLITE_NOMEM),
  246. DECLARE_ERROR_CODE(SQLITE_NOTADB),
  247. DECLARE_ERROR_CODE(SQLITE_NOTFOUND),
  248. DECLARE_ERROR_CODE(SQLITE_OK),
  249. DECLARE_ERROR_CODE(SQLITE_PERM),
  250. DECLARE_ERROR_CODE(SQLITE_PROTOCOL),
  251. DECLARE_ERROR_CODE(SQLITE_RANGE),
  252. DECLARE_ERROR_CODE(SQLITE_READONLY),
  253. DECLARE_ERROR_CODE(SQLITE_ROW),
  254. DECLARE_ERROR_CODE(SQLITE_SCHEMA),
  255. DECLARE_ERROR_CODE(SQLITE_TOOBIG),
  256. #if SQLITE_VERSION_NUMBER >= 3007017
  257. DECLARE_ERROR_CODE(SQLITE_NOTICE),
  258. DECLARE_ERROR_CODE(SQLITE_WARNING),
  259. #endif
  260. // Extended result code list
  261. DECLARE_ERROR_CODE(SQLITE_ABORT_ROLLBACK),
  262. DECLARE_ERROR_CODE(SQLITE_BUSY_RECOVERY),
  263. DECLARE_ERROR_CODE(SQLITE_CANTOPEN_FULLPATH),
  264. DECLARE_ERROR_CODE(SQLITE_CANTOPEN_ISDIR),
  265. DECLARE_ERROR_CODE(SQLITE_CANTOPEN_NOTEMPDIR),
  266. DECLARE_ERROR_CODE(SQLITE_CORRUPT_VTAB),
  267. DECLARE_ERROR_CODE(SQLITE_IOERR_ACCESS),
  268. DECLARE_ERROR_CODE(SQLITE_IOERR_BLOCKED),
  269. DECLARE_ERROR_CODE(SQLITE_IOERR_CHECKRESERVEDLOCK),
  270. DECLARE_ERROR_CODE(SQLITE_IOERR_CLOSE),
  271. DECLARE_ERROR_CODE(SQLITE_IOERR_DELETE),
  272. DECLARE_ERROR_CODE(SQLITE_IOERR_DELETE_NOENT),
  273. DECLARE_ERROR_CODE(SQLITE_IOERR_DIR_CLOSE),
  274. DECLARE_ERROR_CODE(SQLITE_IOERR_DIR_FSYNC),
  275. DECLARE_ERROR_CODE(SQLITE_IOERR_FSTAT),
  276. DECLARE_ERROR_CODE(SQLITE_IOERR_FSYNC),
  277. DECLARE_ERROR_CODE(SQLITE_IOERR_LOCK),
  278. DECLARE_ERROR_CODE(SQLITE_IOERR_NOMEM),
  279. DECLARE_ERROR_CODE(SQLITE_IOERR_RDLOCK),
  280. DECLARE_ERROR_CODE(SQLITE_IOERR_READ),
  281. DECLARE_ERROR_CODE(SQLITE_IOERR_SEEK),
  282. DECLARE_ERROR_CODE(SQLITE_IOERR_SHMLOCK),
  283. DECLARE_ERROR_CODE(SQLITE_IOERR_SHMMAP),
  284. DECLARE_ERROR_CODE(SQLITE_IOERR_SHMOPEN),
  285. DECLARE_ERROR_CODE(SQLITE_IOERR_SHMSIZE),
  286. DECLARE_ERROR_CODE(SQLITE_IOERR_SHORT_READ),
  287. DECLARE_ERROR_CODE(SQLITE_IOERR_TRUNCATE),
  288. DECLARE_ERROR_CODE(SQLITE_IOERR_UNLOCK),
  289. DECLARE_ERROR_CODE(SQLITE_IOERR_WRITE),
  290. DECLARE_ERROR_CODE(SQLITE_LOCKED_SHAREDCACHE),
  291. DECLARE_ERROR_CODE(SQLITE_READONLY_CANTLOCK),
  292. DECLARE_ERROR_CODE(SQLITE_READONLY_RECOVERY),
  293. #if SQLITE_VERSION_NUMBER >= 3007016
  294. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_CHECK),
  295. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_COMMITHOOK),
  296. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_FOREIGNKEY),
  297. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_FUNCTION),
  298. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_NOTNULL),
  299. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_PRIMARYKEY),
  300. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_TRIGGER),
  301. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_UNIQUE),
  302. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_VTAB),
  303. DECLARE_ERROR_CODE(SQLITE_READONLY_ROLLBACK),
  304. #endif
  305. #if SQLITE_VERSION_NUMBER >= 3007017
  306. DECLARE_ERROR_CODE(SQLITE_IOERR_MMAP),
  307. DECLARE_ERROR_CODE(SQLITE_NOTICE_RECOVER_ROLLBACK),
  308. DECLARE_ERROR_CODE(SQLITE_NOTICE_RECOVER_WAL),
  309. #endif
  310. #if SQLITE_VERSION_NUMBER >= 3008000
  311. DECLARE_ERROR_CODE(SQLITE_BUSY_SNAPSHOT),
  312. DECLARE_ERROR_CODE(SQLITE_IOERR_GETTEMPPATH),
  313. DECLARE_ERROR_CODE(SQLITE_WARNING_AUTOINDEX),
  314. #endif
  315. #if SQLITE_VERSION_NUMBER >= 3008001
  316. DECLARE_ERROR_CODE(SQLITE_CANTOPEN_CONVPATH),
  317. DECLARE_ERROR_CODE(SQLITE_IOERR_CONVPATH),
  318. #endif
  319. #if SQLITE_VERSION_NUMBER >= 3008002
  320. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_ROWID),
  321. #endif
  322. #if SQLITE_VERSION_NUMBER >= 3008003
  323. DECLARE_ERROR_CODE(SQLITE_READONLY_DBMOVED),
  324. #endif
  325. #if SQLITE_VERSION_NUMBER >= 3008007
  326. DECLARE_ERROR_CODE(SQLITE_AUTH_USER),
  327. #endif
  328. #if SQLITE_VERSION_NUMBER >= 3009000
  329. DECLARE_ERROR_CODE(SQLITE_IOERR_VNODE),
  330. #endif
  331. #if SQLITE_VERSION_NUMBER >= 3010000
  332. DECLARE_ERROR_CODE(SQLITE_IOERR_AUTH),
  333. #endif
  334. #if SQLITE_VERSION_NUMBER >= 3014001
  335. DECLARE_ERROR_CODE(SQLITE_OK_LOAD_PERMANENTLY),
  336. #endif
  337. #if SQLITE_VERSION_NUMBER >= 3021000
  338. DECLARE_ERROR_CODE(SQLITE_IOERR_BEGIN_ATOMIC),
  339. DECLARE_ERROR_CODE(SQLITE_IOERR_COMMIT_ATOMIC),
  340. DECLARE_ERROR_CODE(SQLITE_IOERR_ROLLBACK_ATOMIC),
  341. #endif
  342. #if SQLITE_VERSION_NUMBER >= 3022000
  343. DECLARE_ERROR_CODE(SQLITE_ERROR_MISSING_COLLSEQ),
  344. DECLARE_ERROR_CODE(SQLITE_ERROR_RETRY),
  345. DECLARE_ERROR_CODE(SQLITE_READONLY_CANTINIT),
  346. DECLARE_ERROR_CODE(SQLITE_READONLY_DIRECTORY),
  347. #endif
  348. #if SQLITE_VERSION_NUMBER >= 3024000
  349. DECLARE_ERROR_CODE(SQLITE_CORRUPT_SEQUENCE),
  350. DECLARE_ERROR_CODE(SQLITE_LOCKED_VTAB),
  351. #endif
  352. #if SQLITE_VERSION_NUMBER >= 3025000
  353. DECLARE_ERROR_CODE(SQLITE_CANTOPEN_DIRTYWAL),
  354. DECLARE_ERROR_CODE(SQLITE_ERROR_SNAPSHOT),
  355. #endif
  356. #if SQLITE_VERSION_NUMBER >= 3031000
  357. DECLARE_ERROR_CODE(SQLITE_CANTOPEN_SYMLINK),
  358. DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_PINNED),
  359. DECLARE_ERROR_CODE(SQLITE_OK_SYMLINK),
  360. #endif
  361. #if SQLITE_VERSION_NUMBER >= 3032000
  362. DECLARE_ERROR_CODE(SQLITE_BUSY_TIMEOUT),
  363. DECLARE_ERROR_CODE(SQLITE_CORRUPT_INDEX),
  364. DECLARE_ERROR_CODE(SQLITE_IOERR_DATA),
  365. #endif
  366. #if SQLITE_VERSION_NUMBER >= 3034000
  367. DECLARE_ERROR_CODE(SQLITE_IOERR_CORRUPTFS),
  368. #endif
  369. #undef DECLARE_ERROR_CODE
  370. {NULL, 0},
  371. };
  372. static int
  373. add_error_constants(PyObject *module)
  374. {
  375. for (int i = 0; error_codes[i].name != NULL; i++) {
  376. const char *name = error_codes[i].name;
  377. const long value = error_codes[i].value;
  378. if (PyModule_AddIntConstant(module, name, value) < 0) {
  379. return -1;
  380. }
  381. }
  382. return 0;
  383. }
  384. const char *
  385. pysqlite_error_name(int rc)
  386. {
  387. for (int i = 0; error_codes[i].name != NULL; i++) {
  388. if (error_codes[i].value == rc) {
  389. return error_codes[i].name;
  390. }
  391. }
  392. // No error code matched.
  393. return NULL;
  394. }
  395. static int
  396. add_integer_constants(PyObject *module) {
  397. #define ADD_INT(ival) \
  398. do { \
  399. if (PyModule_AddIntConstant(module, #ival, ival) < 0) { \
  400. return -1; \
  401. } \
  402. } while (0); \
  403. ADD_INT(PARSE_DECLTYPES);
  404. ADD_INT(PARSE_COLNAMES);
  405. ADD_INT(SQLITE_DENY);
  406. ADD_INT(SQLITE_IGNORE);
  407. ADD_INT(SQLITE_CREATE_INDEX);
  408. ADD_INT(SQLITE_CREATE_TABLE);
  409. ADD_INT(SQLITE_CREATE_TEMP_INDEX);
  410. ADD_INT(SQLITE_CREATE_TEMP_TABLE);
  411. ADD_INT(SQLITE_CREATE_TEMP_TRIGGER);
  412. ADD_INT(SQLITE_CREATE_TEMP_VIEW);
  413. ADD_INT(SQLITE_CREATE_TRIGGER);
  414. ADD_INT(SQLITE_CREATE_VIEW);
  415. ADD_INT(SQLITE_DELETE);
  416. ADD_INT(SQLITE_DROP_INDEX);
  417. ADD_INT(SQLITE_DROP_TABLE);
  418. ADD_INT(SQLITE_DROP_TEMP_INDEX);
  419. ADD_INT(SQLITE_DROP_TEMP_TABLE);
  420. ADD_INT(SQLITE_DROP_TEMP_TRIGGER);
  421. ADD_INT(SQLITE_DROP_TEMP_VIEW);
  422. ADD_INT(SQLITE_DROP_TRIGGER);
  423. ADD_INT(SQLITE_DROP_VIEW);
  424. ADD_INT(SQLITE_INSERT);
  425. ADD_INT(SQLITE_PRAGMA);
  426. ADD_INT(SQLITE_READ);
  427. ADD_INT(SQLITE_SELECT);
  428. ADD_INT(SQLITE_TRANSACTION);
  429. ADD_INT(SQLITE_UPDATE);
  430. ADD_INT(SQLITE_ATTACH);
  431. ADD_INT(SQLITE_DETACH);
  432. ADD_INT(SQLITE_ALTER_TABLE);
  433. ADD_INT(SQLITE_REINDEX);
  434. ADD_INT(SQLITE_ANALYZE);
  435. ADD_INT(SQLITE_CREATE_VTABLE);
  436. ADD_INT(SQLITE_DROP_VTABLE);
  437. ADD_INT(SQLITE_FUNCTION);
  438. ADD_INT(SQLITE_SAVEPOINT);
  439. #if SQLITE_VERSION_NUMBER >= 3008003
  440. ADD_INT(SQLITE_RECURSIVE);
  441. #endif
  442. // Run-time limit categories
  443. ADD_INT(SQLITE_LIMIT_LENGTH);
  444. ADD_INT(SQLITE_LIMIT_SQL_LENGTH);
  445. ADD_INT(SQLITE_LIMIT_COLUMN);
  446. ADD_INT(SQLITE_LIMIT_EXPR_DEPTH);
  447. ADD_INT(SQLITE_LIMIT_COMPOUND_SELECT);
  448. ADD_INT(SQLITE_LIMIT_VDBE_OP);
  449. ADD_INT(SQLITE_LIMIT_FUNCTION_ARG);
  450. ADD_INT(SQLITE_LIMIT_ATTACHED);
  451. ADD_INT(SQLITE_LIMIT_LIKE_PATTERN_LENGTH);
  452. ADD_INT(SQLITE_LIMIT_VARIABLE_NUMBER);
  453. ADD_INT(SQLITE_LIMIT_TRIGGER_DEPTH);
  454. #if SQLITE_VERSION_NUMBER >= 3008007
  455. ADD_INT(SQLITE_LIMIT_WORKER_THREADS);
  456. #endif
  457. /*
  458. * Database connection configuration options.
  459. * See https://www.sqlite.org/c3ref/c_dbconfig_defensive.html
  460. */
  461. ADD_INT(SQLITE_DBCONFIG_ENABLE_FKEY);
  462. ADD_INT(SQLITE_DBCONFIG_ENABLE_TRIGGER);
  463. #if SQLITE_VERSION_NUMBER >= 3012002
  464. ADD_INT(SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER);
  465. #endif
  466. #if SQLITE_VERSION_NUMBER >= 3013000
  467. ADD_INT(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION);
  468. #endif
  469. #if SQLITE_VERSION_NUMBER >= 3016000
  470. ADD_INT(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE);
  471. #endif
  472. #if SQLITE_VERSION_NUMBER >= 3020000
  473. ADD_INT(SQLITE_DBCONFIG_ENABLE_QPSG);
  474. #endif
  475. #if SQLITE_VERSION_NUMBER >= 3022000
  476. ADD_INT(SQLITE_DBCONFIG_TRIGGER_EQP);
  477. #endif
  478. #if SQLITE_VERSION_NUMBER >= 3024000
  479. ADD_INT(SQLITE_DBCONFIG_RESET_DATABASE);
  480. #endif
  481. #if SQLITE_VERSION_NUMBER >= 3026000
  482. ADD_INT(SQLITE_DBCONFIG_DEFENSIVE);
  483. #endif
  484. #if SQLITE_VERSION_NUMBER >= 3028000
  485. ADD_INT(SQLITE_DBCONFIG_WRITABLE_SCHEMA);
  486. #endif
  487. #if SQLITE_VERSION_NUMBER >= 3029000
  488. ADD_INT(SQLITE_DBCONFIG_DQS_DDL);
  489. ADD_INT(SQLITE_DBCONFIG_DQS_DML);
  490. ADD_INT(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE);
  491. #endif
  492. #if SQLITE_VERSION_NUMBER >= 3030000
  493. ADD_INT(SQLITE_DBCONFIG_ENABLE_VIEW);
  494. #endif
  495. #if SQLITE_VERSION_NUMBER >= 3031000
  496. ADD_INT(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT);
  497. ADD_INT(SQLITE_DBCONFIG_TRUSTED_SCHEMA);
  498. #endif
  499. #undef ADD_INT
  500. return 0;
  501. }
  502. /* Convert SQLite default threading mode (as set by the compile-time constant
  503. * SQLITE_THREADSAFE) to the corresponding DB-API 2.0 (PEP 249) threadsafety
  504. * level. */
  505. static int
  506. get_threadsafety(pysqlite_state *state)
  507. {
  508. int mode = sqlite3_threadsafe();
  509. switch (mode) {
  510. case 0: // Single-thread mode; threads may not share the module.
  511. return 0;
  512. case 1: // Serialized mode; threads may share the module,
  513. return 3; // connections, and cursors.
  514. case 2: // Multi-thread mode; threads may share the module, but not
  515. return 1; // connections.
  516. default:
  517. PyErr_Format(state->InterfaceError,
  518. "Unable to interpret SQLite threadsafety mode. Got %d, "
  519. "expected 0, 1, or 2", mode);
  520. return -1;
  521. }
  522. }
  523. static int
  524. module_traverse(PyObject *module, visitproc visit, void *arg)
  525. {
  526. pysqlite_state *state = pysqlite_get_state(module);
  527. // Exceptions
  528. Py_VISIT(state->DataError);
  529. Py_VISIT(state->DatabaseError);
  530. Py_VISIT(state->Error);
  531. Py_VISIT(state->IntegrityError);
  532. Py_VISIT(state->InterfaceError);
  533. Py_VISIT(state->InternalError);
  534. Py_VISIT(state->NotSupportedError);
  535. Py_VISIT(state->OperationalError);
  536. Py_VISIT(state->ProgrammingError);
  537. Py_VISIT(state->Warning);
  538. // Types
  539. Py_VISIT(state->BlobType);
  540. Py_VISIT(state->ConnectionType);
  541. Py_VISIT(state->CursorType);
  542. Py_VISIT(state->PrepareProtocolType);
  543. Py_VISIT(state->RowType);
  544. Py_VISIT(state->StatementType);
  545. // Misc
  546. Py_VISIT(state->converters);
  547. Py_VISIT(state->lru_cache);
  548. Py_VISIT(state->psyco_adapters);
  549. return 0;
  550. }
  551. static int
  552. module_clear(PyObject *module)
  553. {
  554. pysqlite_state *state = pysqlite_get_state(module);
  555. // Exceptions
  556. Py_CLEAR(state->DataError);
  557. Py_CLEAR(state->DatabaseError);
  558. Py_CLEAR(state->Error);
  559. Py_CLEAR(state->IntegrityError);
  560. Py_CLEAR(state->InterfaceError);
  561. Py_CLEAR(state->InternalError);
  562. Py_CLEAR(state->NotSupportedError);
  563. Py_CLEAR(state->OperationalError);
  564. Py_CLEAR(state->ProgrammingError);
  565. Py_CLEAR(state->Warning);
  566. // Types
  567. Py_CLEAR(state->BlobType);
  568. Py_CLEAR(state->ConnectionType);
  569. Py_CLEAR(state->CursorType);
  570. Py_CLEAR(state->PrepareProtocolType);
  571. Py_CLEAR(state->RowType);
  572. Py_CLEAR(state->StatementType);
  573. // Misc
  574. Py_CLEAR(state->converters);
  575. Py_CLEAR(state->lru_cache);
  576. Py_CLEAR(state->psyco_adapters);
  577. // Interned strings
  578. Py_CLEAR(state->str___adapt__);
  579. Py_CLEAR(state->str___conform__);
  580. Py_CLEAR(state->str_executescript);
  581. Py_CLEAR(state->str_finalize);
  582. Py_CLEAR(state->str_inverse);
  583. Py_CLEAR(state->str_step);
  584. Py_CLEAR(state->str_upper);
  585. Py_CLEAR(state->str_value);
  586. return 0;
  587. }
  588. static void
  589. module_free(void *module)
  590. {
  591. module_clear((PyObject *)module);
  592. }
  593. #define ADD_TYPE(module, type) \
  594. do { \
  595. if (PyModule_AddType(module, type) < 0) { \
  596. goto error; \
  597. } \
  598. } while (0)
  599. #define ADD_EXCEPTION(module, state, exc, base) \
  600. do { \
  601. state->exc = PyErr_NewException(MODULE_NAME "." #exc, base, NULL); \
  602. if (state->exc == NULL) { \
  603. goto error; \
  604. } \
  605. ADD_TYPE(module, (PyTypeObject *)state->exc); \
  606. } while (0)
  607. #define ADD_INTERNED(state, string) \
  608. do { \
  609. PyObject *tmp = PyUnicode_InternFromString(#string); \
  610. if (tmp == NULL) { \
  611. goto error; \
  612. } \
  613. state->str_ ## string = tmp; \
  614. } while (0)
  615. static int
  616. module_exec(PyObject *module)
  617. {
  618. if (sqlite3_libversion_number() < 3007015) {
  619. PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required");
  620. return -1;
  621. }
  622. int rc = sqlite3_initialize();
  623. if (rc != SQLITE_OK) {
  624. PyErr_SetString(PyExc_ImportError, sqlite3_errstr(rc));
  625. return -1;
  626. }
  627. if ((pysqlite_row_setup_types(module) < 0) ||
  628. (pysqlite_cursor_setup_types(module) < 0) ||
  629. (pysqlite_connection_setup_types(module) < 0) ||
  630. (pysqlite_statement_setup_types(module) < 0) ||
  631. (pysqlite_prepare_protocol_setup_types(module) < 0) ||
  632. (pysqlite_blob_setup_types(module) < 0)
  633. ) {
  634. goto error;
  635. }
  636. pysqlite_state *state = pysqlite_get_state(module);
  637. ADD_TYPE(module, state->BlobType);
  638. ADD_TYPE(module, state->ConnectionType);
  639. ADD_TYPE(module, state->CursorType);
  640. ADD_TYPE(module, state->PrepareProtocolType);
  641. ADD_TYPE(module, state->RowType);
  642. /*** Create DB-API Exception hierarchy */
  643. ADD_EXCEPTION(module, state, Error, PyExc_Exception);
  644. ADD_EXCEPTION(module, state, Warning, PyExc_Exception);
  645. /* Error subclasses */
  646. ADD_EXCEPTION(module, state, InterfaceError, state->Error);
  647. ADD_EXCEPTION(module, state, DatabaseError, state->Error);
  648. /* DatabaseError subclasses */
  649. ADD_EXCEPTION(module, state, InternalError, state->DatabaseError);
  650. ADD_EXCEPTION(module, state, OperationalError, state->DatabaseError);
  651. ADD_EXCEPTION(module, state, ProgrammingError, state->DatabaseError);
  652. ADD_EXCEPTION(module, state, IntegrityError, state->DatabaseError);
  653. ADD_EXCEPTION(module, state, DataError, state->DatabaseError);
  654. ADD_EXCEPTION(module, state, NotSupportedError, state->DatabaseError);
  655. /* Add interned strings */
  656. ADD_INTERNED(state, __adapt__);
  657. ADD_INTERNED(state, __conform__);
  658. ADD_INTERNED(state, executescript);
  659. ADD_INTERNED(state, finalize);
  660. ADD_INTERNED(state, inverse);
  661. ADD_INTERNED(state, step);
  662. ADD_INTERNED(state, upper);
  663. ADD_INTERNED(state, value);
  664. /* Set error constants */
  665. if (add_error_constants(module) < 0) {
  666. goto error;
  667. }
  668. /* Set integer constants */
  669. if (add_integer_constants(module) < 0) {
  670. goto error;
  671. }
  672. if (PyModule_AddStringConstant(module, "_deprecated_version", PYSQLITE_VERSION) < 0) {
  673. goto error;
  674. }
  675. if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) {
  676. goto error;
  677. }
  678. if (PyModule_AddIntMacro(module, LEGACY_TRANSACTION_CONTROL) < 0) {
  679. goto error;
  680. }
  681. int threadsafety = get_threadsafety(state);
  682. if (threadsafety < 0) {
  683. goto error;
  684. }
  685. if (PyModule_AddIntConstant(module, "threadsafety", threadsafety) < 0) {
  686. goto error;
  687. }
  688. /* initialize microprotocols layer */
  689. if (pysqlite_microprotocols_init(module) < 0) {
  690. goto error;
  691. }
  692. /* initialize the default converters */
  693. if (converters_init(module) < 0) {
  694. goto error;
  695. }
  696. if (load_functools_lru_cache(module) < 0) {
  697. goto error;
  698. }
  699. return 0;
  700. error:
  701. sqlite3_shutdown();
  702. return -1;
  703. }
  704. static struct PyModuleDef_Slot module_slots[] = {
  705. {Py_mod_exec, module_exec},
  706. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  707. {0, NULL},
  708. };
  709. struct PyModuleDef _sqlite3module = {
  710. .m_base = PyModuleDef_HEAD_INIT,
  711. .m_name = "_sqlite3",
  712. .m_size = sizeof(pysqlite_state),
  713. .m_methods = module_methods,
  714. .m_slots = module_slots,
  715. .m_traverse = module_traverse,
  716. .m_clear = module_clear,
  717. .m_free = module_free,
  718. };
  719. PyMODINIT_FUNC
  720. PyInit__sqlite3(void)
  721. {
  722. return PyModuleDef_Init(&_sqlite3module);
  723. }