cursor.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /* cursor.c - the cursor type
  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 "cursor.h"
  24. #include "microprotocols.h"
  25. #include "module.h"
  26. #include "util.h"
  27. typedef enum {
  28. TYPE_LONG,
  29. TYPE_FLOAT,
  30. TYPE_UNICODE,
  31. TYPE_BUFFER,
  32. TYPE_UNKNOWN
  33. } parameter_type;
  34. #define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
  35. #include "clinic/cursor.c.h"
  36. #undef clinic_state
  37. static inline int
  38. check_cursor_locked(pysqlite_Cursor *cur)
  39. {
  40. if (cur->locked) {
  41. PyErr_SetString(cur->connection->ProgrammingError,
  42. "Recursive use of cursors not allowed.");
  43. return 0;
  44. }
  45. return 1;
  46. }
  47. /*[clinic input]
  48. module _sqlite3
  49. class _sqlite3.Cursor "pysqlite_Cursor *" "clinic_state()->CursorType"
  50. [clinic start generated code]*/
  51. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c5b8115c5cf30f1]*/
  52. /*
  53. * Registers a cursor with the connection.
  54. *
  55. * 0 => error; 1 => ok
  56. */
  57. static int
  58. register_cursor(pysqlite_Connection *connection, PyObject *cursor)
  59. {
  60. PyObject *weakref = PyWeakref_NewRef((PyObject *)cursor, NULL);
  61. if (weakref == NULL) {
  62. return 0;
  63. }
  64. if (PyList_Append(connection->cursors, weakref) < 0) {
  65. Py_CLEAR(weakref);
  66. return 0;
  67. }
  68. Py_DECREF(weakref);
  69. return 1;
  70. }
  71. /*[clinic input]
  72. _sqlite3.Cursor.__init__ as pysqlite_cursor_init
  73. connection: object(type='pysqlite_Connection *', subclass_of='clinic_state()->ConnectionType')
  74. /
  75. [clinic start generated code]*/
  76. static int
  77. pysqlite_cursor_init_impl(pysqlite_Cursor *self,
  78. pysqlite_Connection *connection)
  79. /*[clinic end generated code: output=ac59dce49a809ca8 input=23d4265b534989fb]*/
  80. {
  81. if (!check_cursor_locked(self)) {
  82. return -1;
  83. }
  84. Py_INCREF(connection);
  85. Py_XSETREF(self->connection, connection);
  86. Py_CLEAR(self->statement);
  87. Py_CLEAR(self->row_cast_map);
  88. Py_INCREF(Py_None);
  89. Py_XSETREF(self->description, Py_None);
  90. Py_INCREF(Py_None);
  91. Py_XSETREF(self->lastrowid, Py_None);
  92. self->arraysize = 1;
  93. self->closed = 0;
  94. self->rowcount = -1L;
  95. Py_INCREF(Py_None);
  96. Py_XSETREF(self->row_factory, Py_None);
  97. if (!pysqlite_check_thread(self->connection)) {
  98. return -1;
  99. }
  100. if (!register_cursor(connection, (PyObject *)self)) {
  101. return -1;
  102. }
  103. self->initialized = 1;
  104. return 0;
  105. }
  106. static inline int
  107. stmt_reset(pysqlite_Statement *self)
  108. {
  109. int rc = SQLITE_OK;
  110. if (self->st != NULL) {
  111. Py_BEGIN_ALLOW_THREADS
  112. rc = sqlite3_reset(self->st);
  113. Py_END_ALLOW_THREADS
  114. }
  115. return rc;
  116. }
  117. static int
  118. cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg)
  119. {
  120. Py_VISIT(Py_TYPE(self));
  121. Py_VISIT(self->connection);
  122. Py_VISIT(self->description);
  123. Py_VISIT(self->row_cast_map);
  124. Py_VISIT(self->lastrowid);
  125. Py_VISIT(self->row_factory);
  126. Py_VISIT(self->statement);
  127. return 0;
  128. }
  129. static int
  130. cursor_clear(pysqlite_Cursor *self)
  131. {
  132. Py_CLEAR(self->connection);
  133. Py_CLEAR(self->description);
  134. Py_CLEAR(self->row_cast_map);
  135. Py_CLEAR(self->lastrowid);
  136. Py_CLEAR(self->row_factory);
  137. if (self->statement) {
  138. /* Reset the statement if the user has not closed the cursor */
  139. stmt_reset(self->statement);
  140. Py_CLEAR(self->statement);
  141. }
  142. return 0;
  143. }
  144. static void
  145. cursor_dealloc(pysqlite_Cursor *self)
  146. {
  147. PyTypeObject *tp = Py_TYPE(self);
  148. PyObject_GC_UnTrack(self);
  149. if (self->in_weakreflist != NULL) {
  150. PyObject_ClearWeakRefs((PyObject*)self);
  151. }
  152. tp->tp_clear((PyObject *)self);
  153. tp->tp_free(self);
  154. Py_DECREF(tp);
  155. }
  156. static PyObject *
  157. _pysqlite_get_converter(pysqlite_state *state, const char *keystr,
  158. Py_ssize_t keylen)
  159. {
  160. PyObject *key;
  161. PyObject *upcase_key;
  162. PyObject *retval;
  163. key = PyUnicode_FromStringAndSize(keystr, keylen);
  164. if (!key) {
  165. return NULL;
  166. }
  167. upcase_key = PyObject_CallMethodNoArgs(key, state->str_upper);
  168. Py_DECREF(key);
  169. if (!upcase_key) {
  170. return NULL;
  171. }
  172. retval = PyDict_GetItemWithError(state->converters, upcase_key);
  173. Py_DECREF(upcase_key);
  174. return retval;
  175. }
  176. static int
  177. pysqlite_build_row_cast_map(pysqlite_Cursor* self)
  178. {
  179. int i;
  180. const char* pos;
  181. const char* decltype;
  182. PyObject* converter;
  183. if (!self->connection->detect_types) {
  184. return 0;
  185. }
  186. Py_XSETREF(self->row_cast_map, PyList_New(0));
  187. if (!self->row_cast_map) {
  188. return -1;
  189. }
  190. for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
  191. converter = NULL;
  192. if (self->connection->detect_types & PARSE_COLNAMES) {
  193. const char *colname = sqlite3_column_name(self->statement->st, i);
  194. if (colname == NULL) {
  195. PyErr_NoMemory();
  196. Py_CLEAR(self->row_cast_map);
  197. return -1;
  198. }
  199. const char *type_start = NULL;
  200. for (pos = colname; *pos != 0; pos++) {
  201. if (*pos == '[') {
  202. type_start = pos + 1;
  203. }
  204. else if (*pos == ']' && type_start != NULL) {
  205. pysqlite_state *state = self->connection->state;
  206. converter = _pysqlite_get_converter(state, type_start,
  207. pos - type_start);
  208. if (!converter && PyErr_Occurred()) {
  209. Py_CLEAR(self->row_cast_map);
  210. return -1;
  211. }
  212. break;
  213. }
  214. }
  215. }
  216. if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
  217. decltype = sqlite3_column_decltype(self->statement->st, i);
  218. if (decltype) {
  219. for (pos = decltype;;pos++) {
  220. /* Converter names are split at '(' and blanks.
  221. * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
  222. * 'NUMBER(10)' to be treated as 'NUMBER', for example.
  223. * In other words, it will work as people expect it to work.*/
  224. if (*pos == ' ' || *pos == '(' || *pos == 0) {
  225. pysqlite_state *state = self->connection->state;
  226. converter = _pysqlite_get_converter(state, decltype,
  227. pos - decltype);
  228. if (!converter && PyErr_Occurred()) {
  229. Py_CLEAR(self->row_cast_map);
  230. return -1;
  231. }
  232. break;
  233. }
  234. }
  235. }
  236. }
  237. if (!converter) {
  238. converter = Py_None;
  239. }
  240. if (PyList_Append(self->row_cast_map, converter) != 0) {
  241. Py_CLEAR(self->row_cast_map);
  242. return -1;
  243. }
  244. }
  245. return 0;
  246. }
  247. static PyObject *
  248. _pysqlite_build_column_name(pysqlite_Cursor *self, const char *colname)
  249. {
  250. const char* pos;
  251. Py_ssize_t len;
  252. if (self->connection->detect_types & PARSE_COLNAMES) {
  253. for (pos = colname; *pos; pos++) {
  254. if (*pos == '[') {
  255. if ((pos != colname) && (*(pos-1) == ' ')) {
  256. pos--;
  257. }
  258. break;
  259. }
  260. }
  261. len = pos - colname;
  262. }
  263. else {
  264. len = strlen(colname);
  265. }
  266. return PyUnicode_FromStringAndSize(colname, len);
  267. }
  268. /*
  269. * Returns a row from the currently active SQLite statement
  270. *
  271. * Precondidition:
  272. * - sqlite3_step() has been called before and it returned SQLITE_ROW.
  273. */
  274. static PyObject *
  275. _pysqlite_fetch_one_row(pysqlite_Cursor* self)
  276. {
  277. int i, numcols;
  278. PyObject* row;
  279. int coltype;
  280. PyObject* converter;
  281. PyObject* converted;
  282. Py_ssize_t nbytes;
  283. char buf[200];
  284. const char* colname;
  285. PyObject* error_msg;
  286. Py_BEGIN_ALLOW_THREADS
  287. numcols = sqlite3_data_count(self->statement->st);
  288. Py_END_ALLOW_THREADS
  289. row = PyTuple_New(numcols);
  290. if (!row)
  291. return NULL;
  292. sqlite3 *db = self->connection->db;
  293. for (i = 0; i < numcols; i++) {
  294. if (self->connection->detect_types
  295. && self->row_cast_map != NULL
  296. && i < PyList_GET_SIZE(self->row_cast_map))
  297. {
  298. converter = PyList_GET_ITEM(self->row_cast_map, i);
  299. }
  300. else {
  301. converter = Py_None;
  302. }
  303. /*
  304. * Note, sqlite3_column_bytes() must come after sqlite3_column_blob()
  305. * or sqlite3_column_text().
  306. *
  307. * See https://sqlite.org/c3ref/column_blob.html for details.
  308. */
  309. if (converter != Py_None) {
  310. const void *blob = sqlite3_column_blob(self->statement->st, i);
  311. if (blob == NULL) {
  312. if (sqlite3_errcode(db) == SQLITE_NOMEM) {
  313. PyErr_NoMemory();
  314. goto error;
  315. }
  316. converted = Py_NewRef(Py_None);
  317. }
  318. else {
  319. nbytes = sqlite3_column_bytes(self->statement->st, i);
  320. PyObject *item = PyBytes_FromStringAndSize(blob, nbytes);
  321. if (item == NULL) {
  322. goto error;
  323. }
  324. converted = PyObject_CallOneArg(converter, item);
  325. Py_DECREF(item);
  326. }
  327. } else {
  328. Py_BEGIN_ALLOW_THREADS
  329. coltype = sqlite3_column_type(self->statement->st, i);
  330. Py_END_ALLOW_THREADS
  331. if (coltype == SQLITE_NULL) {
  332. converted = Py_NewRef(Py_None);
  333. } else if (coltype == SQLITE_INTEGER) {
  334. converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i));
  335. } else if (coltype == SQLITE_FLOAT) {
  336. converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
  337. } else if (coltype == SQLITE_TEXT) {
  338. const char *text = (const char*)sqlite3_column_text(self->statement->st, i);
  339. if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
  340. PyErr_NoMemory();
  341. goto error;
  342. }
  343. nbytes = sqlite3_column_bytes(self->statement->st, i);
  344. if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
  345. converted = PyUnicode_FromStringAndSize(text, nbytes);
  346. if (!converted && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
  347. PyErr_Clear();
  348. colname = sqlite3_column_name(self->statement->st, i);
  349. if (colname == NULL) {
  350. PyErr_NoMemory();
  351. goto error;
  352. }
  353. PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
  354. colname , text);
  355. error_msg = PyUnicode_Decode(buf, strlen(buf), "ascii", "replace");
  356. PyObject *exc = self->connection->OperationalError;
  357. if (!error_msg) {
  358. PyErr_SetString(exc, "Could not decode to UTF-8");
  359. } else {
  360. PyErr_SetObject(exc, error_msg);
  361. Py_DECREF(error_msg);
  362. }
  363. }
  364. } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
  365. converted = PyBytes_FromStringAndSize(text, nbytes);
  366. } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
  367. converted = PyByteArray_FromStringAndSize(text, nbytes);
  368. } else {
  369. converted = PyObject_CallFunction(self->connection->text_factory, "y#", text, nbytes);
  370. }
  371. } else {
  372. /* coltype == SQLITE_BLOB */
  373. const void *blob = sqlite3_column_blob(self->statement->st, i);
  374. if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
  375. PyErr_NoMemory();
  376. goto error;
  377. }
  378. nbytes = sqlite3_column_bytes(self->statement->st, i);
  379. converted = PyBytes_FromStringAndSize(blob, nbytes);
  380. }
  381. }
  382. if (!converted) {
  383. goto error;
  384. }
  385. PyTuple_SET_ITEM(row, i, converted);
  386. }
  387. if (PyErr_Occurred())
  388. goto error;
  389. return row;
  390. error:
  391. Py_DECREF(row);
  392. return NULL;
  393. }
  394. /*
  395. * Checks if a cursor object is usable.
  396. *
  397. * 0 => error; 1 => ok
  398. */
  399. static int check_cursor(pysqlite_Cursor* cur)
  400. {
  401. if (!cur->initialized) {
  402. pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(cur));
  403. PyErr_SetString(state->ProgrammingError,
  404. "Base Cursor.__init__ not called.");
  405. return 0;
  406. }
  407. if (cur->closed) {
  408. PyErr_SetString(cur->connection->state->ProgrammingError,
  409. "Cannot operate on a closed cursor.");
  410. return 0;
  411. }
  412. return (pysqlite_check_thread(cur->connection)
  413. && pysqlite_check_connection(cur->connection)
  414. && check_cursor_locked(cur));
  415. }
  416. static int
  417. begin_transaction(pysqlite_Connection *self)
  418. {
  419. assert(self->isolation_level != NULL);
  420. int rc;
  421. Py_BEGIN_ALLOW_THREADS
  422. sqlite3_stmt *statement;
  423. char begin_stmt[16] = "BEGIN ";
  424. #ifdef Py_DEBUG
  425. size_t len = strlen(self->isolation_level);
  426. assert(len <= 9);
  427. #endif
  428. (void)strcat(begin_stmt, self->isolation_level);
  429. rc = sqlite3_prepare_v2(self->db, begin_stmt, -1, &statement, NULL);
  430. if (rc == SQLITE_OK) {
  431. (void)sqlite3_step(statement);
  432. rc = sqlite3_finalize(statement);
  433. }
  434. Py_END_ALLOW_THREADS
  435. if (rc != SQLITE_OK) {
  436. (void)_pysqlite_seterror(self->state, self->db);
  437. return -1;
  438. }
  439. return 0;
  440. }
  441. static PyObject *
  442. get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
  443. {
  444. PyObject *args[] = { NULL, operation, }; // Borrowed ref.
  445. PyObject *cache = self->connection->statement_cache;
  446. size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
  447. return PyObject_Vectorcall(cache, args + 1, nargsf, NULL);
  448. }
  449. static inline int
  450. stmt_step(sqlite3_stmt *statement)
  451. {
  452. int rc;
  453. Py_BEGIN_ALLOW_THREADS
  454. rc = sqlite3_step(statement);
  455. Py_END_ALLOW_THREADS
  456. return rc;
  457. }
  458. static int
  459. bind_param(pysqlite_state *state, pysqlite_Statement *self, int pos,
  460. PyObject *parameter)
  461. {
  462. int rc = SQLITE_OK;
  463. const char *string;
  464. Py_ssize_t buflen;
  465. parameter_type paramtype;
  466. if (parameter == Py_None) {
  467. rc = sqlite3_bind_null(self->st, pos);
  468. goto final;
  469. }
  470. if (PyLong_CheckExact(parameter)) {
  471. paramtype = TYPE_LONG;
  472. } else if (PyFloat_CheckExact(parameter)) {
  473. paramtype = TYPE_FLOAT;
  474. } else if (PyUnicode_CheckExact(parameter)) {
  475. paramtype = TYPE_UNICODE;
  476. } else if (PyLong_Check(parameter)) {
  477. paramtype = TYPE_LONG;
  478. } else if (PyFloat_Check(parameter)) {
  479. paramtype = TYPE_FLOAT;
  480. } else if (PyUnicode_Check(parameter)) {
  481. paramtype = TYPE_UNICODE;
  482. } else if (PyObject_CheckBuffer(parameter)) {
  483. paramtype = TYPE_BUFFER;
  484. } else {
  485. paramtype = TYPE_UNKNOWN;
  486. }
  487. switch (paramtype) {
  488. case TYPE_LONG: {
  489. sqlite_int64 value = _pysqlite_long_as_int64(parameter);
  490. if (value == -1 && PyErr_Occurred())
  491. rc = -1;
  492. else
  493. rc = sqlite3_bind_int64(self->st, pos, value);
  494. break;
  495. }
  496. case TYPE_FLOAT: {
  497. double value = PyFloat_AsDouble(parameter);
  498. if (value == -1 && PyErr_Occurred()) {
  499. rc = -1;
  500. }
  501. else {
  502. rc = sqlite3_bind_double(self->st, pos, value);
  503. }
  504. break;
  505. }
  506. case TYPE_UNICODE:
  507. string = PyUnicode_AsUTF8AndSize(parameter, &buflen);
  508. if (string == NULL)
  509. return -1;
  510. if (buflen > INT_MAX) {
  511. PyErr_SetString(PyExc_OverflowError,
  512. "string longer than INT_MAX bytes");
  513. return -1;
  514. }
  515. rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT);
  516. break;
  517. case TYPE_BUFFER: {
  518. Py_buffer view;
  519. if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) {
  520. return -1;
  521. }
  522. if (view.len > INT_MAX) {
  523. PyErr_SetString(PyExc_OverflowError,
  524. "BLOB longer than INT_MAX bytes");
  525. PyBuffer_Release(&view);
  526. return -1;
  527. }
  528. rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT);
  529. PyBuffer_Release(&view);
  530. break;
  531. }
  532. case TYPE_UNKNOWN:
  533. PyErr_Format(state->ProgrammingError,
  534. "Error binding parameter %d: type '%s' is not supported",
  535. pos, Py_TYPE(parameter)->tp_name);
  536. rc = -1;
  537. }
  538. final:
  539. return rc;
  540. }
  541. /* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
  542. static inline int
  543. need_adapt(pysqlite_state *state, PyObject *obj)
  544. {
  545. if (state->BaseTypeAdapted) {
  546. return 1;
  547. }
  548. if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj)
  549. || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) {
  550. return 0;
  551. } else {
  552. return 1;
  553. }
  554. }
  555. static void
  556. bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
  557. PyObject *parameters)
  558. {
  559. PyObject* current_param;
  560. PyObject* adapted;
  561. const char* binding_name;
  562. int i;
  563. int rc;
  564. int num_params_needed;
  565. Py_ssize_t num_params;
  566. Py_BEGIN_ALLOW_THREADS
  567. num_params_needed = sqlite3_bind_parameter_count(self->st);
  568. Py_END_ALLOW_THREADS
  569. if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) {
  570. /* parameters passed as sequence */
  571. if (PyTuple_CheckExact(parameters)) {
  572. num_params = PyTuple_GET_SIZE(parameters);
  573. } else if (PyList_CheckExact(parameters)) {
  574. num_params = PyList_GET_SIZE(parameters);
  575. } else {
  576. num_params = PySequence_Size(parameters);
  577. if (num_params == -1) {
  578. return;
  579. }
  580. }
  581. if (num_params != num_params_needed) {
  582. PyErr_Format(state->ProgrammingError,
  583. "Incorrect number of bindings supplied. The current "
  584. "statement uses %d, and there are %zd supplied.",
  585. num_params_needed, num_params);
  586. return;
  587. }
  588. for (i = 0; i < num_params; i++) {
  589. const char *name = sqlite3_bind_parameter_name(self->st, i+1);
  590. if (name != NULL && name[0] != '?') {
  591. int ret = PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
  592. "Binding %d ('%s') is a named parameter, but you "
  593. "supplied a sequence which requires nameless (qmark) "
  594. "placeholders. Starting with Python 3.14 an "
  595. "sqlite3.ProgrammingError will be raised.",
  596. i+1, name);
  597. if (ret < 0) {
  598. return;
  599. }
  600. }
  601. if (PyTuple_CheckExact(parameters)) {
  602. PyObject *item = PyTuple_GET_ITEM(parameters, i);
  603. current_param = Py_NewRef(item);
  604. } else if (PyList_CheckExact(parameters)) {
  605. PyObject *item = PyList_GetItem(parameters, i);
  606. current_param = Py_XNewRef(item);
  607. } else {
  608. current_param = PySequence_GetItem(parameters, i);
  609. }
  610. if (!current_param) {
  611. return;
  612. }
  613. if (!need_adapt(state, current_param)) {
  614. adapted = current_param;
  615. } else {
  616. PyObject *protocol = (PyObject *)state->PrepareProtocolType;
  617. adapted = pysqlite_microprotocols_adapt(state, current_param,
  618. protocol,
  619. current_param);
  620. Py_DECREF(current_param);
  621. if (!adapted) {
  622. return;
  623. }
  624. }
  625. rc = bind_param(state, self, i + 1, adapted);
  626. Py_DECREF(adapted);
  627. if (rc != SQLITE_OK) {
  628. PyObject *exc = PyErr_GetRaisedException();
  629. sqlite3 *db = sqlite3_db_handle(self->st);
  630. _pysqlite_seterror(state, db);
  631. _PyErr_ChainExceptions1(exc);
  632. return;
  633. }
  634. }
  635. } else if (PyDict_Check(parameters)) {
  636. /* parameters passed as dictionary */
  637. for (i = 1; i <= num_params_needed; i++) {
  638. PyObject *binding_name_obj;
  639. Py_BEGIN_ALLOW_THREADS
  640. binding_name = sqlite3_bind_parameter_name(self->st, i);
  641. Py_END_ALLOW_THREADS
  642. if (!binding_name) {
  643. PyErr_Format(state->ProgrammingError,
  644. "Binding %d has no name, but you supplied a "
  645. "dictionary (which has only names).", i);
  646. return;
  647. }
  648. binding_name++; /* skip first char (the colon) */
  649. binding_name_obj = PyUnicode_FromString(binding_name);
  650. if (!binding_name_obj) {
  651. return;
  652. }
  653. if (PyDict_CheckExact(parameters)) {
  654. PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj);
  655. current_param = Py_XNewRef(item);
  656. } else {
  657. current_param = PyObject_GetItem(parameters, binding_name_obj);
  658. }
  659. Py_DECREF(binding_name_obj);
  660. if (!current_param) {
  661. if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_LookupError)) {
  662. PyErr_Format(state->ProgrammingError,
  663. "You did not supply a value for binding "
  664. "parameter :%s.", binding_name);
  665. }
  666. return;
  667. }
  668. if (!need_adapt(state, current_param)) {
  669. adapted = current_param;
  670. } else {
  671. PyObject *protocol = (PyObject *)state->PrepareProtocolType;
  672. adapted = pysqlite_microprotocols_adapt(state, current_param,
  673. protocol,
  674. current_param);
  675. Py_DECREF(current_param);
  676. if (!adapted) {
  677. return;
  678. }
  679. }
  680. rc = bind_param(state, self, i, adapted);
  681. Py_DECREF(adapted);
  682. if (rc != SQLITE_OK) {
  683. PyObject *exc = PyErr_GetRaisedException();
  684. sqlite3 *db = sqlite3_db_handle(self->st);
  685. _pysqlite_seterror(state, db);
  686. _PyErr_ChainExceptions1(exc);
  687. return;
  688. }
  689. }
  690. } else {
  691. PyErr_SetString(state->ProgrammingError,
  692. "parameters are of unsupported type");
  693. }
  694. }
  695. PyObject *
  696. _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
  697. {
  698. PyObject* parameters_list = NULL;
  699. PyObject* parameters_iter = NULL;
  700. PyObject* parameters = NULL;
  701. int i;
  702. int rc;
  703. int numcols;
  704. PyObject* column_name;
  705. if (!check_cursor(self)) {
  706. goto error;
  707. }
  708. self->locked = 1;
  709. if (multiple) {
  710. if (PyIter_Check(second_argument)) {
  711. /* iterator */
  712. parameters_iter = Py_NewRef(second_argument);
  713. } else {
  714. /* sequence */
  715. parameters_iter = PyObject_GetIter(second_argument);
  716. if (!parameters_iter) {
  717. goto error;
  718. }
  719. }
  720. } else {
  721. parameters_list = PyList_New(0);
  722. if (!parameters_list) {
  723. goto error;
  724. }
  725. if (second_argument == NULL) {
  726. second_argument = PyTuple_New(0);
  727. if (!second_argument) {
  728. goto error;
  729. }
  730. } else {
  731. Py_INCREF(second_argument);
  732. }
  733. if (PyList_Append(parameters_list, second_argument) != 0) {
  734. Py_DECREF(second_argument);
  735. goto error;
  736. }
  737. Py_DECREF(second_argument);
  738. parameters_iter = PyObject_GetIter(parameters_list);
  739. if (!parameters_iter) {
  740. goto error;
  741. }
  742. }
  743. /* reset description */
  744. Py_INCREF(Py_None);
  745. Py_SETREF(self->description, Py_None);
  746. if (self->statement) {
  747. // Reset pending statements on this cursor.
  748. (void)stmt_reset(self->statement);
  749. }
  750. PyObject *stmt = get_statement_from_cache(self, operation);
  751. Py_XSETREF(self->statement, (pysqlite_Statement *)stmt);
  752. if (!self->statement) {
  753. goto error;
  754. }
  755. pysqlite_state *state = self->connection->state;
  756. if (multiple && sqlite3_stmt_readonly(self->statement->st)) {
  757. PyErr_SetString(state->ProgrammingError,
  758. "executemany() can only execute DML statements.");
  759. goto error;
  760. }
  761. if (sqlite3_stmt_busy(self->statement->st)) {
  762. Py_SETREF(self->statement,
  763. pysqlite_statement_create(self->connection, operation));
  764. if (self->statement == NULL) {
  765. goto error;
  766. }
  767. }
  768. (void)stmt_reset(self->statement);
  769. self->rowcount = self->statement->is_dml ? 0L : -1L;
  770. /* We start a transaction implicitly before a DML statement.
  771. SELECT is the only exception. See #9924. */
  772. if (self->connection->autocommit == AUTOCOMMIT_LEGACY
  773. && self->connection->isolation_level
  774. && self->statement->is_dml
  775. && sqlite3_get_autocommit(self->connection->db))
  776. {
  777. if (begin_transaction(self->connection) < 0) {
  778. goto error;
  779. }
  780. }
  781. assert(!sqlite3_stmt_busy(self->statement->st));
  782. while (1) {
  783. parameters = PyIter_Next(parameters_iter);
  784. if (!parameters) {
  785. break;
  786. }
  787. bind_parameters(state, self->statement, parameters);
  788. if (PyErr_Occurred()) {
  789. goto error;
  790. }
  791. rc = stmt_step(self->statement->st);
  792. if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
  793. if (PyErr_Occurred()) {
  794. /* there was an error that occurred in a user-defined callback */
  795. if (state->enable_callback_tracebacks) {
  796. PyErr_Print();
  797. } else {
  798. PyErr_Clear();
  799. }
  800. }
  801. _pysqlite_seterror(state, self->connection->db);
  802. goto error;
  803. }
  804. if (pysqlite_build_row_cast_map(self) != 0) {
  805. _PyErr_FormatFromCause(state->OperationalError,
  806. "Error while building row_cast_map");
  807. goto error;
  808. }
  809. assert(rc == SQLITE_ROW || rc == SQLITE_DONE);
  810. Py_BEGIN_ALLOW_THREADS
  811. numcols = sqlite3_column_count(self->statement->st);
  812. Py_END_ALLOW_THREADS
  813. if (self->description == Py_None && numcols > 0) {
  814. Py_SETREF(self->description, PyTuple_New(numcols));
  815. if (!self->description) {
  816. goto error;
  817. }
  818. for (i = 0; i < numcols; i++) {
  819. const char *colname;
  820. colname = sqlite3_column_name(self->statement->st, i);
  821. if (colname == NULL) {
  822. PyErr_NoMemory();
  823. goto error;
  824. }
  825. column_name = _pysqlite_build_column_name(self, colname);
  826. if (column_name == NULL) {
  827. goto error;
  828. }
  829. PyObject *descriptor = PyTuple_Pack(7, column_name,
  830. Py_None, Py_None, Py_None,
  831. Py_None, Py_None, Py_None);
  832. Py_DECREF(column_name);
  833. if (descriptor == NULL) {
  834. goto error;
  835. }
  836. PyTuple_SET_ITEM(self->description, i, descriptor);
  837. }
  838. }
  839. if (rc == SQLITE_DONE) {
  840. if (self->statement->is_dml) {
  841. self->rowcount += (long)sqlite3_changes(self->connection->db);
  842. }
  843. stmt_reset(self->statement);
  844. }
  845. Py_XDECREF(parameters);
  846. }
  847. if (!multiple) {
  848. sqlite_int64 lastrowid;
  849. Py_BEGIN_ALLOW_THREADS
  850. lastrowid = sqlite3_last_insert_rowid(self->connection->db);
  851. Py_END_ALLOW_THREADS
  852. Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
  853. // Fall through on error.
  854. }
  855. error:
  856. Py_XDECREF(parameters);
  857. Py_XDECREF(parameters_iter);
  858. Py_XDECREF(parameters_list);
  859. self->locked = 0;
  860. if (PyErr_Occurred()) {
  861. if (self->statement) {
  862. (void)stmt_reset(self->statement);
  863. Py_CLEAR(self->statement);
  864. }
  865. self->rowcount = -1L;
  866. return NULL;
  867. }
  868. if (self->statement && !sqlite3_stmt_busy(self->statement->st)) {
  869. Py_CLEAR(self->statement);
  870. }
  871. return Py_NewRef((PyObject *)self);
  872. }
  873. /*[clinic input]
  874. _sqlite3.Cursor.execute as pysqlite_cursor_execute
  875. sql: unicode
  876. parameters: object(c_default = 'NULL') = ()
  877. /
  878. Executes an SQL statement.
  879. [clinic start generated code]*/
  880. static PyObject *
  881. pysqlite_cursor_execute_impl(pysqlite_Cursor *self, PyObject *sql,
  882. PyObject *parameters)
  883. /*[clinic end generated code: output=d81b4655c7c0bbad input=a8e0200a11627f94]*/
  884. {
  885. return _pysqlite_query_execute(self, 0, sql, parameters);
  886. }
  887. /*[clinic input]
  888. _sqlite3.Cursor.executemany as pysqlite_cursor_executemany
  889. sql: unicode
  890. seq_of_parameters: object
  891. /
  892. Repeatedly executes an SQL statement.
  893. [clinic start generated code]*/
  894. static PyObject *
  895. pysqlite_cursor_executemany_impl(pysqlite_Cursor *self, PyObject *sql,
  896. PyObject *seq_of_parameters)
  897. /*[clinic end generated code: output=2c65a3c4733fb5d8 input=0d0a52e5eb7ccd35]*/
  898. {
  899. return _pysqlite_query_execute(self, 1, sql, seq_of_parameters);
  900. }
  901. /*[clinic input]
  902. _sqlite3.Cursor.executescript as pysqlite_cursor_executescript
  903. sql_script: str
  904. /
  905. Executes multiple SQL statements at once.
  906. [clinic start generated code]*/
  907. static PyObject *
  908. pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
  909. const char *sql_script)
  910. /*[clinic end generated code: output=8fd726dde1c65164 input=78f093be415a8a2c]*/
  911. {
  912. if (!check_cursor(self)) {
  913. return NULL;
  914. }
  915. size_t sql_len = strlen(sql_script);
  916. int max_length = sqlite3_limit(self->connection->db,
  917. SQLITE_LIMIT_SQL_LENGTH, -1);
  918. if (sql_len > (unsigned)max_length) {
  919. PyErr_SetString(self->connection->DataError,
  920. "query string is too large");
  921. return NULL;
  922. }
  923. // Commit if needed
  924. sqlite3 *db = self->connection->db;
  925. if (self->connection->autocommit == AUTOCOMMIT_LEGACY
  926. && !sqlite3_get_autocommit(db))
  927. {
  928. int rc = SQLITE_OK;
  929. Py_BEGIN_ALLOW_THREADS
  930. rc = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
  931. Py_END_ALLOW_THREADS
  932. if (rc != SQLITE_OK) {
  933. goto error;
  934. }
  935. }
  936. while (1) {
  937. int rc;
  938. const char *tail;
  939. Py_BEGIN_ALLOW_THREADS
  940. sqlite3_stmt *stmt;
  941. rc = sqlite3_prepare_v2(db, sql_script, (int)sql_len + 1, &stmt,
  942. &tail);
  943. if (rc == SQLITE_OK) {
  944. do {
  945. rc = sqlite3_step(stmt);
  946. } while (rc == SQLITE_ROW);
  947. rc = sqlite3_finalize(stmt);
  948. }
  949. Py_END_ALLOW_THREADS
  950. if (rc != SQLITE_OK) {
  951. goto error;
  952. }
  953. if (*tail == (char)0) {
  954. break;
  955. }
  956. sql_len -= (tail - sql_script);
  957. sql_script = tail;
  958. }
  959. return Py_NewRef((PyObject *)self);
  960. error:
  961. _pysqlite_seterror(self->connection->state, db);
  962. return NULL;
  963. }
  964. static PyObject *
  965. pysqlite_cursor_iternext(pysqlite_Cursor *self)
  966. {
  967. if (!check_cursor(self)) {
  968. return NULL;
  969. }
  970. if (self->statement == NULL) {
  971. return NULL;
  972. }
  973. sqlite3_stmt *stmt = self->statement->st;
  974. assert(stmt != NULL);
  975. assert(sqlite3_data_count(stmt) != 0);
  976. self->locked = 1; // GH-80254: Prevent recursive use of cursors.
  977. PyObject *row = _pysqlite_fetch_one_row(self);
  978. self->locked = 0;
  979. if (row == NULL) {
  980. return NULL;
  981. }
  982. int rc = stmt_step(stmt);
  983. if (rc == SQLITE_DONE) {
  984. if (self->statement->is_dml) {
  985. self->rowcount = (long)sqlite3_changes(self->connection->db);
  986. }
  987. (void)stmt_reset(self->statement);
  988. Py_CLEAR(self->statement);
  989. }
  990. else if (rc != SQLITE_ROW) {
  991. (void)_pysqlite_seterror(self->connection->state,
  992. self->connection->db);
  993. (void)stmt_reset(self->statement);
  994. Py_CLEAR(self->statement);
  995. Py_DECREF(row);
  996. return NULL;
  997. }
  998. if (!Py_IsNone(self->row_factory)) {
  999. PyObject *factory = self->row_factory;
  1000. PyObject *args[] = { (PyObject *)self, row, };
  1001. PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL);
  1002. Py_SETREF(row, new_row);
  1003. }
  1004. return row;
  1005. }
  1006. /*[clinic input]
  1007. _sqlite3.Cursor.fetchone as pysqlite_cursor_fetchone
  1008. Fetches one row from the resultset.
  1009. [clinic start generated code]*/
  1010. static PyObject *
  1011. pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self)
  1012. /*[clinic end generated code: output=4bd2eabf5baaddb0 input=e78294ec5980fdba]*/
  1013. {
  1014. PyObject* row;
  1015. row = pysqlite_cursor_iternext(self);
  1016. if (!row && !PyErr_Occurred()) {
  1017. Py_RETURN_NONE;
  1018. }
  1019. return row;
  1020. }
  1021. /*[clinic input]
  1022. _sqlite3.Cursor.fetchmany as pysqlite_cursor_fetchmany
  1023. size as maxrows: int(c_default='self->arraysize') = 1
  1024. The default value is set by the Cursor.arraysize attribute.
  1025. Fetches several rows from the resultset.
  1026. [clinic start generated code]*/
  1027. static PyObject *
  1028. pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows)
  1029. /*[clinic end generated code: output=a8ef31fea64d0906 input=c26e6ca3f34debd0]*/
  1030. {
  1031. PyObject* row;
  1032. PyObject* list;
  1033. int counter = 0;
  1034. list = PyList_New(0);
  1035. if (!list) {
  1036. return NULL;
  1037. }
  1038. while ((row = pysqlite_cursor_iternext(self))) {
  1039. if (PyList_Append(list, row) < 0) {
  1040. Py_DECREF(row);
  1041. break;
  1042. }
  1043. Py_DECREF(row);
  1044. if (++counter == maxrows) {
  1045. break;
  1046. }
  1047. }
  1048. if (PyErr_Occurred()) {
  1049. Py_DECREF(list);
  1050. return NULL;
  1051. } else {
  1052. return list;
  1053. }
  1054. }
  1055. /*[clinic input]
  1056. _sqlite3.Cursor.fetchall as pysqlite_cursor_fetchall
  1057. Fetches all rows from the resultset.
  1058. [clinic start generated code]*/
  1059. static PyObject *
  1060. pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self)
  1061. /*[clinic end generated code: output=d5da12aca2da4b27 input=f5d401086a8df25a]*/
  1062. {
  1063. PyObject* row;
  1064. PyObject* list;
  1065. list = PyList_New(0);
  1066. if (!list) {
  1067. return NULL;
  1068. }
  1069. while ((row = pysqlite_cursor_iternext(self))) {
  1070. if (PyList_Append(list, row) < 0) {
  1071. Py_DECREF(row);
  1072. break;
  1073. }
  1074. Py_DECREF(row);
  1075. }
  1076. if (PyErr_Occurred()) {
  1077. Py_DECREF(list);
  1078. return NULL;
  1079. } else {
  1080. return list;
  1081. }
  1082. }
  1083. /*[clinic input]
  1084. _sqlite3.Cursor.setinputsizes as pysqlite_cursor_setinputsizes
  1085. sizes: object
  1086. /
  1087. Required by DB-API. Does nothing in sqlite3.
  1088. [clinic start generated code]*/
  1089. static PyObject *
  1090. pysqlite_cursor_setinputsizes(pysqlite_Cursor *self, PyObject *sizes)
  1091. /*[clinic end generated code: output=893c817afe9d08ad input=de7950a3aec79bdf]*/
  1092. {
  1093. Py_RETURN_NONE;
  1094. }
  1095. /*[clinic input]
  1096. _sqlite3.Cursor.setoutputsize as pysqlite_cursor_setoutputsize
  1097. size: object
  1098. column: object = None
  1099. /
  1100. Required by DB-API. Does nothing in sqlite3.
  1101. [clinic start generated code]*/
  1102. static PyObject *
  1103. pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size,
  1104. PyObject *column)
  1105. /*[clinic end generated code: output=018d7e9129d45efe input=607a6bece8bbb273]*/
  1106. {
  1107. Py_RETURN_NONE;
  1108. }
  1109. /*[clinic input]
  1110. _sqlite3.Cursor.close as pysqlite_cursor_close
  1111. Closes the cursor.
  1112. [clinic start generated code]*/
  1113. static PyObject *
  1114. pysqlite_cursor_close_impl(pysqlite_Cursor *self)
  1115. /*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
  1116. {
  1117. if (!check_cursor_locked(self)) {
  1118. return NULL;
  1119. }
  1120. if (!self->connection) {
  1121. PyTypeObject *tp = Py_TYPE(self);
  1122. pysqlite_state *state = pysqlite_get_state_by_type(tp);
  1123. PyErr_SetString(state->ProgrammingError,
  1124. "Base Cursor.__init__ not called.");
  1125. return NULL;
  1126. }
  1127. if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
  1128. return NULL;
  1129. }
  1130. if (self->statement) {
  1131. (void)stmt_reset(self->statement);
  1132. Py_CLEAR(self->statement);
  1133. }
  1134. self->closed = 1;
  1135. Py_RETURN_NONE;
  1136. }
  1137. static PyMethodDef cursor_methods[] = {
  1138. PYSQLITE_CURSOR_CLOSE_METHODDEF
  1139. PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF
  1140. PYSQLITE_CURSOR_EXECUTESCRIPT_METHODDEF
  1141. PYSQLITE_CURSOR_EXECUTE_METHODDEF
  1142. PYSQLITE_CURSOR_FETCHALL_METHODDEF
  1143. PYSQLITE_CURSOR_FETCHMANY_METHODDEF
  1144. PYSQLITE_CURSOR_FETCHONE_METHODDEF
  1145. PYSQLITE_CURSOR_SETINPUTSIZES_METHODDEF
  1146. PYSQLITE_CURSOR_SETOUTPUTSIZE_METHODDEF
  1147. {NULL, NULL}
  1148. };
  1149. static struct PyMemberDef cursor_members[] =
  1150. {
  1151. {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
  1152. {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
  1153. {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
  1154. {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
  1155. {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
  1156. {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
  1157. {"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Cursor, in_weakreflist), READONLY},
  1158. {NULL}
  1159. };
  1160. static const char cursor_doc[] =
  1161. PyDoc_STR("SQLite database cursor class.");
  1162. static PyType_Slot cursor_slots[] = {
  1163. {Py_tp_dealloc, cursor_dealloc},
  1164. {Py_tp_doc, (void *)cursor_doc},
  1165. {Py_tp_iter, PyObject_SelfIter},
  1166. {Py_tp_iternext, pysqlite_cursor_iternext},
  1167. {Py_tp_methods, cursor_methods},
  1168. {Py_tp_members, cursor_members},
  1169. {Py_tp_init, pysqlite_cursor_init},
  1170. {Py_tp_traverse, cursor_traverse},
  1171. {Py_tp_clear, cursor_clear},
  1172. {0, NULL},
  1173. };
  1174. static PyType_Spec cursor_spec = {
  1175. .name = MODULE_NAME ".Cursor",
  1176. .basicsize = sizeof(pysqlite_Cursor),
  1177. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
  1178. Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
  1179. .slots = cursor_slots,
  1180. };
  1181. int
  1182. pysqlite_cursor_setup_types(PyObject *module)
  1183. {
  1184. PyObject *type = PyType_FromModuleAndSpec(module, &cursor_spec, NULL);
  1185. if (type == NULL) {
  1186. return -1;
  1187. }
  1188. pysqlite_state *state = pysqlite_get_state(module);
  1189. state->CursorType = (PyTypeObject *)type;
  1190. return 0;
  1191. }