diff options
| author | AlexSm <[email protected]> | 2024-03-05 10:40:59 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-05 12:40:59 +0300 |
| commit | 1ac13c847b5358faba44dbb638a828e24369467b (patch) | |
| tree | 07672b4dd3604ad3dee540a02c6494cb7d10dc3d /contrib/tools/python3/src/Modules/_sqlite | |
| parent | ffcca3e7f7958ddc6487b91d3df8c01054bd0638 (diff) | |
Library import 16 (#2433)
Co-authored-by: robot-piglet <[email protected]>
Co-authored-by: deshevoy <[email protected]>
Co-authored-by: robot-contrib <[email protected]>
Co-authored-by: thegeorg <[email protected]>
Co-authored-by: robot-ya-builder <[email protected]>
Co-authored-by: svidyuk <[email protected]>
Co-authored-by: shadchin <[email protected]>
Co-authored-by: robot-ratatosk <[email protected]>
Co-authored-by: innokentii <[email protected]>
Co-authored-by: arkady-e1ppa <[email protected]>
Co-authored-by: snermolaev <[email protected]>
Co-authored-by: dimdim11 <[email protected]>
Co-authored-by: kickbutt <[email protected]>
Co-authored-by: abdullinsaid <[email protected]>
Co-authored-by: korsunandrei <[email protected]>
Co-authored-by: petrk <[email protected]>
Co-authored-by: miroslav2 <[email protected]>
Co-authored-by: serjflint <[email protected]>
Co-authored-by: akhropov <[email protected]>
Co-authored-by: prettyboy <[email protected]>
Co-authored-by: ilikepugs <[email protected]>
Co-authored-by: hiddenpath <[email protected]>
Co-authored-by: mikhnenko <[email protected]>
Co-authored-by: spreis <[email protected]>
Co-authored-by: andreyshspb <[email protected]>
Co-authored-by: dimaandreev <[email protected]>
Co-authored-by: rashid <[email protected]>
Co-authored-by: robot-ydb-importer <[email protected]>
Co-authored-by: r-vetrov <[email protected]>
Co-authored-by: ypodlesov <[email protected]>
Co-authored-by: zaverden <[email protected]>
Co-authored-by: vpozdyayev <[email protected]>
Co-authored-by: robot-cozmo <[email protected]>
Co-authored-by: v-korovin <[email protected]>
Co-authored-by: arikon <[email protected]>
Co-authored-by: khoden <[email protected]>
Co-authored-by: psydmm <[email protected]>
Co-authored-by: robot-javacom <[email protected]>
Co-authored-by: dtorilov <[email protected]>
Co-authored-by: sennikovmv <[email protected]>
Co-authored-by: hcpp <[email protected]>
Diffstat (limited to 'contrib/tools/python3/src/Modules/_sqlite')
24 files changed, 0 insertions, 9343 deletions
diff --git a/contrib/tools/python3/src/Modules/_sqlite/blob.c b/contrib/tools/python3/src/Modules/_sqlite/blob.c deleted file mode 100644 index 76d261baf00..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/blob.c +++ /dev/null @@ -1,617 +0,0 @@ -#include "blob.h" -#include "util.h" - -#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self))) -#include "clinic/blob.c.h" -#undef clinic_state - -/*[clinic input] -module _sqlite3 -class _sqlite3.Blob "pysqlite_Blob *" "clinic_state()->BlobType" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=908d3e16a45f8da7]*/ - -static void -close_blob(pysqlite_Blob *self) -{ - if (self->blob) { - sqlite3_blob *blob = self->blob; - self->blob = NULL; - - Py_BEGIN_ALLOW_THREADS - sqlite3_blob_close(blob); - Py_END_ALLOW_THREADS - } -} - -static int -blob_traverse(pysqlite_Blob *self, visitproc visit, void *arg) -{ - Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->connection); - return 0; -} - -static int -blob_clear(pysqlite_Blob *self) -{ - Py_CLEAR(self->connection); - return 0; -} - -static void -blob_dealloc(pysqlite_Blob *self) -{ - PyTypeObject *tp = Py_TYPE(self); - PyObject_GC_UnTrack(self); - - close_blob(self); - - if (self->in_weakreflist != NULL) { - PyObject_ClearWeakRefs((PyObject*)self); - } - tp->tp_clear((PyObject *)self); - tp->tp_free(self); - Py_DECREF(tp); -} - -// Return 1 if the blob object is usable, 0 if not. -static int -check_blob(pysqlite_Blob *self) -{ - if (!pysqlite_check_connection(self->connection) || - !pysqlite_check_thread(self->connection)) { - return 0; - } - if (self->blob == NULL) { - pysqlite_state *state = self->connection->state; - PyErr_SetString(state->ProgrammingError, - "Cannot operate on a closed blob."); - return 0; - } - return 1; -} - - -/*[clinic input] -_sqlite3.Blob.close as blob_close - -Close the blob. -[clinic start generated code]*/ - -static PyObject * -blob_close_impl(pysqlite_Blob *self) -/*[clinic end generated code: output=848accc20a138d1b input=7bc178a402a40bd8]*/ -{ - if (!pysqlite_check_connection(self->connection) || - !pysqlite_check_thread(self->connection)) - { - return NULL; - } - close_blob(self); - Py_RETURN_NONE; -}; - -void -pysqlite_close_all_blobs(pysqlite_Connection *self) -{ - for (int i = 0; i < PyList_GET_SIZE(self->blobs); i++) { - PyObject *weakref = PyList_GET_ITEM(self->blobs, i); - PyObject *blob = PyWeakref_GetObject(weakref); - if (!Py_IsNone(blob)) { - close_blob((pysqlite_Blob *)blob); - } - } -} - -static void -blob_seterror(pysqlite_Blob *self, int rc) -{ - assert(self->connection != NULL); -#if SQLITE_VERSION_NUMBER < 3008008 - // SQLite pre 3.8.8 does not set this blob error on the connection - if (rc == SQLITE_ABORT) { - PyErr_SetString(self->connection->OperationalError, - "Cannot operate on an expired blob handle"); - return; - } -#endif - _pysqlite_seterror(self->connection->state, self->connection->db); -} - -static PyObject * -read_single(pysqlite_Blob *self, Py_ssize_t offset) -{ - unsigned char buf = 0; - int rc; - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_blob_read(self->blob, (void *)&buf, 1, (int)offset); - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - blob_seterror(self, rc); - return NULL; - } - return PyLong_FromUnsignedLong((unsigned long)buf); -} - -static PyObject * -read_multiple(pysqlite_Blob *self, Py_ssize_t length, Py_ssize_t offset) -{ - assert(length <= sqlite3_blob_bytes(self->blob)); - assert(offset < sqlite3_blob_bytes(self->blob)); - - PyObject *buffer = PyBytes_FromStringAndSize(NULL, length); - if (buffer == NULL) { - return NULL; - } - - char *raw_buffer = PyBytes_AS_STRING(buffer); - int rc; - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_blob_read(self->blob, raw_buffer, (int)length, (int)offset); - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - Py_DECREF(buffer); - blob_seterror(self, rc); - return NULL; - } - return buffer; -} - - -/*[clinic input] -_sqlite3.Blob.read as blob_read - - length: int = -1 - Read length in bytes. - / - -Read data at the current offset position. - -If the end of the blob is reached, the data up to end of file will be returned. -When length is not specified, or is negative, Blob.read() will read until the -end of the blob. -[clinic start generated code]*/ - -static PyObject * -blob_read_impl(pysqlite_Blob *self, int length) -/*[clinic end generated code: output=1fc99b2541360dde input=f2e4aa4378837250]*/ -{ - if (!check_blob(self)) { - return NULL; - } - - /* Make sure we never read past "EOB". Also read the rest of the blob if a - * negative length is specified. */ - int blob_len = sqlite3_blob_bytes(self->blob); - int max_read_len = blob_len - self->offset; - if (length < 0 || length > max_read_len) { - length = max_read_len; - } - - assert(length >= 0); - if (length == 0) { - return PyBytes_FromStringAndSize(NULL, 0); - } - - PyObject *buffer = read_multiple(self, length, self->offset); - if (buffer == NULL) { - return NULL; - } - self->offset += length; - return buffer; -}; - -static int -inner_write(pysqlite_Blob *self, const void *buf, Py_ssize_t len, - Py_ssize_t offset) -{ - Py_ssize_t blob_len = sqlite3_blob_bytes(self->blob); - Py_ssize_t remaining_len = blob_len - offset; - if (len > remaining_len) { - PyErr_SetString(PyExc_ValueError, "data longer than blob length"); - return -1; - } - - assert(offset <= blob_len); - int rc; - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_blob_write(self->blob, buf, (int)len, (int)offset); - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - blob_seterror(self, rc); - return -1; - } - return 0; -} - - -/*[clinic input] -_sqlite3.Blob.write as blob_write - - data: Py_buffer - / - -Write data at the current offset. - -This function cannot change the blob length. Writing beyond the end of the -blob will result in an exception being raised. -[clinic start generated code]*/ - -static PyObject * -blob_write_impl(pysqlite_Blob *self, Py_buffer *data) -/*[clinic end generated code: output=b34cf22601b570b2 input=a84712f24a028e6d]*/ -{ - if (!check_blob(self)) { - return NULL; - } - - int rc = inner_write(self, data->buf, data->len, self->offset); - if (rc < 0) { - return NULL; - } - self->offset += (int)data->len; - Py_RETURN_NONE; -} - - -/*[clinic input] -_sqlite3.Blob.seek as blob_seek - - offset: int - origin: int = 0 - / - -Set the current access position to offset. - -The origin argument defaults to os.SEEK_SET (absolute blob positioning). -Other values for origin are os.SEEK_CUR (seek relative to the current position) -and os.SEEK_END (seek relative to the blob's end). -[clinic start generated code]*/ - -static PyObject * -blob_seek_impl(pysqlite_Blob *self, int offset, int origin) -/*[clinic end generated code: output=854c5a0e208547a5 input=5da9a07e55fe6bb6]*/ -{ - if (!check_blob(self)) { - return NULL; - } - - int blob_len = sqlite3_blob_bytes(self->blob); - switch (origin) { - case SEEK_SET: - break; - case SEEK_CUR: - if (offset > INT_MAX - self->offset) { - goto overflow; - } - offset += self->offset; - break; - case SEEK_END: - if (offset > INT_MAX - blob_len) { - goto overflow; - } - offset += blob_len; - break; - default: - PyErr_SetString(PyExc_ValueError, - "'origin' should be os.SEEK_SET, os.SEEK_CUR, or " - "os.SEEK_END"); - return NULL; - } - - if (offset < 0 || offset > blob_len) { - PyErr_SetString(PyExc_ValueError, "offset out of blob range"); - return NULL; - } - - self->offset = offset; - Py_RETURN_NONE; - -overflow: - PyErr_SetString(PyExc_OverflowError, "seek offset results in overflow"); - return NULL; -} - - -/*[clinic input] -_sqlite3.Blob.tell as blob_tell - -Return the current access position for the blob. -[clinic start generated code]*/ - -static PyObject * -blob_tell_impl(pysqlite_Blob *self) -/*[clinic end generated code: output=3d3ba484a90b3a99 input=7e34057aa303612c]*/ -{ - if (!check_blob(self)) { - return NULL; - } - return PyLong_FromLong(self->offset); -} - - -/*[clinic input] -_sqlite3.Blob.__enter__ as blob_enter - -Blob context manager enter. -[clinic start generated code]*/ - -static PyObject * -blob_enter_impl(pysqlite_Blob *self) -/*[clinic end generated code: output=4fd32484b071a6cd input=fe4842c3c582d5a7]*/ -{ - if (!check_blob(self)) { - return NULL; - } - return Py_NewRef(self); -} - - -/*[clinic input] -_sqlite3.Blob.__exit__ as blob_exit - - type: object - val: object - tb: object - / - -Blob context manager exit. -[clinic start generated code]*/ - -static PyObject * -blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val, - PyObject *tb) -/*[clinic end generated code: output=fc86ceeb2b68c7b2 input=575d9ecea205f35f]*/ -{ - if (!check_blob(self)) { - return NULL; - } - close_blob(self); - Py_RETURN_FALSE; -} - -static Py_ssize_t -blob_length(pysqlite_Blob *self) -{ - if (!check_blob(self)) { - return -1; - } - return sqlite3_blob_bytes(self->blob); -}; - -static Py_ssize_t -get_subscript_index(pysqlite_Blob *self, PyObject *item) -{ - Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); - if (i == -1 && PyErr_Occurred()) { - return -1; - } - int blob_len = sqlite3_blob_bytes(self->blob); - if (i < 0) { - i += blob_len; - } - if (i < 0 || i >= blob_len) { - PyErr_SetString(PyExc_IndexError, "Blob index out of range"); - return -1; - } - return i; -} - -static PyObject * -subscript_index(pysqlite_Blob *self, PyObject *item) -{ - Py_ssize_t i = get_subscript_index(self, item); - if (i < 0) { - return NULL; - } - return read_single(self, i); -} - -static int -get_slice_info(pysqlite_Blob *self, PyObject *item, Py_ssize_t *start, - Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelen) -{ - if (PySlice_Unpack(item, start, stop, step) < 0) { - return -1; - } - int len = sqlite3_blob_bytes(self->blob); - *slicelen = PySlice_AdjustIndices(len, start, stop, *step); - return 0; -} - -static PyObject * -subscript_slice(pysqlite_Blob *self, PyObject *item) -{ - Py_ssize_t start, stop, step, len; - if (get_slice_info(self, item, &start, &stop, &step, &len) < 0) { - return NULL; - } - - if (step == 1) { - return read_multiple(self, len, start); - } - PyObject *blob = read_multiple(self, stop - start, start); - if (blob == NULL) { - return NULL; - } - PyObject *result = PyBytes_FromStringAndSize(NULL, len); - if (result != NULL) { - char *blob_buf = PyBytes_AS_STRING(blob); - char *res_buf = PyBytes_AS_STRING(result); - for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) { - res_buf[i] = blob_buf[j]; - } - Py_DECREF(blob); - } - return result; -} - -static PyObject * -blob_subscript(pysqlite_Blob *self, PyObject *item) -{ - if (!check_blob(self)) { - return NULL; - } - - if (PyIndex_Check(item)) { - return subscript_index(self, item); - } - if (PySlice_Check(item)) { - return subscript_slice(self, item); - } - - PyErr_SetString(PyExc_TypeError, "Blob indices must be integers"); - return NULL; -} - -static int -ass_subscript_index(pysqlite_Blob *self, PyObject *item, PyObject *value) -{ - if (value == NULL) { - PyErr_SetString(PyExc_TypeError, - "Blob doesn't support item deletion"); - return -1; - } - if (!PyLong_Check(value)) { - PyErr_Format(PyExc_TypeError, - "'%s' object cannot be interpreted as an integer", - Py_TYPE(value)->tp_name); - return -1; - } - Py_ssize_t i = get_subscript_index(self, item); - if (i < 0) { - return -1; - } - - long val = PyLong_AsLong(value); - if (val == -1 && PyErr_Occurred()) { - PyErr_Clear(); - val = -1; - } - if (val < 0 || val > 255) { - PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); - return -1; - } - // Downcast to avoid endianness problems. - unsigned char byte = (unsigned char)val; - return inner_write(self, (const void *)&byte, 1, i); -} - -static int -ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value) -{ - if (value == NULL) { - PyErr_SetString(PyExc_TypeError, - "Blob doesn't support slice deletion"); - return -1; - } - - Py_ssize_t start, stop, step, len; - if (get_slice_info(self, item, &start, &stop, &step, &len) < 0) { - return -1; - } - - if (len == 0) { - return 0; - } - - Py_buffer vbuf; - if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) { - return -1; - } - - int rc = -1; - if (vbuf.len != len) { - PyErr_SetString(PyExc_IndexError, - "Blob slice assignment is wrong size"); - } - else if (step == 1) { - rc = inner_write(self, vbuf.buf, len, start); - } - else { - PyObject *blob_bytes = read_multiple(self, stop - start, start); - if (blob_bytes != NULL) { - char *blob_buf = PyBytes_AS_STRING(blob_bytes); - for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) { - blob_buf[j] = ((char *)vbuf.buf)[i]; - } - rc = inner_write(self, blob_buf, stop - start, start); - Py_DECREF(blob_bytes); - } - } - PyBuffer_Release(&vbuf); - return rc; -} - -static int -blob_ass_subscript(pysqlite_Blob *self, PyObject *item, PyObject *value) -{ - if (!check_blob(self)) { - return -1; - } - - if (PyIndex_Check(item)) { - return ass_subscript_index(self, item, value); - } - if (PySlice_Check(item)) { - return ass_subscript_slice(self, item, value); - } - - PyErr_SetString(PyExc_TypeError, "Blob indices must be integers"); - return -1; -} - - -static PyMethodDef blob_methods[] = { - BLOB_CLOSE_METHODDEF - BLOB_ENTER_METHODDEF - BLOB_EXIT_METHODDEF - BLOB_READ_METHODDEF - BLOB_SEEK_METHODDEF - BLOB_TELL_METHODDEF - BLOB_WRITE_METHODDEF - {NULL, NULL} -}; - -static struct PyMemberDef blob_members[] = { - {"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Blob, in_weakreflist), READONLY}, - {NULL}, -}; - -static PyType_Slot blob_slots[] = { - {Py_tp_dealloc, blob_dealloc}, - {Py_tp_traverse, blob_traverse}, - {Py_tp_clear, blob_clear}, - {Py_tp_methods, blob_methods}, - {Py_tp_members, blob_members}, - - // Mapping protocol - {Py_mp_length, blob_length}, - {Py_mp_subscript, blob_subscript}, - {Py_mp_ass_subscript, blob_ass_subscript}, - {0, NULL}, -}; - -static PyType_Spec blob_spec = { - .name = MODULE_NAME ".Blob", - .basicsize = sizeof(pysqlite_Blob), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), - .slots = blob_slots, -}; - -int -pysqlite_blob_setup_types(PyObject *mod) -{ - PyObject *type = PyType_FromModuleAndSpec(mod, &blob_spec, NULL); - if (type == NULL) { - return -1; - } - pysqlite_state *state = pysqlite_get_state(mod); - state->BlobType = (PyTypeObject *)type; - return 0; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/blob.h b/contrib/tools/python3/src/Modules/_sqlite/blob.h deleted file mode 100644 index 418ca03bdb5..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/blob.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef PYSQLITE_BLOB_H -#define PYSQLITE_BLOB_H - -#include "Python.h" -#include "sqlite3.h" -#include "connection.h" - -#define BLOB_SEEK_START 0 -#define BLOB_SEEK_CUR 1 -#define BLOB_SEEK_END 2 - -typedef struct { - PyObject_HEAD - pysqlite_Connection *connection; - sqlite3_blob *blob; - int offset; - - PyObject *in_weakreflist; -} pysqlite_Blob; - -int pysqlite_blob_setup_types(PyObject *mod); -void pysqlite_close_all_blobs(pysqlite_Connection *self); - -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/clinic/blob.c.h b/contrib/tools/python3/src/Modules/_sqlite/clinic/blob.c.h deleted file mode 100644 index f3d8a35be46..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/clinic/blob.c.h +++ /dev/null @@ -1,222 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) -# include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() -#endif - - -PyDoc_STRVAR(blob_close__doc__, -"close($self, /)\n" -"--\n" -"\n" -"Close the blob."); - -#define BLOB_CLOSE_METHODDEF \ - {"close", (PyCFunction)blob_close, METH_NOARGS, blob_close__doc__}, - -static PyObject * -blob_close_impl(pysqlite_Blob *self); - -static PyObject * -blob_close(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored)) -{ - return blob_close_impl(self); -} - -PyDoc_STRVAR(blob_read__doc__, -"read($self, length=-1, /)\n" -"--\n" -"\n" -"Read data at the current offset position.\n" -"\n" -" length\n" -" Read length in bytes.\n" -"\n" -"If the end of the blob is reached, the data up to end of file will be returned.\n" -"When length is not specified, or is negative, Blob.read() will read until the\n" -"end of the blob."); - -#define BLOB_READ_METHODDEF \ - {"read", _PyCFunction_CAST(blob_read), METH_FASTCALL, blob_read__doc__}, - -static PyObject * -blob_read_impl(pysqlite_Blob *self, int length); - -static PyObject * -blob_read(pysqlite_Blob *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - int length = -1; - - if (!_PyArg_CheckPositional("read", nargs, 0, 1)) { - goto exit; - } - if (nargs < 1) { - goto skip_optional; - } - length = _PyLong_AsInt(args[0]); - if (length == -1 && PyErr_Occurred()) { - goto exit; - } -skip_optional: - return_value = blob_read_impl(self, length); - -exit: - return return_value; -} - -PyDoc_STRVAR(blob_write__doc__, -"write($self, data, /)\n" -"--\n" -"\n" -"Write data at the current offset.\n" -"\n" -"This function cannot change the blob length. Writing beyond the end of the\n" -"blob will result in an exception being raised."); - -#define BLOB_WRITE_METHODDEF \ - {"write", (PyCFunction)blob_write, METH_O, blob_write__doc__}, - -static PyObject * -blob_write_impl(pysqlite_Blob *self, Py_buffer *data); - -static PyObject * -blob_write(pysqlite_Blob *self, PyObject *arg) -{ - PyObject *return_value = NULL; - Py_buffer data = {NULL, NULL}; - - if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) { - goto exit; - } - if (!PyBuffer_IsContiguous(&data, 'C')) { - _PyArg_BadArgument("write", "argument", "contiguous buffer", arg); - goto exit; - } - return_value = blob_write_impl(self, &data); - -exit: - /* Cleanup for data */ - if (data.obj) { - PyBuffer_Release(&data); - } - - return return_value; -} - -PyDoc_STRVAR(blob_seek__doc__, -"seek($self, offset, origin=0, /)\n" -"--\n" -"\n" -"Set the current access position to offset.\n" -"\n" -"The origin argument defaults to os.SEEK_SET (absolute blob positioning).\n" -"Other values for origin are os.SEEK_CUR (seek relative to the current position)\n" -"and os.SEEK_END (seek relative to the blob\'s end)."); - -#define BLOB_SEEK_METHODDEF \ - {"seek", _PyCFunction_CAST(blob_seek), METH_FASTCALL, blob_seek__doc__}, - -static PyObject * -blob_seek_impl(pysqlite_Blob *self, int offset, int origin); - -static PyObject * -blob_seek(pysqlite_Blob *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - int offset; - int origin = 0; - - if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) { - goto exit; - } - offset = _PyLong_AsInt(args[0]); - if (offset == -1 && PyErr_Occurred()) { - goto exit; - } - if (nargs < 2) { - goto skip_optional; - } - origin = _PyLong_AsInt(args[1]); - if (origin == -1 && PyErr_Occurred()) { - goto exit; - } -skip_optional: - return_value = blob_seek_impl(self, offset, origin); - -exit: - return return_value; -} - -PyDoc_STRVAR(blob_tell__doc__, -"tell($self, /)\n" -"--\n" -"\n" -"Return the current access position for the blob."); - -#define BLOB_TELL_METHODDEF \ - {"tell", (PyCFunction)blob_tell, METH_NOARGS, blob_tell__doc__}, - -static PyObject * -blob_tell_impl(pysqlite_Blob *self); - -static PyObject * -blob_tell(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored)) -{ - return blob_tell_impl(self); -} - -PyDoc_STRVAR(blob_enter__doc__, -"__enter__($self, /)\n" -"--\n" -"\n" -"Blob context manager enter."); - -#define BLOB_ENTER_METHODDEF \ - {"__enter__", (PyCFunction)blob_enter, METH_NOARGS, blob_enter__doc__}, - -static PyObject * -blob_enter_impl(pysqlite_Blob *self); - -static PyObject * -blob_enter(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored)) -{ - return blob_enter_impl(self); -} - -PyDoc_STRVAR(blob_exit__doc__, -"__exit__($self, type, val, tb, /)\n" -"--\n" -"\n" -"Blob context manager exit."); - -#define BLOB_EXIT_METHODDEF \ - {"__exit__", _PyCFunction_CAST(blob_exit), METH_FASTCALL, blob_exit__doc__}, - -static PyObject * -blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val, - PyObject *tb); - -static PyObject * -blob_exit(pysqlite_Blob *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *type; - PyObject *val; - PyObject *tb; - - if (!_PyArg_CheckPositional("__exit__", nargs, 3, 3)) { - goto exit; - } - type = args[0]; - val = args[1]; - tb = args[2]; - return_value = blob_exit_impl(self, type, val, tb); - -exit: - return return_value; -} -/*[clinic end generated code: output=ad6a402f70e85977 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_sqlite/clinic/connection.c.h b/contrib/tools/python3/src/Modules/_sqlite/clinic/connection.c.h deleted file mode 100644 index 417abcc4626..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/clinic/connection.c.h +++ /dev/null @@ -1,1668 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) -# include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() -#endif - - -static int -pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, - double timeout, int detect_types, - const char *isolation_level, - int check_same_thread, PyObject *factory, - int cache_size, int uri, - enum autocommit_mode autocommit); - -static int -pysqlite_connection_init(PyObject *self, PyObject *args, PyObject *kwargs) -{ - int return_value = -1; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 9 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(database), &_Py_ID(timeout), &_Py_ID(detect_types), &_Py_ID(isolation_level), &_Py_ID(check_same_thread), &_Py_ID(factory), &_Py_ID(cached_statements), &_Py_ID(uri), &_Py_ID(autocommit), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", "uri", "autocommit", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "Connection", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[9]; - PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; - PyObject *database; - double timeout = 5.0; - int detect_types = 0; - const char *isolation_level = ""; - int check_same_thread = 1; - PyObject *factory = (PyObject*)clinic_state()->ConnectionType; - int cache_size = 128; - int uri = 0; - enum autocommit_mode autocommit = LEGACY_TRANSACTION_CONTROL; - - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 8, 0, argsbuf); - if (!fastargs) { - goto exit; - } - database = fastargs[0]; - if (!noptargs) { - goto skip_optional_pos; - } - if (fastargs[1]) { - if (PyFloat_CheckExact(fastargs[1])) { - timeout = PyFloat_AS_DOUBLE(fastargs[1]); - } - else - { - timeout = PyFloat_AsDouble(fastargs[1]); - if (timeout == -1.0 && PyErr_Occurred()) { - goto exit; - } - } - if (!--noptargs) { - goto skip_optional_pos; - } - } - if (fastargs[2]) { - detect_types = _PyLong_AsInt(fastargs[2]); - if (detect_types == -1 && PyErr_Occurred()) { - goto exit; - } - if (!--noptargs) { - goto skip_optional_pos; - } - } - if (fastargs[3]) { - if (!isolation_level_converter(fastargs[3], &isolation_level)) { - goto exit; - } - if (!--noptargs) { - goto skip_optional_pos; - } - } - if (fastargs[4]) { - check_same_thread = PyObject_IsTrue(fastargs[4]); - if (check_same_thread < 0) { - goto exit; - } - if (!--noptargs) { - goto skip_optional_pos; - } - } - if (fastargs[5]) { - factory = fastargs[5]; - if (!--noptargs) { - goto skip_optional_pos; - } - } - if (fastargs[6]) { - cache_size = _PyLong_AsInt(fastargs[6]); - if (cache_size == -1 && PyErr_Occurred()) { - goto exit; - } - if (!--noptargs) { - goto skip_optional_pos; - } - } - if (fastargs[7]) { - uri = PyObject_IsTrue(fastargs[7]); - if (uri < 0) { - goto exit; - } - if (!--noptargs) { - goto skip_optional_pos; - } - } -skip_optional_pos: - if (!noptargs) { - goto skip_optional_kwonly; - } - if (!autocommit_converter(fastargs[8], &autocommit)) { - goto exit; - } -skip_optional_kwonly: - return_value = pysqlite_connection_init_impl((pysqlite_Connection *)self, database, timeout, detect_types, isolation_level, check_same_thread, factory, cache_size, uri, autocommit); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_cursor__doc__, -"cursor($self, /, factory=<unrepresentable>)\n" -"--\n" -"\n" -"Return a cursor for the connection."); - -#define PYSQLITE_CONNECTION_CURSOR_METHODDEF \ - {"cursor", _PyCFunction_CAST(pysqlite_connection_cursor), METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_cursor__doc__}, - -static PyObject * -pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory); - -static PyObject * -pysqlite_connection_cursor(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(factory), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"factory", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "cursor", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; - PyObject *factory = NULL; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); - if (!args) { - goto exit; - } - if (!noptargs) { - goto skip_optional_pos; - } - factory = args[0]; -skip_optional_pos: - return_value = pysqlite_connection_cursor_impl(self, factory); - -exit: - return return_value; -} - -PyDoc_STRVAR(blobopen__doc__, -"blobopen($self, table, column, row, /, *, readonly=False, name=\'main\')\n" -"--\n" -"\n" -"Open and return a BLOB object.\n" -"\n" -" table\n" -" Table name.\n" -" column\n" -" Column name.\n" -" row\n" -" Row index.\n" -" readonly\n" -" Open the BLOB without write permissions.\n" -" name\n" -" Database name."); - -#define BLOBOPEN_METHODDEF \ - {"blobopen", _PyCFunction_CAST(blobopen), METH_FASTCALL|METH_KEYWORDS, blobopen__doc__}, - -static PyObject * -blobopen_impl(pysqlite_Connection *self, const char *table, const char *col, - sqlite3_int64 row, int readonly, const char *name); - -static PyObject * -blobopen(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 2 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(readonly), &_Py_ID(name), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"", "", "", "readonly", "name", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "blobopen", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[5]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3; - const char *table; - const char *col; - sqlite3_int64 row; - int readonly = 0; - const char *name = "main"; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("blobopen", "argument 1", "str", args[0]); - goto exit; - } - Py_ssize_t table_length; - table = PyUnicode_AsUTF8AndSize(args[0], &table_length); - if (table == NULL) { - goto exit; - } - if (strlen(table) != (size_t)table_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - if (!PyUnicode_Check(args[1])) { - _PyArg_BadArgument("blobopen", "argument 2", "str", args[1]); - goto exit; - } - Py_ssize_t col_length; - col = PyUnicode_AsUTF8AndSize(args[1], &col_length); - if (col == NULL) { - goto exit; - } - if (strlen(col) != (size_t)col_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - if (!sqlite3_int64_converter(args[2], &row)) { - goto exit; - } - if (!noptargs) { - goto skip_optional_kwonly; - } - if (args[3]) { - readonly = PyObject_IsTrue(args[3]); - if (readonly < 0) { - goto exit; - } - if (!--noptargs) { - goto skip_optional_kwonly; - } - } - if (!PyUnicode_Check(args[4])) { - _PyArg_BadArgument("blobopen", "argument 'name'", "str", args[4]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[4], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } -skip_optional_kwonly: - return_value = blobopen_impl(self, table, col, row, readonly, name); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_close__doc__, -"close($self, /)\n" -"--\n" -"\n" -"Close the database connection.\n" -"\n" -"Any pending transaction is not committed implicitly."); - -#define PYSQLITE_CONNECTION_CLOSE_METHODDEF \ - {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS, pysqlite_connection_close__doc__}, - -static PyObject * -pysqlite_connection_close_impl(pysqlite_Connection *self); - -static PyObject * -pysqlite_connection_close(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_connection_close_impl(self); -} - -PyDoc_STRVAR(pysqlite_connection_commit__doc__, -"commit($self, /)\n" -"--\n" -"\n" -"Commit any pending transaction to the database.\n" -"\n" -"If there is no open transaction, this method is a no-op."); - -#define PYSQLITE_CONNECTION_COMMIT_METHODDEF \ - {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS, pysqlite_connection_commit__doc__}, - -static PyObject * -pysqlite_connection_commit_impl(pysqlite_Connection *self); - -static PyObject * -pysqlite_connection_commit(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_connection_commit_impl(self); -} - -PyDoc_STRVAR(pysqlite_connection_rollback__doc__, -"rollback($self, /)\n" -"--\n" -"\n" -"Roll back to the start of any pending transaction.\n" -"\n" -"If there is no open transaction, this method is a no-op."); - -#define PYSQLITE_CONNECTION_ROLLBACK_METHODDEF \ - {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS, pysqlite_connection_rollback__doc__}, - -static PyObject * -pysqlite_connection_rollback_impl(pysqlite_Connection *self); - -static PyObject * -pysqlite_connection_rollback(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_connection_rollback_impl(self); -} - -PyDoc_STRVAR(pysqlite_connection_create_function__doc__, -"create_function($self, /, name, narg, func, *, deterministic=False)\n" -"--\n" -"\n" -"Creates a new function."); - -#define PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF \ - {"create_function", _PyCFunction_CAST(pysqlite_connection_create_function), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_create_function__doc__}, - -static PyObject * -pysqlite_connection_create_function_impl(pysqlite_Connection *self, - PyTypeObject *cls, const char *name, - int narg, PyObject *func, - int deterministic); - -static PyObject * -pysqlite_connection_create_function(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 4 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(name), &_Py_ID(narg), &_Py_ID(func), &_Py_ID(deterministic), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"name", "narg", "func", "deterministic", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "create_function", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[4]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3; - const char *name; - int narg; - PyObject *func; - int deterministic = 0; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("create_function", "argument 'name'", "str", args[0]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[0], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - narg = _PyLong_AsInt(args[1]); - if (narg == -1 && PyErr_Occurred()) { - goto exit; - } - func = args[2]; - if (!noptargs) { - goto skip_optional_kwonly; - } - deterministic = PyObject_IsTrue(args[3]); - if (deterministic < 0) { - goto exit; - } -skip_optional_kwonly: - return_value = pysqlite_connection_create_function_impl(self, cls, name, narg, func, deterministic); - -exit: - return return_value; -} - -#if defined(HAVE_WINDOW_FUNCTIONS) - -PyDoc_STRVAR(create_window_function__doc__, -"create_window_function($self, name, num_params, aggregate_class, /)\n" -"--\n" -"\n" -"Creates or redefines an aggregate window function. Non-standard.\n" -"\n" -" name\n" -" The name of the SQL aggregate window function to be created or\n" -" redefined.\n" -" num_params\n" -" The number of arguments the step and inverse methods takes.\n" -" aggregate_class\n" -" A class with step(), finalize(), value(), and inverse() methods.\n" -" Set to None to clear the window function."); - -#define CREATE_WINDOW_FUNCTION_METHODDEF \ - {"create_window_function", _PyCFunction_CAST(create_window_function), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, create_window_function__doc__}, - -static PyObject * -create_window_function_impl(pysqlite_Connection *self, PyTypeObject *cls, - const char *name, int num_params, - PyObject *aggregate_class); - -static PyObject * -create_window_function(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) - #else - # define KWTUPLE NULL - #endif - - static const char * const _keywords[] = {"", "", "", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "create_window_function", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[3]; - const char *name; - int num_params; - PyObject *aggregate_class; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("create_window_function", "argument 1", "str", args[0]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[0], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - num_params = _PyLong_AsInt(args[1]); - if (num_params == -1 && PyErr_Occurred()) { - goto exit; - } - aggregate_class = args[2]; - return_value = create_window_function_impl(self, cls, name, num_params, aggregate_class); - -exit: - return return_value; -} - -#endif /* defined(HAVE_WINDOW_FUNCTIONS) */ - -PyDoc_STRVAR(pysqlite_connection_create_aggregate__doc__, -"create_aggregate($self, /, name, n_arg, aggregate_class)\n" -"--\n" -"\n" -"Creates a new aggregate."); - -#define PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF \ - {"create_aggregate", _PyCFunction_CAST(pysqlite_connection_create_aggregate), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_create_aggregate__doc__}, - -static PyObject * -pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self, - PyTypeObject *cls, - const char *name, int n_arg, - PyObject *aggregate_class); - -static PyObject * -pysqlite_connection_create_aggregate(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 3 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(name), &_Py_ID(n_arg), &_Py_ID(aggregate_class), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"name", "n_arg", "aggregate_class", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "create_aggregate", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[3]; - const char *name; - int n_arg; - PyObject *aggregate_class; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("create_aggregate", "argument 'name'", "str", args[0]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[0], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - n_arg = _PyLong_AsInt(args[1]); - if (n_arg == -1 && PyErr_Occurred()) { - goto exit; - } - aggregate_class = args[2]; - return_value = pysqlite_connection_create_aggregate_impl(self, cls, name, n_arg, aggregate_class); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_set_authorizer__doc__, -"set_authorizer($self, /, authorizer_callback)\n" -"--\n" -"\n" -"Sets authorizer callback."); - -#define PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF \ - {"set_authorizer", _PyCFunction_CAST(pysqlite_connection_set_authorizer), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_set_authorizer__doc__}, - -static PyObject * -pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self, - PyTypeObject *cls, - PyObject *callable); - -static PyObject * -pysqlite_connection_set_authorizer(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(authorizer_callback), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"authorizer_callback", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "set_authorizer", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - PyObject *callable; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); - if (!args) { - goto exit; - } - callable = args[0]; - return_value = pysqlite_connection_set_authorizer_impl(self, cls, callable); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_set_progress_handler__doc__, -"set_progress_handler($self, /, progress_handler, n)\n" -"--\n" -"\n" -"Sets progress handler callback."); - -#define PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF \ - {"set_progress_handler", _PyCFunction_CAST(pysqlite_connection_set_progress_handler), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_set_progress_handler__doc__}, - -static PyObject * -pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self, - PyTypeObject *cls, - PyObject *callable, int n); - -static PyObject * -pysqlite_connection_set_progress_handler(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 2 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(progress_handler), &_Py_ID(n), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"progress_handler", "n", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "set_progress_handler", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[2]; - PyObject *callable; - int n; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); - if (!args) { - goto exit; - } - callable = args[0]; - n = _PyLong_AsInt(args[1]); - if (n == -1 && PyErr_Occurred()) { - goto exit; - } - return_value = pysqlite_connection_set_progress_handler_impl(self, cls, callable, n); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_set_trace_callback__doc__, -"set_trace_callback($self, /, trace_callback)\n" -"--\n" -"\n" -"Sets a trace callback called for each SQL statement (passed as unicode)."); - -#define PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF \ - {"set_trace_callback", _PyCFunction_CAST(pysqlite_connection_set_trace_callback), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_set_trace_callback__doc__}, - -static PyObject * -pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self, - PyTypeObject *cls, - PyObject *callable); - -static PyObject * -pysqlite_connection_set_trace_callback(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(trace_callback), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"trace_callback", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "set_trace_callback", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - PyObject *callable; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); - if (!args) { - goto exit; - } - callable = args[0]; - return_value = pysqlite_connection_set_trace_callback_impl(self, cls, callable); - -exit: - return return_value; -} - -#if defined(PY_SQLITE_ENABLE_LOAD_EXTENSION) - -PyDoc_STRVAR(pysqlite_connection_enable_load_extension__doc__, -"enable_load_extension($self, enable, /)\n" -"--\n" -"\n" -"Enable dynamic loading of SQLite extension modules."); - -#define PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF \ - {"enable_load_extension", (PyCFunction)pysqlite_connection_enable_load_extension, METH_O, pysqlite_connection_enable_load_extension__doc__}, - -static PyObject * -pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self, - int onoff); - -static PyObject * -pysqlite_connection_enable_load_extension(pysqlite_Connection *self, PyObject *arg) -{ - PyObject *return_value = NULL; - int onoff; - - onoff = PyObject_IsTrue(arg); - if (onoff < 0) { - goto exit; - } - return_value = pysqlite_connection_enable_load_extension_impl(self, onoff); - -exit: - return return_value; -} - -#endif /* defined(PY_SQLITE_ENABLE_LOAD_EXTENSION) */ - -#if defined(PY_SQLITE_ENABLE_LOAD_EXTENSION) - -PyDoc_STRVAR(pysqlite_connection_load_extension__doc__, -"load_extension($self, name, /, *, entrypoint=None)\n" -"--\n" -"\n" -"Load SQLite extension module."); - -#define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF \ - {"load_extension", _PyCFunction_CAST(pysqlite_connection_load_extension), METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_load_extension__doc__}, - -static PyObject * -pysqlite_connection_load_extension_impl(pysqlite_Connection *self, - const char *extension_name, - const char *entrypoint); - -static PyObject * -pysqlite_connection_load_extension(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(entrypoint), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"", "entrypoint", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "load_extension", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[2]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; - const char *extension_name; - const char *entrypoint = NULL; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("load_extension", "argument 1", "str", args[0]); - goto exit; - } - Py_ssize_t extension_name_length; - extension_name = PyUnicode_AsUTF8AndSize(args[0], &extension_name_length); - if (extension_name == NULL) { - goto exit; - } - if (strlen(extension_name) != (size_t)extension_name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - if (!noptargs) { - goto skip_optional_kwonly; - } - if (args[1] == Py_None) { - entrypoint = NULL; - } - else if (PyUnicode_Check(args[1])) { - Py_ssize_t entrypoint_length; - entrypoint = PyUnicode_AsUTF8AndSize(args[1], &entrypoint_length); - if (entrypoint == NULL) { - goto exit; - } - if (strlen(entrypoint) != (size_t)entrypoint_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - } - else { - _PyArg_BadArgument("load_extension", "argument 'entrypoint'", "str or None", args[1]); - goto exit; - } -skip_optional_kwonly: - return_value = pysqlite_connection_load_extension_impl(self, extension_name, entrypoint); - -exit: - return return_value; -} - -#endif /* defined(PY_SQLITE_ENABLE_LOAD_EXTENSION) */ - -PyDoc_STRVAR(pysqlite_connection_execute__doc__, -"execute($self, sql, parameters=<unrepresentable>, /)\n" -"--\n" -"\n" -"Executes an SQL statement."); - -#define PYSQLITE_CONNECTION_EXECUTE_METHODDEF \ - {"execute", _PyCFunction_CAST(pysqlite_connection_execute), METH_FASTCALL, pysqlite_connection_execute__doc__}, - -static PyObject * -pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql, - PyObject *parameters); - -static PyObject * -pysqlite_connection_execute(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *sql; - PyObject *parameters = NULL; - - if (!_PyArg_CheckPositional("execute", nargs, 1, 2)) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("execute", "argument 1", "str", args[0]); - goto exit; - } - if (PyUnicode_READY(args[0]) == -1) { - goto exit; - } - sql = args[0]; - if (nargs < 2) { - goto skip_optional; - } - parameters = args[1]; -skip_optional: - return_value = pysqlite_connection_execute_impl(self, sql, parameters); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_executemany__doc__, -"executemany($self, sql, parameters, /)\n" -"--\n" -"\n" -"Repeatedly executes an SQL statement."); - -#define PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF \ - {"executemany", _PyCFunction_CAST(pysqlite_connection_executemany), METH_FASTCALL, pysqlite_connection_executemany__doc__}, - -static PyObject * -pysqlite_connection_executemany_impl(pysqlite_Connection *self, - PyObject *sql, PyObject *parameters); - -static PyObject * -pysqlite_connection_executemany(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *sql; - PyObject *parameters; - - if (!_PyArg_CheckPositional("executemany", nargs, 2, 2)) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("executemany", "argument 1", "str", args[0]); - goto exit; - } - if (PyUnicode_READY(args[0]) == -1) { - goto exit; - } - sql = args[0]; - parameters = args[1]; - return_value = pysqlite_connection_executemany_impl(self, sql, parameters); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_executescript__doc__, -"executescript($self, sql_script, /)\n" -"--\n" -"\n" -"Executes multiple SQL statements at once."); - -#define PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF \ - {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_O, pysqlite_connection_executescript__doc__}, - -PyDoc_STRVAR(pysqlite_connection_interrupt__doc__, -"interrupt($self, /)\n" -"--\n" -"\n" -"Abort any pending database operation."); - -#define PYSQLITE_CONNECTION_INTERRUPT_METHODDEF \ - {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS, pysqlite_connection_interrupt__doc__}, - -static PyObject * -pysqlite_connection_interrupt_impl(pysqlite_Connection *self); - -static PyObject * -pysqlite_connection_interrupt(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_connection_interrupt_impl(self); -} - -PyDoc_STRVAR(pysqlite_connection_iterdump__doc__, -"iterdump($self, /)\n" -"--\n" -"\n" -"Returns iterator to the dump of the database in an SQL text format."); - -#define PYSQLITE_CONNECTION_ITERDUMP_METHODDEF \ - {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS, pysqlite_connection_iterdump__doc__}, - -static PyObject * -pysqlite_connection_iterdump_impl(pysqlite_Connection *self); - -static PyObject * -pysqlite_connection_iterdump(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_connection_iterdump_impl(self); -} - -PyDoc_STRVAR(pysqlite_connection_backup__doc__, -"backup($self, /, target, *, pages=-1, progress=None, name=\'main\',\n" -" sleep=0.25)\n" -"--\n" -"\n" -"Makes a backup of the database."); - -#define PYSQLITE_CONNECTION_BACKUP_METHODDEF \ - {"backup", _PyCFunction_CAST(pysqlite_connection_backup), METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_backup__doc__}, - -static PyObject * -pysqlite_connection_backup_impl(pysqlite_Connection *self, - pysqlite_Connection *target, int pages, - PyObject *progress, const char *name, - double sleep); - -static PyObject * -pysqlite_connection_backup(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 5 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(target), &_Py_ID(pages), &_Py_ID(progress), &_Py_ID(name), &_Py_ID(sleep), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"target", "pages", "progress", "name", "sleep", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "backup", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[5]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; - pysqlite_Connection *target; - int pages = -1; - PyObject *progress = Py_None; - const char *name = "main"; - double sleep = 0.25; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyObject_TypeCheck(args[0], clinic_state()->ConnectionType)) { - _PyArg_BadArgument("backup", "argument 'target'", (clinic_state()->ConnectionType)->tp_name, args[0]); - goto exit; - } - target = (pysqlite_Connection *)args[0]; - if (!noptargs) { - goto skip_optional_kwonly; - } - if (args[1]) { - pages = _PyLong_AsInt(args[1]); - if (pages == -1 && PyErr_Occurred()) { - goto exit; - } - if (!--noptargs) { - goto skip_optional_kwonly; - } - } - if (args[2]) { - progress = args[2]; - if (!--noptargs) { - goto skip_optional_kwonly; - } - } - if (args[3]) { - if (!PyUnicode_Check(args[3])) { - _PyArg_BadArgument("backup", "argument 'name'", "str", args[3]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[3], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - if (!--noptargs) { - goto skip_optional_kwonly; - } - } - if (PyFloat_CheckExact(args[4])) { - sleep = PyFloat_AS_DOUBLE(args[4]); - } - else - { - sleep = PyFloat_AsDouble(args[4]); - if (sleep == -1.0 && PyErr_Occurred()) { - goto exit; - } - } -skip_optional_kwonly: - return_value = pysqlite_connection_backup_impl(self, target, pages, progress, name, sleep); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_connection_create_collation__doc__, -"create_collation($self, name, callback, /)\n" -"--\n" -"\n" -"Creates a collation function."); - -#define PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF \ - {"create_collation", _PyCFunction_CAST(pysqlite_connection_create_collation), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_connection_create_collation__doc__}, - -static PyObject * -pysqlite_connection_create_collation_impl(pysqlite_Connection *self, - PyTypeObject *cls, - const char *name, - PyObject *callable); - -static PyObject * -pysqlite_connection_create_collation(pysqlite_Connection *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty) - #else - # define KWTUPLE NULL - #endif - - static const char * const _keywords[] = {"", "", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "create_collation", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[2]; - const char *name; - PyObject *callable; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("create_collation", "argument 1", "str", args[0]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[0], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - callable = args[1]; - return_value = pysqlite_connection_create_collation_impl(self, cls, name, callable); - -exit: - return return_value; -} - -#if defined(PY_SQLITE_HAVE_SERIALIZE) - -PyDoc_STRVAR(serialize__doc__, -"serialize($self, /, *, name=\'main\')\n" -"--\n" -"\n" -"Serialize a database into a byte string.\n" -"\n" -" name\n" -" Which database to serialize.\n" -"\n" -"For an ordinary on-disk database file, the serialization is just a copy of the\n" -"disk file. For an in-memory database or a \"temp\" database, the serialization is\n" -"the same sequence of bytes which would be written to disk if that database\n" -"were backed up to disk."); - -#define SERIALIZE_METHODDEF \ - {"serialize", _PyCFunction_CAST(serialize), METH_FASTCALL|METH_KEYWORDS, serialize__doc__}, - -static PyObject * -serialize_impl(pysqlite_Connection *self, const char *name); - -static PyObject * -serialize(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(name), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"name", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "serialize", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; - const char *name = "main"; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf); - if (!args) { - goto exit; - } - if (!noptargs) { - goto skip_optional_kwonly; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("serialize", "argument 'name'", "str", args[0]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[0], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } -skip_optional_kwonly: - return_value = serialize_impl(self, name); - -exit: - return return_value; -} - -#endif /* defined(PY_SQLITE_HAVE_SERIALIZE) */ - -#if defined(PY_SQLITE_HAVE_SERIALIZE) - -PyDoc_STRVAR(deserialize__doc__, -"deserialize($self, data, /, *, name=\'main\')\n" -"--\n" -"\n" -"Load a serialized database.\n" -"\n" -" data\n" -" The serialized database content.\n" -" name\n" -" Which database to reopen with the deserialization.\n" -"\n" -"The deserialize interface causes the database connection to disconnect from the\n" -"target database, and then reopen it as an in-memory database based on the given\n" -"serialized data.\n" -"\n" -"The deserialize interface will fail with SQLITE_BUSY if the database is\n" -"currently in a read transaction or is involved in a backup operation."); - -#define DESERIALIZE_METHODDEF \ - {"deserialize", _PyCFunction_CAST(deserialize), METH_FASTCALL|METH_KEYWORDS, deserialize__doc__}, - -static PyObject * -deserialize_impl(pysqlite_Connection *self, Py_buffer *data, - const char *name); - -static PyObject * -deserialize(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(name), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"", "name", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "deserialize", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[2]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; - Py_buffer data = {NULL, NULL}; - const char *name = "main"; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); - if (!args) { - goto exit; - } - if (PyUnicode_Check(args[0])) { - Py_ssize_t len; - const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len); - if (ptr == NULL) { - goto exit; - } - PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0); - } - else { /* any bytes-like object */ - if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { - goto exit; - } - if (!PyBuffer_IsContiguous(&data, 'C')) { - _PyArg_BadArgument("deserialize", "argument 1", "contiguous buffer", args[0]); - goto exit; - } - } - if (!noptargs) { - goto skip_optional_kwonly; - } - if (!PyUnicode_Check(args[1])) { - _PyArg_BadArgument("deserialize", "argument 'name'", "str", args[1]); - goto exit; - } - Py_ssize_t name_length; - name = PyUnicode_AsUTF8AndSize(args[1], &name_length); - if (name == NULL) { - goto exit; - } - if (strlen(name) != (size_t)name_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } -skip_optional_kwonly: - return_value = deserialize_impl(self, &data, name); - -exit: - /* Cleanup for data */ - if (data.obj) { - PyBuffer_Release(&data); - } - - return return_value; -} - -#endif /* defined(PY_SQLITE_HAVE_SERIALIZE) */ - -PyDoc_STRVAR(pysqlite_connection_enter__doc__, -"__enter__($self, /)\n" -"--\n" -"\n" -"Called when the connection is used as a context manager.\n" -"\n" -"Returns itself as a convenience to the caller."); - -#define PYSQLITE_CONNECTION_ENTER_METHODDEF \ - {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS, pysqlite_connection_enter__doc__}, - -static PyObject * -pysqlite_connection_enter_impl(pysqlite_Connection *self); - -static PyObject * -pysqlite_connection_enter(pysqlite_Connection *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_connection_enter_impl(self); -} - -PyDoc_STRVAR(pysqlite_connection_exit__doc__, -"__exit__($self, type, value, traceback, /)\n" -"--\n" -"\n" -"Called when the connection is used as a context manager.\n" -"\n" -"If there was any exception, a rollback takes place; otherwise we commit."); - -#define PYSQLITE_CONNECTION_EXIT_METHODDEF \ - {"__exit__", _PyCFunction_CAST(pysqlite_connection_exit), METH_FASTCALL, pysqlite_connection_exit__doc__}, - -static PyObject * -pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type, - PyObject *exc_value, PyObject *exc_tb); - -static PyObject * -pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *exc_type; - PyObject *exc_value; - PyObject *exc_tb; - - if (!_PyArg_CheckPositional("__exit__", nargs, 3, 3)) { - goto exit; - } - exc_type = args[0]; - exc_value = args[1]; - exc_tb = args[2]; - return_value = pysqlite_connection_exit_impl(self, exc_type, exc_value, exc_tb); - -exit: - return return_value; -} - -PyDoc_STRVAR(setlimit__doc__, -"setlimit($self, category, limit, /)\n" -"--\n" -"\n" -"Set connection run-time limits.\n" -"\n" -" category\n" -" The limit category to be set.\n" -" limit\n" -" The new limit. If the new limit is a negative number, the limit is\n" -" unchanged.\n" -"\n" -"Attempts to increase a limit above its hard upper bound are silently truncated\n" -"to the hard upper bound. Regardless of whether or not the limit was changed,\n" -"the prior value of the limit is returned."); - -#define SETLIMIT_METHODDEF \ - {"setlimit", _PyCFunction_CAST(setlimit), METH_FASTCALL, setlimit__doc__}, - -static PyObject * -setlimit_impl(pysqlite_Connection *self, int category, int limit); - -static PyObject * -setlimit(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - int category; - int limit; - - if (!_PyArg_CheckPositional("setlimit", nargs, 2, 2)) { - goto exit; - } - category = _PyLong_AsInt(args[0]); - if (category == -1 && PyErr_Occurred()) { - goto exit; - } - limit = _PyLong_AsInt(args[1]); - if (limit == -1 && PyErr_Occurred()) { - goto exit; - } - return_value = setlimit_impl(self, category, limit); - -exit: - return return_value; -} - -PyDoc_STRVAR(getlimit__doc__, -"getlimit($self, category, /)\n" -"--\n" -"\n" -"Get connection run-time limits.\n" -"\n" -" category\n" -" The limit category to be queried."); - -#define GETLIMIT_METHODDEF \ - {"getlimit", (PyCFunction)getlimit, METH_O, getlimit__doc__}, - -static PyObject * -getlimit_impl(pysqlite_Connection *self, int category); - -static PyObject * -getlimit(pysqlite_Connection *self, PyObject *arg) -{ - PyObject *return_value = NULL; - int category; - - category = _PyLong_AsInt(arg); - if (category == -1 && PyErr_Occurred()) { - goto exit; - } - return_value = getlimit_impl(self, category); - -exit: - return return_value; -} - -PyDoc_STRVAR(setconfig__doc__, -"setconfig($self, op, enable=True, /)\n" -"--\n" -"\n" -"Set a boolean connection configuration option.\n" -"\n" -" op\n" -" The configuration verb; one of the sqlite3.SQLITE_DBCONFIG codes."); - -#define SETCONFIG_METHODDEF \ - {"setconfig", _PyCFunction_CAST(setconfig), METH_FASTCALL, setconfig__doc__}, - -static PyObject * -setconfig_impl(pysqlite_Connection *self, int op, int enable); - -static PyObject * -setconfig(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - int op; - int enable = 1; - - if (!_PyArg_CheckPositional("setconfig", nargs, 1, 2)) { - goto exit; - } - op = _PyLong_AsInt(args[0]); - if (op == -1 && PyErr_Occurred()) { - goto exit; - } - if (nargs < 2) { - goto skip_optional; - } - enable = PyObject_IsTrue(args[1]); - if (enable < 0) { - goto exit; - } -skip_optional: - return_value = setconfig_impl(self, op, enable); - -exit: - return return_value; -} - -PyDoc_STRVAR(getconfig__doc__, -"getconfig($self, op, /)\n" -"--\n" -"\n" -"Query a boolean connection configuration option.\n" -"\n" -" op\n" -" The configuration verb; one of the sqlite3.SQLITE_DBCONFIG codes."); - -#define GETCONFIG_METHODDEF \ - {"getconfig", (PyCFunction)getconfig, METH_O, getconfig__doc__}, - -static int -getconfig_impl(pysqlite_Connection *self, int op); - -static PyObject * -getconfig(pysqlite_Connection *self, PyObject *arg) -{ - PyObject *return_value = NULL; - int op; - int _return_value; - - op = _PyLong_AsInt(arg); - if (op == -1 && PyErr_Occurred()) { - goto exit; - } - _return_value = getconfig_impl(self, op); - if ((_return_value == -1) && PyErr_Occurred()) { - goto exit; - } - return_value = PyBool_FromLong((long)_return_value); - -exit: - return return_value; -} - -#ifndef CREATE_WINDOW_FUNCTION_METHODDEF - #define CREATE_WINDOW_FUNCTION_METHODDEF -#endif /* !defined(CREATE_WINDOW_FUNCTION_METHODDEF) */ - -#ifndef PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF - #define PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF -#endif /* !defined(PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF) */ - -#ifndef PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF - #define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF -#endif /* !defined(PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF) */ - -#ifndef SERIALIZE_METHODDEF - #define SERIALIZE_METHODDEF -#endif /* !defined(SERIALIZE_METHODDEF) */ - -#ifndef DESERIALIZE_METHODDEF - #define DESERIALIZE_METHODDEF -#endif /* !defined(DESERIALIZE_METHODDEF) */ -/*[clinic end generated code: output=834a99827555bf1a input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_sqlite/clinic/cursor.c.h b/contrib/tools/python3/src/Modules/_sqlite/clinic/cursor.c.h deleted file mode 100644 index 43e912d1347..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/clinic/cursor.c.h +++ /dev/null @@ -1,322 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) -# include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() -#endif - - -static int -pysqlite_cursor_init_impl(pysqlite_Cursor *self, - pysqlite_Connection *connection); - -static int -pysqlite_cursor_init(PyObject *self, PyObject *args, PyObject *kwargs) -{ - int return_value = -1; - PyTypeObject *base_tp = clinic_state()->CursorType; - pysqlite_Connection *connection; - - if ((Py_IS_TYPE(self, base_tp) || - Py_TYPE(self)->tp_new == base_tp->tp_new) && - !_PyArg_NoKeywords("Cursor", kwargs)) { - goto exit; - } - if (!_PyArg_CheckPositional("Cursor", PyTuple_GET_SIZE(args), 1, 1)) { - goto exit; - } - if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), clinic_state()->ConnectionType)) { - _PyArg_BadArgument("Cursor", "argument 1", (clinic_state()->ConnectionType)->tp_name, PyTuple_GET_ITEM(args, 0)); - goto exit; - } - connection = (pysqlite_Connection *)PyTuple_GET_ITEM(args, 0); - return_value = pysqlite_cursor_init_impl((pysqlite_Cursor *)self, connection); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_cursor_execute__doc__, -"execute($self, sql, parameters=(), /)\n" -"--\n" -"\n" -"Executes an SQL statement."); - -#define PYSQLITE_CURSOR_EXECUTE_METHODDEF \ - {"execute", _PyCFunction_CAST(pysqlite_cursor_execute), METH_FASTCALL, pysqlite_cursor_execute__doc__}, - -static PyObject * -pysqlite_cursor_execute_impl(pysqlite_Cursor *self, PyObject *sql, - PyObject *parameters); - -static PyObject * -pysqlite_cursor_execute(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *sql; - PyObject *parameters = NULL; - - if (!_PyArg_CheckPositional("execute", nargs, 1, 2)) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("execute", "argument 1", "str", args[0]); - goto exit; - } - if (PyUnicode_READY(args[0]) == -1) { - goto exit; - } - sql = args[0]; - if (nargs < 2) { - goto skip_optional; - } - parameters = args[1]; -skip_optional: - return_value = pysqlite_cursor_execute_impl(self, sql, parameters); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_cursor_executemany__doc__, -"executemany($self, sql, seq_of_parameters, /)\n" -"--\n" -"\n" -"Repeatedly executes an SQL statement."); - -#define PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF \ - {"executemany", _PyCFunction_CAST(pysqlite_cursor_executemany), METH_FASTCALL, pysqlite_cursor_executemany__doc__}, - -static PyObject * -pysqlite_cursor_executemany_impl(pysqlite_Cursor *self, PyObject *sql, - PyObject *seq_of_parameters); - -static PyObject * -pysqlite_cursor_executemany(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *sql; - PyObject *seq_of_parameters; - - if (!_PyArg_CheckPositional("executemany", nargs, 2, 2)) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("executemany", "argument 1", "str", args[0]); - goto exit; - } - if (PyUnicode_READY(args[0]) == -1) { - goto exit; - } - sql = args[0]; - seq_of_parameters = args[1]; - return_value = pysqlite_cursor_executemany_impl(self, sql, seq_of_parameters); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_cursor_executescript__doc__, -"executescript($self, sql_script, /)\n" -"--\n" -"\n" -"Executes multiple SQL statements at once."); - -#define PYSQLITE_CURSOR_EXECUTESCRIPT_METHODDEF \ - {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_O, pysqlite_cursor_executescript__doc__}, - -static PyObject * -pysqlite_cursor_executescript_impl(pysqlite_Cursor *self, - const char *sql_script); - -static PyObject * -pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *arg) -{ - PyObject *return_value = NULL; - const char *sql_script; - - if (!PyUnicode_Check(arg)) { - _PyArg_BadArgument("executescript", "argument", "str", arg); - goto exit; - } - Py_ssize_t sql_script_length; - sql_script = PyUnicode_AsUTF8AndSize(arg, &sql_script_length); - if (sql_script == NULL) { - goto exit; - } - if (strlen(sql_script) != (size_t)sql_script_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - return_value = pysqlite_cursor_executescript_impl(self, sql_script); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_cursor_fetchone__doc__, -"fetchone($self, /)\n" -"--\n" -"\n" -"Fetches one row from the resultset."); - -#define PYSQLITE_CURSOR_FETCHONE_METHODDEF \ - {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, pysqlite_cursor_fetchone__doc__}, - -static PyObject * -pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self); - -static PyObject * -pysqlite_cursor_fetchone(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_cursor_fetchone_impl(self); -} - -PyDoc_STRVAR(pysqlite_cursor_fetchmany__doc__, -"fetchmany($self, /, size=1)\n" -"--\n" -"\n" -"Fetches several rows from the resultset.\n" -"\n" -" size\n" -" The default value is set by the Cursor.arraysize attribute."); - -#define PYSQLITE_CURSOR_FETCHMANY_METHODDEF \ - {"fetchmany", _PyCFunction_CAST(pysqlite_cursor_fetchmany), METH_FASTCALL|METH_KEYWORDS, pysqlite_cursor_fetchmany__doc__}, - -static PyObject * -pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows); - -static PyObject * -pysqlite_cursor_fetchmany(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(size), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"size", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "fetchmany", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; - int maxrows = self->arraysize; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); - if (!args) { - goto exit; - } - if (!noptargs) { - goto skip_optional_pos; - } - maxrows = _PyLong_AsInt(args[0]); - if (maxrows == -1 && PyErr_Occurred()) { - goto exit; - } -skip_optional_pos: - return_value = pysqlite_cursor_fetchmany_impl(self, maxrows); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_cursor_fetchall__doc__, -"fetchall($self, /)\n" -"--\n" -"\n" -"Fetches all rows from the resultset."); - -#define PYSQLITE_CURSOR_FETCHALL_METHODDEF \ - {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, pysqlite_cursor_fetchall__doc__}, - -static PyObject * -pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self); - -static PyObject * -pysqlite_cursor_fetchall(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_cursor_fetchall_impl(self); -} - -PyDoc_STRVAR(pysqlite_cursor_setinputsizes__doc__, -"setinputsizes($self, sizes, /)\n" -"--\n" -"\n" -"Required by DB-API. Does nothing in sqlite3."); - -#define PYSQLITE_CURSOR_SETINPUTSIZES_METHODDEF \ - {"setinputsizes", (PyCFunction)pysqlite_cursor_setinputsizes, METH_O, pysqlite_cursor_setinputsizes__doc__}, - -PyDoc_STRVAR(pysqlite_cursor_setoutputsize__doc__, -"setoutputsize($self, size, column=None, /)\n" -"--\n" -"\n" -"Required by DB-API. Does nothing in sqlite3."); - -#define PYSQLITE_CURSOR_SETOUTPUTSIZE_METHODDEF \ - {"setoutputsize", _PyCFunction_CAST(pysqlite_cursor_setoutputsize), METH_FASTCALL, pysqlite_cursor_setoutputsize__doc__}, - -static PyObject * -pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size, - PyObject *column); - -static PyObject * -pysqlite_cursor_setoutputsize(pysqlite_Cursor *self, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *size; - PyObject *column = Py_None; - - if (!_PyArg_CheckPositional("setoutputsize", nargs, 1, 2)) { - goto exit; - } - size = args[0]; - if (nargs < 2) { - goto skip_optional; - } - column = args[1]; -skip_optional: - return_value = pysqlite_cursor_setoutputsize_impl(self, size, column); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_cursor_close__doc__, -"close($self, /)\n" -"--\n" -"\n" -"Closes the cursor."); - -#define PYSQLITE_CURSOR_CLOSE_METHODDEF \ - {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, pysqlite_cursor_close__doc__}, - -static PyObject * -pysqlite_cursor_close_impl(pysqlite_Cursor *self); - -static PyObject * -pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_cursor_close_impl(self); -} -/*[clinic end generated code: output=1f82e3c9791bb9a5 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_sqlite/clinic/module.c.h b/contrib/tools/python3/src/Modules/_sqlite/clinic/module.c.h deleted file mode 100644 index 12f60835880..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/clinic/module.c.h +++ /dev/null @@ -1,214 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) -# include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() -#endif - - -PyDoc_STRVAR(pysqlite_complete_statement__doc__, -"complete_statement($module, /, statement)\n" -"--\n" -"\n" -"Checks if a string contains a complete SQL statement."); - -#define PYSQLITE_COMPLETE_STATEMENT_METHODDEF \ - {"complete_statement", _PyCFunction_CAST(pysqlite_complete_statement), METH_FASTCALL|METH_KEYWORDS, pysqlite_complete_statement__doc__}, - -static PyObject * -pysqlite_complete_statement_impl(PyObject *module, const char *statement); - -static PyObject * -pysqlite_complete_statement(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) -{ - PyObject *return_value = NULL; - #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) - - #define NUM_KEYWORDS 1 - static struct { - PyGC_Head _this_is_not_used; - PyObject_VAR_HEAD - PyObject *ob_item[NUM_KEYWORDS]; - } _kwtuple = { - .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) - .ob_item = { &_Py_ID(statement), }, - }; - #undef NUM_KEYWORDS - #define KWTUPLE (&_kwtuple.ob_base.ob_base) - - #else // !Py_BUILD_CORE - # define KWTUPLE NULL - #endif // !Py_BUILD_CORE - - static const char * const _keywords[] = {"statement", NULL}; - static _PyArg_Parser _parser = { - .keywords = _keywords, - .fname = "complete_statement", - .kwtuple = KWTUPLE, - }; - #undef KWTUPLE - PyObject *argsbuf[1]; - const char *statement; - - args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); - if (!args) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("complete_statement", "argument 'statement'", "str", args[0]); - goto exit; - } - Py_ssize_t statement_length; - statement = PyUnicode_AsUTF8AndSize(args[0], &statement_length); - if (statement == NULL) { - goto exit; - } - if (strlen(statement) != (size_t)statement_length) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - goto exit; - } - return_value = pysqlite_complete_statement_impl(module, statement); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_register_adapter__doc__, -"register_adapter($module, type, adapter, /)\n" -"--\n" -"\n" -"Register a function to adapt Python objects to SQLite values."); - -#define PYSQLITE_REGISTER_ADAPTER_METHODDEF \ - {"register_adapter", _PyCFunction_CAST(pysqlite_register_adapter), METH_FASTCALL, pysqlite_register_adapter__doc__}, - -static PyObject * -pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type, - PyObject *caster); - -static PyObject * -pysqlite_register_adapter(PyObject *module, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyTypeObject *type; - PyObject *caster; - - if (!_PyArg_CheckPositional("register_adapter", nargs, 2, 2)) { - goto exit; - } - type = (PyTypeObject *)args[0]; - caster = args[1]; - return_value = pysqlite_register_adapter_impl(module, type, caster); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_register_converter__doc__, -"register_converter($module, typename, converter, /)\n" -"--\n" -"\n" -"Register a function to convert SQLite values to Python objects."); - -#define PYSQLITE_REGISTER_CONVERTER_METHODDEF \ - {"register_converter", _PyCFunction_CAST(pysqlite_register_converter), METH_FASTCALL, pysqlite_register_converter__doc__}, - -static PyObject * -pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name, - PyObject *callable); - -static PyObject * -pysqlite_register_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *orig_name; - PyObject *callable; - - if (!_PyArg_CheckPositional("register_converter", nargs, 2, 2)) { - goto exit; - } - if (!PyUnicode_Check(args[0])) { - _PyArg_BadArgument("register_converter", "argument 1", "str", args[0]); - goto exit; - } - if (PyUnicode_READY(args[0]) == -1) { - goto exit; - } - orig_name = args[0]; - callable = args[1]; - return_value = pysqlite_register_converter_impl(module, orig_name, callable); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_enable_callback_trace__doc__, -"enable_callback_tracebacks($module, enable, /)\n" -"--\n" -"\n" -"Enable or disable callback functions throwing errors to stderr."); - -#define PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF \ - {"enable_callback_tracebacks", (PyCFunction)pysqlite_enable_callback_trace, METH_O, pysqlite_enable_callback_trace__doc__}, - -static PyObject * -pysqlite_enable_callback_trace_impl(PyObject *module, int enable); - -static PyObject * -pysqlite_enable_callback_trace(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - int enable; - - enable = _PyLong_AsInt(arg); - if (enable == -1 && PyErr_Occurred()) { - goto exit; - } - return_value = pysqlite_enable_callback_trace_impl(module, enable); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_adapt__doc__, -"adapt($module, obj, proto=PrepareProtocolType, alt=<unrepresentable>, /)\n" -"--\n" -"\n" -"Adapt given object to given protocol."); - -#define PYSQLITE_ADAPT_METHODDEF \ - {"adapt", _PyCFunction_CAST(pysqlite_adapt), METH_FASTCALL, pysqlite_adapt__doc__}, - -static PyObject * -pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto, - PyObject *alt); - -static PyObject * -pysqlite_adapt(PyObject *module, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - PyObject *obj; - PyObject *proto = (PyObject *)clinic_state()->PrepareProtocolType; - PyObject *alt = NULL; - - if (!_PyArg_CheckPositional("adapt", nargs, 1, 3)) { - goto exit; - } - obj = args[0]; - if (nargs < 2) { - goto skip_optional; - } - proto = args[1]; - if (nargs < 3) { - goto skip_optional; - } - alt = args[2]; -skip_optional: - return_value = pysqlite_adapt_impl(module, obj, proto, alt); - -exit: - return return_value; -} -/*[clinic end generated code: output=39d38c6cfc455042 input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_sqlite/clinic/row.c.h b/contrib/tools/python3/src/Modules/_sqlite/clinic/row.c.h deleted file mode 100644 index 89a48fd52da..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/clinic/row.c.h +++ /dev/null @@ -1,63 +0,0 @@ -/*[clinic input] -preserve -[clinic start generated code]*/ - -#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) -# include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() -#endif - - -static PyObject * -pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor, - PyObject *data); - -static PyObject * -pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - PyObject *return_value = NULL; - PyTypeObject *base_tp = clinic_state()->RowType; - pysqlite_Cursor *cursor; - PyObject *data; - - if ((type == base_tp || type->tp_init == base_tp->tp_init) && - !_PyArg_NoKeywords("Row", kwargs)) { - goto exit; - } - if (!_PyArg_CheckPositional("Row", PyTuple_GET_SIZE(args), 2, 2)) { - goto exit; - } - if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), clinic_state()->CursorType)) { - _PyArg_BadArgument("Row", "argument 1", (clinic_state()->CursorType)->tp_name, PyTuple_GET_ITEM(args, 0)); - goto exit; - } - cursor = (pysqlite_Cursor *)PyTuple_GET_ITEM(args, 0); - if (!PyTuple_Check(PyTuple_GET_ITEM(args, 1))) { - _PyArg_BadArgument("Row", "argument 2", "tuple", PyTuple_GET_ITEM(args, 1)); - goto exit; - } - data = PyTuple_GET_ITEM(args, 1); - return_value = pysqlite_row_new_impl(type, cursor, data); - -exit: - return return_value; -} - -PyDoc_STRVAR(pysqlite_row_keys__doc__, -"keys($self, /)\n" -"--\n" -"\n" -"Returns the keys of the row."); - -#define PYSQLITE_ROW_KEYS_METHODDEF \ - {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, pysqlite_row_keys__doc__}, - -static PyObject * -pysqlite_row_keys_impl(pysqlite_Row *self); - -static PyObject * -pysqlite_row_keys(pysqlite_Row *self, PyObject *Py_UNUSED(ignored)) -{ - return pysqlite_row_keys_impl(self); -} -/*[clinic end generated code: output=157b31ac3f6af1ba input=a9049054013a1b77]*/ diff --git a/contrib/tools/python3/src/Modules/_sqlite/connection.c b/contrib/tools/python3/src/Modules/_sqlite/connection.c deleted file mode 100644 index 12e5c135aaf..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/connection.c +++ /dev/null @@ -1,2659 +0,0 @@ -/* connection.c - the connection type - * - * Copyright (C) 2004-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include "module.h" -#include "structmember.h" // PyMemberDef -#include "connection.h" -#include "statement.h" -#include "cursor.h" -#include "blob.h" -#include "prepare_protocol.h" -#include "util.h" - -#include <stdbool.h> - -#if SQLITE_VERSION_NUMBER >= 3014000 -#define HAVE_TRACE_V2 -#endif - -#if SQLITE_VERSION_NUMBER >= 3025000 -#define HAVE_WINDOW_FUNCTIONS -#endif - -static const char * -get_isolation_level(const char *level) -{ - assert(level != NULL); - static const char *const allowed_levels[] = { - "", - "DEFERRED", - "IMMEDIATE", - "EXCLUSIVE", - NULL - }; - for (int i = 0; allowed_levels[i] != NULL; i++) { - const char *candidate = allowed_levels[i]; - if (sqlite3_stricmp(level, candidate) == 0) { - return candidate; - } - } - PyErr_SetString(PyExc_ValueError, - "isolation_level string must be '', 'DEFERRED', " - "'IMMEDIATE', or 'EXCLUSIVE'"); - return NULL; -} - -static int -isolation_level_converter(PyObject *str_or_none, const char **result) -{ - if (Py_IsNone(str_or_none)) { - *result = NULL; - } - else if (PyUnicode_Check(str_or_none)) { - Py_ssize_t sz; - const char *str = PyUnicode_AsUTF8AndSize(str_or_none, &sz); - if (str == NULL) { - return 0; - } - if (strlen(str) != (size_t)sz) { - PyErr_SetString(PyExc_ValueError, "embedded null character"); - return 0; - } - - const char *level = get_isolation_level(str); - if (level == NULL) { - return 0; - } - *result = level; - } - else { - PyErr_SetString(PyExc_TypeError, - "isolation_level must be str or None"); - return 0; - } - return 1; -} - -static int -autocommit_converter(PyObject *val, enum autocommit_mode *result) -{ - if (Py_IsTrue(val)) { - *result = AUTOCOMMIT_ENABLED; - return 1; - } - if (Py_IsFalse(val)) { - *result = AUTOCOMMIT_DISABLED; - return 1; - } - if (PyLong_Check(val) && - PyLong_AsLong(val) == LEGACY_TRANSACTION_CONTROL) - { - *result = AUTOCOMMIT_LEGACY; - return 1; - } - - PyErr_SetString(PyExc_ValueError, - "autocommit must be True, False, or " - "sqlite3.LEGACY_TRANSACTION_CONTROL"); - return 0; -} - -static int -sqlite3_int64_converter(PyObject *obj, sqlite3_int64 *result) -{ - if (!PyLong_Check(obj)) { - PyErr_SetString(PyExc_TypeError, "expected 'int'"); - return 0; - } - *result = _pysqlite_long_as_int64(obj); - if (PyErr_Occurred()) { - return 0; - } - return 1; -} - -#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self))) -#include "clinic/connection.c.h" -#undef clinic_state - -/*[clinic input] -module _sqlite3 -class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionType" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=67369db2faf80891]*/ - -static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self); -static void free_callback_context(callback_context *ctx); -static void set_callback_context(callback_context **ctx_pp, - callback_context *ctx); -static int connection_close(pysqlite_Connection *self); -PyObject *_pysqlite_query_execute(pysqlite_Cursor *, int, PyObject *, PyObject *); - -static PyObject * -new_statement_cache(pysqlite_Connection *self, pysqlite_state *state, - int maxsize) -{ - PyObject *args[] = { NULL, PyLong_FromLong(maxsize), }; - if (args[1] == NULL) { - return NULL; - } - PyObject *lru_cache = state->lru_cache; - size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; - PyObject *inner = PyObject_Vectorcall(lru_cache, args + 1, nargsf, NULL); - Py_DECREF(args[1]); - if (inner == NULL) { - return NULL; - } - - args[1] = (PyObject *)self; // Borrowed ref. - nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; - PyObject *res = PyObject_Vectorcall(inner, args + 1, nargsf, NULL); - Py_DECREF(inner); - return res; -} - -static inline int -connection_exec_stmt(pysqlite_Connection *self, const char *sql) -{ - int rc; - Py_BEGIN_ALLOW_THREADS - int len = (int)strlen(sql) + 1; - sqlite3_stmt *stmt; - rc = sqlite3_prepare_v2(self->db, sql, len, &stmt, NULL); - if (rc == SQLITE_OK) { - (void)sqlite3_step(stmt); - rc = sqlite3_finalize(stmt); - } - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - (void)_pysqlite_seterror(self->state, self->db); - return -1; - } - return 0; -} - -/*[python input] -class IsolationLevel_converter(CConverter): - type = "const char *" - converter = "isolation_level_converter" - -class Autocommit_converter(CConverter): - type = "enum autocommit_mode" - converter = "autocommit_converter" - -class sqlite3_int64_converter(CConverter): - type = "sqlite3_int64" - converter = "sqlite3_int64_converter" - -[python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=dff8760fb1eba6a1]*/ - -// NB: This needs to be in sync with the sqlite3.connect docstring -/*[clinic input] -_sqlite3.Connection.__init__ as pysqlite_connection_init - - database: object - timeout: double = 5.0 - detect_types: int = 0 - isolation_level: IsolationLevel = "" - check_same_thread: bool = True - factory: object(c_default='(PyObject*)clinic_state()->ConnectionType') = ConnectionType - cached_statements as cache_size: int = 128 - uri: bool = False - * - autocommit: Autocommit(c_default='LEGACY_TRANSACTION_CONTROL') = sqlite3.LEGACY_TRANSACTION_CONTROL -[clinic start generated code]*/ - -static int -pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, - double timeout, int detect_types, - const char *isolation_level, - int check_same_thread, PyObject *factory, - int cache_size, int uri, - enum autocommit_mode autocommit) -/*[clinic end generated code: output=cba057313ea7712f input=9b0ab6c12f674fa3]*/ -{ - if (PySys_Audit("sqlite3.connect", "O", database) < 0) { - return -1; - } - - PyObject *bytes; - if (!PyUnicode_FSConverter(database, &bytes)) { - return -1; - } - - if (self->initialized) { - self->initialized = 0; - - PyTypeObject *tp = Py_TYPE(self); - tp->tp_clear((PyObject *)self); - if (connection_close(self) < 0) { - return -1; - } - } - - // Create and configure SQLite database object. - sqlite3 *db; - int rc; - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_open_v2(PyBytes_AS_STRING(bytes), &db, - SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | - (uri ? SQLITE_OPEN_URI : 0), NULL); - if (rc == SQLITE_OK) { - (void)sqlite3_busy_timeout(db, (int)(timeout*1000)); - } - Py_END_ALLOW_THREADS - - Py_DECREF(bytes); - if (db == NULL && rc == SQLITE_NOMEM) { - PyErr_NoMemory(); - return -1; - } - - pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self)); - if (rc != SQLITE_OK) { - _pysqlite_seterror(state, db); - goto error; - } - - // Create LRU statement cache; returns a new reference. - PyObject *statement_cache = new_statement_cache(self, state, cache_size); - if (statement_cache == NULL) { - goto error; - } - - /* Create lists of weak references to cursors and blobs */ - PyObject *cursors = PyList_New(0); - if (cursors == NULL) { - Py_DECREF(statement_cache); - goto error; - } - - PyObject *blobs = PyList_New(0); - if (blobs == NULL) { - Py_DECREF(statement_cache); - Py_DECREF(cursors); - goto error; - } - - // Init connection state members. - self->db = db; - self->state = state; - self->detect_types = detect_types; - self->isolation_level = isolation_level; - self->autocommit = autocommit; - self->check_same_thread = check_same_thread; - self->thread_ident = PyThread_get_thread_ident(); - self->statement_cache = statement_cache; - self->cursors = cursors; - self->blobs = blobs; - self->created_cursors = 0; - self->row_factory = Py_NewRef(Py_None); - self->text_factory = Py_NewRef(&PyUnicode_Type); - self->trace_ctx = NULL; - self->progress_ctx = NULL; - self->authorizer_ctx = NULL; - - // Borrowed refs - self->Warning = state->Warning; - self->Error = state->Error; - self->InterfaceError = state->InterfaceError; - self->DatabaseError = state->DatabaseError; - self->DataError = state->DataError; - self->OperationalError = state->OperationalError; - self->IntegrityError = state->IntegrityError; - self->InternalError = state->InternalError; - self->ProgrammingError = state->ProgrammingError; - self->NotSupportedError = state->NotSupportedError; - - if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) { - return -1; // Don't goto error; at this point, dealloc will clean up. - } - - self->initialized = 1; - - if (autocommit == AUTOCOMMIT_DISABLED) { - if (connection_exec_stmt(self, "BEGIN") < 0) { - return -1; - } - } - return 0; - -error: - // There are no statements or other SQLite objects attached to the - // database, so sqlite3_close() should always return SQLITE_OK. - rc = sqlite3_close(db); - assert(rc == SQLITE_OK); - return -1; -} - -#define VISIT_CALLBACK_CONTEXT(ctx) \ -do { \ - if (ctx) { \ - Py_VISIT(ctx->callable); \ - Py_VISIT(ctx->module); \ - } \ -} while (0) - -static int -connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg) -{ - Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->statement_cache); - Py_VISIT(self->cursors); - Py_VISIT(self->blobs); - Py_VISIT(self->row_factory); - Py_VISIT(self->text_factory); - VISIT_CALLBACK_CONTEXT(self->trace_ctx); - VISIT_CALLBACK_CONTEXT(self->progress_ctx); - VISIT_CALLBACK_CONTEXT(self->authorizer_ctx); -#undef VISIT_CALLBACK_CONTEXT - return 0; -} - -static inline void -clear_callback_context(callback_context *ctx) -{ - if (ctx != NULL) { - Py_CLEAR(ctx->callable); - Py_CLEAR(ctx->module); - } -} - -static int -connection_clear(pysqlite_Connection *self) -{ - Py_CLEAR(self->statement_cache); - Py_CLEAR(self->cursors); - Py_CLEAR(self->blobs); - Py_CLEAR(self->row_factory); - Py_CLEAR(self->text_factory); - clear_callback_context(self->trace_ctx); - clear_callback_context(self->progress_ctx); - clear_callback_context(self->authorizer_ctx); - return 0; -} - -static void -free_callback_contexts(pysqlite_Connection *self) -{ - set_callback_context(&self->trace_ctx, NULL); - set_callback_context(&self->progress_ctx, NULL); - set_callback_context(&self->authorizer_ctx, NULL); -} - -static void -remove_callbacks(sqlite3 *db) -{ - assert(db != NULL); - /* None of these APIs can fail, as long as they are given a valid - * database pointer. */ - int rc; -#ifdef HAVE_TRACE_V2 - rc = sqlite3_trace_v2(db, SQLITE_TRACE_STMT, 0, 0); - assert(rc == SQLITE_OK), (void)rc; -#else - sqlite3_trace(db, 0, (void*)0); -#endif - - sqlite3_progress_handler(db, 0, 0, (void *)0); - - rc = sqlite3_set_authorizer(db, NULL, NULL); - assert(rc == SQLITE_OK), (void)rc; -} - -static int -connection_close(pysqlite_Connection *self) -{ - if (self->db == NULL) { - return 0; - } - - int rc = 0; - if (self->autocommit == AUTOCOMMIT_DISABLED && - !sqlite3_get_autocommit(self->db)) - { - if (connection_exec_stmt(self, "ROLLBACK") < 0) { - rc = -1; - } - } - - sqlite3 *db = self->db; - self->db = NULL; - - Py_BEGIN_ALLOW_THREADS - /* The v2 close call always returns SQLITE_OK if given a valid database - * pointer (which we do), so we can safely ignore the return value */ - (void)sqlite3_close_v2(db); - Py_END_ALLOW_THREADS - - free_callback_contexts(self); - return rc; -} - -static void -connection_finalize(PyObject *self) -{ - pysqlite_Connection *con = (pysqlite_Connection *)self; - PyObject *exc = PyErr_GetRaisedException(); - - /* If close is implicitly called as a result of interpreter - * tear-down, we must not call back into Python. */ - PyInterpreterState *interp = PyInterpreterState_Get(); - int teardown = _Py_IsInterpreterFinalizing(interp); - if (teardown && con->db) { - remove_callbacks(con->db); - } - - /* Clean up if user has not called .close() explicitly. */ - if (connection_close(con) < 0) { - if (teardown) { - PyErr_Clear(); - } - else { - PyErr_WriteUnraisable((PyObject *)self); - } - } - - PyErr_SetRaisedException(exc); -} - -static void -connection_dealloc(PyObject *self) -{ - if (PyObject_CallFinalizerFromDealloc(self) < 0) { - return; - } - PyTypeObject *tp = Py_TYPE(self); - PyObject_GC_UnTrack(self); - tp->tp_clear(self); - tp->tp_free(self); - Py_DECREF(tp); -} - -/*[clinic input] -_sqlite3.Connection.cursor as pysqlite_connection_cursor - - factory: object = NULL - -Return a cursor for the connection. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory) -/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/ -{ - PyObject* cursor; - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (factory == NULL) { - factory = (PyObject *)self->state->CursorType; - } - - cursor = PyObject_CallOneArg(factory, (PyObject *)self); - if (cursor == NULL) - return NULL; - if (!PyObject_TypeCheck(cursor, self->state->CursorType)) { - PyErr_Format(PyExc_TypeError, - "factory must return a cursor, not %.100s", - Py_TYPE(cursor)->tp_name); - Py_DECREF(cursor); - return NULL; - } - - _pysqlite_drop_unused_cursor_references(self); - - if (cursor && self->row_factory != Py_None) { - Py_INCREF(self->row_factory); - Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory); - } - - return cursor; -} - -/*[clinic input] -_sqlite3.Connection.blobopen as blobopen - - table: str - Table name. - column as col: str - Column name. - row: sqlite3_int64 - Row index. - / - * - readonly: bool = False - Open the BLOB without write permissions. - name: str = "main" - Database name. - -Open and return a BLOB object. -[clinic start generated code]*/ - -static PyObject * -blobopen_impl(pysqlite_Connection *self, const char *table, const char *col, - sqlite3_int64 row, int readonly, const char *name) -/*[clinic end generated code: output=6a02d43efb885d1c input=23576bd1108d8774]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - int rc; - sqlite3_blob *blob; - - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_blob_open(self->db, name, table, col, row, !readonly, &blob); - Py_END_ALLOW_THREADS - - if (rc == SQLITE_MISUSE) { - PyErr_Format(self->state->InterfaceError, sqlite3_errstr(rc)); - return NULL; - } - else if (rc != SQLITE_OK) { - _pysqlite_seterror(self->state, self->db); - return NULL; - } - - pysqlite_Blob *obj = PyObject_GC_New(pysqlite_Blob, self->state->BlobType); - if (obj == NULL) { - goto error; - } - - obj->connection = (pysqlite_Connection *)Py_NewRef(self); - obj->blob = blob; - obj->offset = 0; - obj->in_weakreflist = NULL; - - PyObject_GC_Track(obj); - - // Add our blob to connection blobs list - PyObject *weakref = PyWeakref_NewRef((PyObject *)obj, NULL); - if (weakref == NULL) { - goto error; - } - rc = PyList_Append(self->blobs, weakref); - Py_DECREF(weakref); - if (rc < 0) { - goto error; - } - - return (PyObject *)obj; - -error: - Py_XDECREF(obj); - return NULL; -} - -/*[clinic input] -_sqlite3.Connection.close as pysqlite_connection_close - -Close the database connection. - -Any pending transaction is not committed implicitly. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_close_impl(pysqlite_Connection *self) -/*[clinic end generated code: output=a546a0da212c9b97 input=b3ed5b74f6fefc06]*/ -{ - if (!pysqlite_check_thread(self)) { - return NULL; - } - - if (!self->initialized) { - PyTypeObject *tp = Py_TYPE(self); - pysqlite_state *state = pysqlite_get_state_by_type(tp); - PyErr_SetString(state->ProgrammingError, - "Base Connection.__init__ not called."); - return NULL; - } - - pysqlite_close_all_blobs(self); - Py_CLEAR(self->statement_cache); - if (connection_close(self) < 0) { - return NULL; - } - - Py_RETURN_NONE; -} - -/* - * Checks if a connection object is usable (i. e. not closed). - * - * 0 => error; 1 => ok - */ -int pysqlite_check_connection(pysqlite_Connection* con) -{ - if (!con->initialized) { - pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(con)); - PyErr_SetString(state->ProgrammingError, - "Base Connection.__init__ not called."); - return 0; - } - - if (!con->db) { - PyErr_SetString(con->state->ProgrammingError, - "Cannot operate on a closed database."); - return 0; - } else { - return 1; - } -} - -/*[clinic input] -_sqlite3.Connection.commit as pysqlite_connection_commit - -Commit any pending transaction to the database. - -If there is no open transaction, this method is a no-op. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_commit_impl(pysqlite_Connection *self) -/*[clinic end generated code: output=3da45579e89407f2 input=c8793c97c3446065]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (self->autocommit == AUTOCOMMIT_LEGACY) { - if (!sqlite3_get_autocommit(self->db)) { - if (connection_exec_stmt(self, "COMMIT") < 0) { - return NULL; - } - } - } - else if (self->autocommit == AUTOCOMMIT_DISABLED) { - if (connection_exec_stmt(self, "COMMIT") < 0) { - return NULL; - } - if (connection_exec_stmt(self, "BEGIN") < 0) { - return NULL; - } - } - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.Connection.rollback as pysqlite_connection_rollback - -Roll back to the start of any pending transaction. - -If there is no open transaction, this method is a no-op. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_rollback_impl(pysqlite_Connection *self) -/*[clinic end generated code: output=b66fa0d43e7ef305 input=7f60a2f1076f16b3]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (self->autocommit == AUTOCOMMIT_LEGACY) { - if (!sqlite3_get_autocommit(self->db)) { - if (connection_exec_stmt(self, "ROLLBACK") < 0) { - return NULL; - } - } - } - else if (self->autocommit == AUTOCOMMIT_DISABLED) { - if (connection_exec_stmt(self, "ROLLBACK") < 0) { - return NULL; - } - if (connection_exec_stmt(self, "BEGIN") < 0) { - return NULL; - } - } - Py_RETURN_NONE; -} - -static int -_pysqlite_set_result(sqlite3_context* context, PyObject* py_val) -{ - if (py_val == Py_None) { - sqlite3_result_null(context); - } else if (PyLong_Check(py_val)) { - sqlite_int64 value = _pysqlite_long_as_int64(py_val); - if (value == -1 && PyErr_Occurred()) - return -1; - sqlite3_result_int64(context, value); - } else if (PyFloat_Check(py_val)) { - double value = PyFloat_AsDouble(py_val); - if (value == -1 && PyErr_Occurred()) { - return -1; - } - sqlite3_result_double(context, value); - } else if (PyUnicode_Check(py_val)) { - Py_ssize_t sz; - const char *str = PyUnicode_AsUTF8AndSize(py_val, &sz); - if (str == NULL) { - return -1; - } - if (sz > INT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "string is longer than INT_MAX bytes"); - return -1; - } - sqlite3_result_text(context, str, (int)sz, SQLITE_TRANSIENT); - } else if (PyObject_CheckBuffer(py_val)) { - Py_buffer view; - if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) { - return -1; - } - if (view.len > INT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "BLOB longer than INT_MAX bytes"); - PyBuffer_Release(&view); - return -1; - } - sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT); - PyBuffer_Release(&view); - } else { - callback_context *ctx = (callback_context *)sqlite3_user_data(context); - PyErr_Format(ctx->state->ProgrammingError, - "User-defined functions cannot return '%s' values to " - "SQLite", - Py_TYPE(py_val)->tp_name); - return -1; - } - return 0; -} - -static PyObject * -_pysqlite_build_py_params(sqlite3_context *context, int argc, - sqlite3_value **argv) -{ - PyObject* args; - int i; - sqlite3_value* cur_value; - PyObject* cur_py_value; - - args = PyTuple_New(argc); - if (!args) { - return NULL; - } - - for (i = 0; i < argc; i++) { - cur_value = argv[i]; - switch (sqlite3_value_type(argv[i])) { - case SQLITE_INTEGER: - cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value)); - break; - case SQLITE_FLOAT: - cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value)); - break; - case SQLITE_TEXT: { - sqlite3 *db = sqlite3_context_db_handle(context); - const char *text = (const char *)sqlite3_value_text(cur_value); - - if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) { - PyErr_NoMemory(); - goto error; - } - - Py_ssize_t size = sqlite3_value_bytes(cur_value); - cur_py_value = PyUnicode_FromStringAndSize(text, size); - break; - } - case SQLITE_BLOB: { - sqlite3 *db = sqlite3_context_db_handle(context); - const void *blob = sqlite3_value_blob(cur_value); - - if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) { - PyErr_NoMemory(); - goto error; - } - - Py_ssize_t size = sqlite3_value_bytes(cur_value); - cur_py_value = PyBytes_FromStringAndSize(blob, size); - break; - } - case SQLITE_NULL: - default: - cur_py_value = Py_NewRef(Py_None); - } - - if (!cur_py_value) { - goto error; - } - - PyTuple_SET_ITEM(args, i, cur_py_value); - } - - return args; - -error: - Py_DECREF(args); - return NULL; -} - -static void -print_or_clear_traceback(callback_context *ctx) -{ - assert(ctx != NULL); - assert(ctx->state != NULL); - if (ctx->state->enable_callback_tracebacks) { - PyErr_WriteUnraisable(ctx->callable); - } - else { - PyErr_Clear(); - } -} - -// Checks the Python exception and sets the appropriate SQLite error code. -static void -set_sqlite_error(sqlite3_context *context, const char *msg) -{ - assert(PyErr_Occurred()); - if (PyErr_ExceptionMatches(PyExc_MemoryError)) { - sqlite3_result_error_nomem(context); - } - else if (PyErr_ExceptionMatches(PyExc_OverflowError)) { - sqlite3_result_error_toobig(context); - } - else { - sqlite3_result_error(context, msg, -1); - } - callback_context *ctx = (callback_context *)sqlite3_user_data(context); - print_or_clear_traceback(ctx); -} - -static void -func_callback(sqlite3_context *context, int argc, sqlite3_value **argv) -{ - PyGILState_STATE threadstate = PyGILState_Ensure(); - - PyObject* args; - PyObject* py_retval = NULL; - int ok; - - args = _pysqlite_build_py_params(context, argc, argv); - if (args) { - callback_context *ctx = (callback_context *)sqlite3_user_data(context); - assert(ctx != NULL); - py_retval = PyObject_CallObject(ctx->callable, args); - Py_DECREF(args); - } - - ok = 0; - if (py_retval) { - ok = _pysqlite_set_result(context, py_retval) == 0; - Py_DECREF(py_retval); - } - if (!ok) { - set_sqlite_error(context, "user-defined function raised exception"); - } - - PyGILState_Release(threadstate); -} - -static void -step_callback(sqlite3_context *context, int argc, sqlite3_value **params) -{ - PyGILState_STATE threadstate = PyGILState_Ensure(); - - PyObject* args; - PyObject* function_result = NULL; - PyObject** aggregate_instance; - PyObject* stepmethod = NULL; - - callback_context *ctx = (callback_context *)sqlite3_user_data(context); - assert(ctx != NULL); - - aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); - if (*aggregate_instance == NULL) { - *aggregate_instance = PyObject_CallNoArgs(ctx->callable); - if (!*aggregate_instance) { - set_sqlite_error(context, - "user-defined aggregate's '__init__' method raised error"); - goto error; - } - } - - stepmethod = PyObject_GetAttr(*aggregate_instance, ctx->state->str_step); - if (!stepmethod) { - set_sqlite_error(context, - "user-defined aggregate's 'step' method not defined"); - goto error; - } - - args = _pysqlite_build_py_params(context, argc, params); - if (!args) { - goto error; - } - - function_result = PyObject_CallObject(stepmethod, args); - Py_DECREF(args); - - if (!function_result) { - set_sqlite_error(context, - "user-defined aggregate's 'step' method raised error"); - } - -error: - Py_XDECREF(stepmethod); - Py_XDECREF(function_result); - - PyGILState_Release(threadstate); -} - -static void -final_callback(sqlite3_context *context) -{ - PyGILState_STATE threadstate = PyGILState_Ensure(); - - PyObject* function_result; - PyObject** aggregate_instance; - int ok; - - aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0); - if (aggregate_instance == NULL) { - /* No rows matched the query; the step handler was never called. */ - goto error; - } - else if (!*aggregate_instance) { - /* this branch is executed if there was an exception in the aggregate's - * __init__ */ - - goto error; - } - - // Keep the exception (if any) of the last call to step, value, or inverse - PyObject *exc = PyErr_GetRaisedException(); - - callback_context *ctx = (callback_context *)sqlite3_user_data(context); - assert(ctx != NULL); - function_result = PyObject_CallMethodNoArgs(*aggregate_instance, - ctx->state->str_finalize); - Py_DECREF(*aggregate_instance); - - ok = 0; - if (function_result) { - ok = _pysqlite_set_result(context, function_result) == 0; - Py_DECREF(function_result); - } - if (!ok) { - int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError); - _PyErr_ChainExceptions1(exc); - - /* Note: contrary to the step, value, and inverse callbacks, SQLite - * does _not_, as of SQLite 3.38.0, propagate errors to sqlite3_step() - * from the finalize callback. This implies that execute*() will not - * raise OperationalError, as it normally would. */ - set_sqlite_error(context, attr_err - ? "user-defined aggregate's 'finalize' method not defined" - : "user-defined aggregate's 'finalize' method raised error"); - } - else { - PyErr_SetRaisedException(exc); - } - -error: - PyGILState_Release(threadstate); -} - -static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) -{ - PyObject* new_list; - PyObject* weakref; - int i; - - /* we only need to do this once in a while */ - if (self->created_cursors++ < 200) { - return; - } - - self->created_cursors = 0; - - new_list = PyList_New(0); - if (!new_list) { - return; - } - - for (i = 0; i < PyList_Size(self->cursors); i++) { - weakref = PyList_GetItem(self->cursors, i); - if (PyWeakref_GetObject(weakref) != Py_None) { - if (PyList_Append(new_list, weakref) != 0) { - Py_DECREF(new_list); - return; - } - } - } - - Py_SETREF(self->cursors, new_list); -} - -/* Allocate a UDF/callback context structure. In order to ensure that the state - * pointer always outlives the callback context, we make sure it owns a - * reference to the module itself. create_callback_context() is always called - * from connection methods, so we use the defining class to fetch the module - * pointer. - */ -static callback_context * -create_callback_context(PyTypeObject *cls, PyObject *callable) -{ - callback_context *ctx = PyMem_Malloc(sizeof(callback_context)); - if (ctx != NULL) { - PyObject *module = PyType_GetModule(cls); - ctx->callable = Py_NewRef(callable); - ctx->module = Py_NewRef(module); - ctx->state = pysqlite_get_state(module); - } - return ctx; -} - -static void -free_callback_context(callback_context *ctx) -{ - assert(ctx != NULL); - Py_XDECREF(ctx->callable); - Py_XDECREF(ctx->module); - PyMem_Free(ctx); -} - -static void -set_callback_context(callback_context **ctx_pp, callback_context *ctx) -{ - assert(ctx_pp != NULL); - callback_context *tmp = *ctx_pp; - *ctx_pp = ctx; - if (tmp != NULL) { - free_callback_context(tmp); - } -} - -static void -destructor_callback(void *ctx) -{ - if (ctx != NULL) { - // This function may be called without the GIL held, so we need to - // ensure that we destroy 'ctx' with the GIL held. - PyGILState_STATE gstate = PyGILState_Ensure(); - free_callback_context((callback_context *)ctx); - PyGILState_Release(gstate); - } -} - -/*[clinic input] -_sqlite3.Connection.create_function as pysqlite_connection_create_function - - cls: defining_class - / - name: str - narg: int - func: object - * - deterministic: bool = False - -Creates a new function. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_create_function_impl(pysqlite_Connection *self, - PyTypeObject *cls, const char *name, - int narg, PyObject *func, - int deterministic) -/*[clinic end generated code: output=8a811529287ad240 input=b3e8e1d8ddaffbef]*/ -{ - int rc; - int flags = SQLITE_UTF8; - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (deterministic) { -#if SQLITE_VERSION_NUMBER < 3008003 - PyErr_SetString(self->NotSupportedError, - "deterministic=True requires SQLite 3.8.3 or higher"); - return NULL; -#else - if (sqlite3_libversion_number() < 3008003) { - PyErr_SetString(self->NotSupportedError, - "deterministic=True requires SQLite 3.8.3 or higher"); - return NULL; - } - flags |= SQLITE_DETERMINISTIC; -#endif - } - callback_context *ctx = create_callback_context(cls, func); - if (ctx == NULL) { - return NULL; - } - rc = sqlite3_create_function_v2(self->db, name, narg, flags, ctx, - func_callback, - NULL, - NULL, - &destructor_callback); // will decref func - - if (rc != SQLITE_OK) { - /* Workaround for SQLite bug: no error code or string is available here */ - PyErr_SetString(self->OperationalError, "Error creating function"); - return NULL; - } - Py_RETURN_NONE; -} - -#ifdef HAVE_WINDOW_FUNCTIONS -/* - * Regarding the 'inverse' aggregate callback: - * This method is only required by window aggregate functions, not - * ordinary aggregate function implementations. It is invoked to remove - * a row from the current window. The function arguments, if any, - * correspond to the row being removed. - */ -static void -inverse_callback(sqlite3_context *context, int argc, sqlite3_value **params) -{ - PyGILState_STATE gilstate = PyGILState_Ensure(); - - callback_context *ctx = (callback_context *)sqlite3_user_data(context); - assert(ctx != NULL); - - int size = sizeof(PyObject *); - PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size); - assert(cls != NULL); - assert(*cls != NULL); - - PyObject *method = PyObject_GetAttr(*cls, ctx->state->str_inverse); - if (method == NULL) { - set_sqlite_error(context, - "user-defined aggregate's 'inverse' method not defined"); - goto exit; - } - - PyObject *args = _pysqlite_build_py_params(context, argc, params); - if (args == NULL) { - set_sqlite_error(context, - "unable to build arguments for user-defined aggregate's " - "'inverse' method"); - goto exit; - } - - PyObject *res = PyObject_CallObject(method, args); - Py_DECREF(args); - if (res == NULL) { - set_sqlite_error(context, - "user-defined aggregate's 'inverse' method raised error"); - goto exit; - } - Py_DECREF(res); - -exit: - Py_XDECREF(method); - PyGILState_Release(gilstate); -} - -/* - * Regarding the 'value' aggregate callback: - * This method is only required by window aggregate functions, not - * ordinary aggregate function implementations. It is invoked to return - * the current value of the aggregate. - */ -static void -value_callback(sqlite3_context *context) -{ - PyGILState_STATE gilstate = PyGILState_Ensure(); - - callback_context *ctx = (callback_context *)sqlite3_user_data(context); - assert(ctx != NULL); - - int size = sizeof(PyObject *); - PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size); - assert(cls != NULL); - assert(*cls != NULL); - - PyObject *res = PyObject_CallMethodNoArgs(*cls, ctx->state->str_value); - if (res == NULL) { - int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError); - set_sqlite_error(context, attr_err - ? "user-defined aggregate's 'value' method not defined" - : "user-defined aggregate's 'value' method raised error"); - } - else { - int rc = _pysqlite_set_result(context, res); - Py_DECREF(res); - if (rc < 0) { - set_sqlite_error(context, - "unable to set result from user-defined aggregate's " - "'value' method"); - } - } - - PyGILState_Release(gilstate); -} - -/*[clinic input] -_sqlite3.Connection.create_window_function as create_window_function - - cls: defining_class - name: str - The name of the SQL aggregate window function to be created or - redefined. - num_params: int - The number of arguments the step and inverse methods takes. - aggregate_class: object - A class with step(), finalize(), value(), and inverse() methods. - Set to None to clear the window function. - / - -Creates or redefines an aggregate window function. Non-standard. -[clinic start generated code]*/ - -static PyObject * -create_window_function_impl(pysqlite_Connection *self, PyTypeObject *cls, - const char *name, int num_params, - PyObject *aggregate_class) -/*[clinic end generated code: output=5332cd9464522235 input=46d57a54225b5228]*/ -{ - if (sqlite3_libversion_number() < 3025000) { - PyErr_SetString(self->NotSupportedError, - "create_window_function() requires " - "SQLite 3.25.0 or higher"); - return NULL; - } - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - int flags = SQLITE_UTF8; - int rc; - if (Py_IsNone(aggregate_class)) { - rc = sqlite3_create_window_function(self->db, name, num_params, flags, - 0, 0, 0, 0, 0, 0); - } - else { - callback_context *ctx = create_callback_context(cls, aggregate_class); - if (ctx == NULL) { - return NULL; - } - rc = sqlite3_create_window_function(self->db, name, num_params, flags, - ctx, - &step_callback, - &final_callback, - &value_callback, - &inverse_callback, - &destructor_callback); - } - - if (rc != SQLITE_OK) { - // Errors are not set on the database connection, so we cannot - // use _pysqlite_seterror(). - PyErr_SetString(self->ProgrammingError, sqlite3_errstr(rc)); - return NULL; - } - Py_RETURN_NONE; -} -#endif - -/*[clinic input] -_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate - - cls: defining_class - / - name: str - n_arg: int - aggregate_class: object - -Creates a new aggregate. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self, - PyTypeObject *cls, - const char *name, int n_arg, - PyObject *aggregate_class) -/*[clinic end generated code: output=1b02d0f0aec7ff96 input=68a2a26366d4c686]*/ -{ - int rc; - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - callback_context *ctx = create_callback_context(cls, aggregate_class); - if (ctx == NULL) { - return NULL; - } - rc = sqlite3_create_function_v2(self->db, name, n_arg, SQLITE_UTF8, ctx, - 0, - &step_callback, - &final_callback, - &destructor_callback); // will decref func - if (rc != SQLITE_OK) { - /* Workaround for SQLite bug: no error code or string is available here */ - PyErr_SetString(self->OperationalError, "Error creating aggregate"); - return NULL; - } - Py_RETURN_NONE; -} - -static int -authorizer_callback(void *ctx, int action, const char *arg1, - const char *arg2 , const char *dbname, - const char *access_attempt_source) -{ - PyGILState_STATE gilstate = PyGILState_Ensure(); - - PyObject *ret; - int rc = SQLITE_DENY; - - assert(ctx != NULL); - PyObject *callable = ((callback_context *)ctx)->callable; - ret = PyObject_CallFunction(callable, "issss", action, arg1, arg2, dbname, - access_attempt_source); - - if (ret == NULL) { - print_or_clear_traceback(ctx); - rc = SQLITE_DENY; - } - else { - if (PyLong_Check(ret)) { - rc = _PyLong_AsInt(ret); - if (rc == -1 && PyErr_Occurred()) { - print_or_clear_traceback(ctx); - rc = SQLITE_DENY; - } - } - else { - rc = SQLITE_DENY; - } - Py_DECREF(ret); - } - - PyGILState_Release(gilstate); - return rc; -} - -static int -progress_callback(void *ctx) -{ - PyGILState_STATE gilstate = PyGILState_Ensure(); - - int rc; - PyObject *ret; - - assert(ctx != NULL); - PyObject *callable = ((callback_context *)ctx)->callable; - ret = PyObject_CallNoArgs(callable); - if (!ret) { - /* abort query if error occurred */ - rc = -1; - } - else { - rc = PyObject_IsTrue(ret); - Py_DECREF(ret); - } - if (rc < 0) { - print_or_clear_traceback(ctx); - } - - PyGILState_Release(gilstate); - return rc; -} - -#ifdef HAVE_TRACE_V2 -/* - * From https://sqlite.org/c3ref/trace_v2.html: - * The integer return value from the callback is currently ignored, though this - * may change in future releases. Callback implementations should return zero - * to ensure future compatibility. - */ -static int -trace_callback(unsigned int type, void *ctx, void *stmt, void *sql) -#else -static void -trace_callback(void *ctx, const char *sql) -#endif -{ -#ifdef HAVE_TRACE_V2 - if (type != SQLITE_TRACE_STMT) { - return 0; - } -#endif - - PyGILState_STATE gilstate = PyGILState_Ensure(); - - assert(ctx != NULL); - pysqlite_state *state = ((callback_context *)ctx)->state; - assert(state != NULL); - - PyObject *py_statement = NULL; -#ifdef HAVE_TRACE_V2 - const char *expanded_sql = sqlite3_expanded_sql((sqlite3_stmt *)stmt); - if (expanded_sql == NULL) { - sqlite3 *db = sqlite3_db_handle((sqlite3_stmt *)stmt); - if (sqlite3_errcode(db) == SQLITE_NOMEM) { - (void)PyErr_NoMemory(); - goto exit; - } - - PyErr_SetString(state->DataError, - "Expanded SQL string exceeds the maximum string length"); - print_or_clear_traceback((callback_context *)ctx); - - // Fall back to unexpanded sql - py_statement = PyUnicode_FromString((const char *)sql); - } - else { - py_statement = PyUnicode_FromString(expanded_sql); - sqlite3_free((void *)expanded_sql); - } -#else - if (sql == NULL) { - PyErr_SetString(state->DataError, - "Expanded SQL string exceeds the maximum string length"); - print_or_clear_traceback((callback_context *)ctx); - goto exit; - } - py_statement = PyUnicode_FromString(sql); -#endif - if (py_statement) { - PyObject *callable = ((callback_context *)ctx)->callable; - PyObject *ret = PyObject_CallOneArg(callable, py_statement); - Py_DECREF(py_statement); - Py_XDECREF(ret); - } - if (PyErr_Occurred()) { - print_or_clear_traceback((callback_context *)ctx); - } - -exit: - PyGILState_Release(gilstate); -#ifdef HAVE_TRACE_V2 - return 0; -#endif -} - -/*[clinic input] -_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer - - cls: defining_class - / - authorizer_callback as callable: object - -Sets authorizer callback. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self, - PyTypeObject *cls, - PyObject *callable) -/*[clinic end generated code: output=75fa60114fc971c3 input=605d32ba92dd3eca]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - int rc; - if (callable == Py_None) { - rc = sqlite3_set_authorizer(self->db, NULL, NULL); - set_callback_context(&self->authorizer_ctx, NULL); - } - else { - callback_context *ctx = create_callback_context(cls, callable); - if (ctx == NULL) { - return NULL; - } - rc = sqlite3_set_authorizer(self->db, authorizer_callback, ctx); - set_callback_context(&self->authorizer_ctx, ctx); - } - if (rc != SQLITE_OK) { - PyErr_SetString(self->OperationalError, - "Error setting authorizer callback"); - set_callback_context(&self->authorizer_ctx, NULL); - return NULL; - } - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler - - cls: defining_class - / - progress_handler as callable: object - n: int - -Sets progress handler callback. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self, - PyTypeObject *cls, - PyObject *callable, int n) -/*[clinic end generated code: output=0739957fd8034a50 input=f7c1837984bd86db]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (callable == Py_None) { - /* None clears the progress handler previously set */ - sqlite3_progress_handler(self->db, 0, 0, (void*)0); - set_callback_context(&self->progress_ctx, NULL); - } - else { - callback_context *ctx = create_callback_context(cls, callable); - if (ctx == NULL) { - return NULL; - } - sqlite3_progress_handler(self->db, n, progress_callback, ctx); - set_callback_context(&self->progress_ctx, ctx); - } - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback - - cls: defining_class - / - trace_callback as callable: object - -Sets a trace callback called for each SQL statement (passed as unicode). -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self, - PyTypeObject *cls, - PyObject *callable) -/*[clinic end generated code: output=d91048c03bfcee05 input=351a94210c5f81bb]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (callable == Py_None) { - /* - * None clears the trace callback previously set - * - * Ref. - * - https://sqlite.org/c3ref/c_trace.html - * - https://sqlite.org/c3ref/trace_v2.html - */ -#ifdef HAVE_TRACE_V2 - sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0); -#else - sqlite3_trace(self->db, 0, (void*)0); -#endif - set_callback_context(&self->trace_ctx, NULL); - } - else { - callback_context *ctx = create_callback_context(cls, callable); - if (ctx == NULL) { - return NULL; - } -#ifdef HAVE_TRACE_V2 - sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, trace_callback, ctx); -#else - sqlite3_trace(self->db, trace_callback, ctx); -#endif - set_callback_context(&self->trace_ctx, ctx); - } - - Py_RETURN_NONE; -} - -#ifdef PY_SQLITE_ENABLE_LOAD_EXTENSION -/*[clinic input] -_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension - - enable as onoff: bool - / - -Enable dynamic loading of SQLite extension modules. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self, - int onoff) -/*[clinic end generated code: output=9cac37190d388baf input=2a1e87931486380f]*/ -{ - int rc; - - if (PySys_Audit("sqlite3.enable_load_extension", - "OO", self, onoff ? Py_True : Py_False) < 0) { - return NULL; - } - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - rc = sqlite3_enable_load_extension(self->db, onoff); - - if (rc != SQLITE_OK) { - PyErr_SetString(self->OperationalError, - "Error enabling load extension"); - return NULL; - } else { - Py_RETURN_NONE; - } -} - -/*[clinic input] -_sqlite3.Connection.load_extension as pysqlite_connection_load_extension - - name as extension_name: str - / - * - entrypoint: str(accept={str, NoneType}) = None - -Load SQLite extension module. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_load_extension_impl(pysqlite_Connection *self, - const char *extension_name, - const char *entrypoint) -/*[clinic end generated code: output=7e61a7add9de0286 input=c36b14ea702e04f5]*/ -{ - int rc; - char* errmsg; - - if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) { - return NULL; - } - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - rc = sqlite3_load_extension(self->db, extension_name, entrypoint, &errmsg); - if (rc != 0) { - PyErr_SetString(self->OperationalError, errmsg); - return NULL; - } else { - Py_RETURN_NONE; - } -} -#endif - -int pysqlite_check_thread(pysqlite_Connection* self) -{ - if (self->check_same_thread) { - if (PyThread_get_thread_ident() != self->thread_ident) { - PyErr_Format(self->ProgrammingError, - "SQLite objects created in a thread can only be used in that same thread. " - "The object was created in thread id %lu and this is thread id %lu.", - self->thread_ident, PyThread_get_thread_ident()); - return 0; - } - - } - return 1; -} - -static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused) -{ - if (!pysqlite_check_connection(self)) { - return NULL; - } - if (self->isolation_level != NULL) { - return PyUnicode_FromString(self->isolation_level); - } - Py_RETURN_NONE; -} - -static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused) -{ - if (!pysqlite_check_connection(self)) { - return NULL; - } - return PyLong_FromLong(sqlite3_total_changes(self->db)); -} - -static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused) -{ - if (!pysqlite_check_connection(self)) { - return NULL; - } - if (!sqlite3_get_autocommit(self->db)) { - Py_RETURN_TRUE; - } - Py_RETURN_FALSE; -} - -static int -pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored)) -{ - if (isolation_level == NULL) { - PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); - return -1; - } - if (Py_IsNone(isolation_level)) { - self->isolation_level = NULL; - - // Execute a COMMIT to re-enable autocommit mode - PyObject *res = pysqlite_connection_commit_impl(self); - if (res == NULL) { - return -1; - } - Py_DECREF(res); - return 0; - } - if (!isolation_level_converter(isolation_level, &self->isolation_level)) { - return -1; - } - return 0; -} - -static PyObject * -pysqlite_connection_call(pysqlite_Connection *self, PyObject *args, - PyObject *kwargs) -{ - PyObject* sql; - pysqlite_Statement* statement; - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs)) - return NULL; - - if (!PyArg_ParseTuple(args, "U", &sql)) - return NULL; - - statement = pysqlite_statement_create(self, sql); - if (statement == NULL) { - return NULL; - } - - return (PyObject*)statement; -} - -/*[clinic input] -_sqlite3.Connection.execute as pysqlite_connection_execute - - sql: unicode - parameters: object = NULL - / - -Executes an SQL statement. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql, - PyObject *parameters) -/*[clinic end generated code: output=5be05ae01ee17ee4 input=27aa7792681ddba2]*/ -{ - PyObject* result = 0; - - PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL); - if (!cursor) { - goto error; - } - - result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 0, sql, parameters); - if (!result) { - Py_CLEAR(cursor); - } - -error: - Py_XDECREF(result); - - return cursor; -} - -/*[clinic input] -_sqlite3.Connection.executemany as pysqlite_connection_executemany - - sql: unicode - parameters: object - / - -Repeatedly executes an SQL statement. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_executemany_impl(pysqlite_Connection *self, - PyObject *sql, PyObject *parameters) -/*[clinic end generated code: output=776cd2fd20bfe71f input=495be76551d525db]*/ -{ - PyObject* result = 0; - - PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL); - if (!cursor) { - goto error; - } - - result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 1, sql, parameters); - if (!result) { - Py_CLEAR(cursor); - } - -error: - Py_XDECREF(result); - - return cursor; -} - -/*[clinic input] -_sqlite3.Connection.executescript as pysqlite_connection_executescript - - sql_script as script_obj: object - / - -Executes multiple SQL statements at once. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_executescript(pysqlite_Connection *self, - PyObject *script_obj) -/*[clinic end generated code: output=4c4f9d77aa0ae37d input=f6e5f1ccfa313db4]*/ -{ - PyObject* result = 0; - - PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL); - if (!cursor) { - goto error; - } - - PyObject *meth = self->state->str_executescript; // borrowed ref. - result = PyObject_CallMethodObjArgs(cursor, meth, script_obj, NULL); - if (!result) { - Py_CLEAR(cursor); - } - -error: - Py_XDECREF(result); - - return cursor; -} - -/* ------------------------- COLLATION CODE ------------------------ */ - -static int -collation_callback(void *context, int text1_length, const void *text1_data, - int text2_length, const void *text2_data) -{ - PyGILState_STATE gilstate = PyGILState_Ensure(); - - PyObject* string1 = 0; - PyObject* string2 = 0; - PyObject* retval = NULL; - long longval; - int result = 0; - - /* This callback may be executed multiple times per sqlite3_step(). Bail if - * the previous call failed */ - if (PyErr_Occurred()) { - goto finally; - } - - string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length); - if (string1 == NULL) { - goto finally; - } - string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length); - if (string2 == NULL) { - goto finally; - } - - callback_context *ctx = (callback_context *)context; - assert(ctx != NULL); - PyObject *args[] = { NULL, string1, string2 }; // Borrowed refs. - size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET; - retval = PyObject_Vectorcall(ctx->callable, args + 1, nargsf, NULL); - if (retval == NULL) { - /* execution failed */ - goto finally; - } - - longval = PyLong_AsLongAndOverflow(retval, &result); - if (longval == -1 && PyErr_Occurred()) { - PyErr_Clear(); - result = 0; - } - else if (!result) { - if (longval > 0) - result = 1; - else if (longval < 0) - result = -1; - } - -finally: - Py_XDECREF(string1); - Py_XDECREF(string2); - Py_XDECREF(retval); - PyGILState_Release(gilstate); - return result; -} - -/*[clinic input] -_sqlite3.Connection.interrupt as pysqlite_connection_interrupt - -Abort any pending database operation. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_interrupt_impl(pysqlite_Connection *self) -/*[clinic end generated code: output=f193204bc9e70b47 input=75ad03ade7012859]*/ -{ - PyObject* retval = NULL; - - if (!pysqlite_check_connection(self)) { - goto finally; - } - - sqlite3_interrupt(self->db); - - retval = Py_NewRef(Py_None); - -finally: - return retval; -} - -/* Function author: Paul Kippes <[email protected]> - * Class method of Connection to call the Python function _iterdump - * of the sqlite3 module. - */ -/*[clinic input] -_sqlite3.Connection.iterdump as pysqlite_connection_iterdump - -Returns iterator to the dump of the database in an SQL text format. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_iterdump_impl(pysqlite_Connection *self) -/*[clinic end generated code: output=586997aaf9808768 input=1911ca756066da89]*/ -{ - if (!pysqlite_check_connection(self)) { - return NULL; - } - - PyObject *iterdump = _PyImport_GetModuleAttrString(MODULE_NAME ".dump", "_iterdump"); - if (!iterdump) { - if (!PyErr_Occurred()) { - PyErr_SetString(self->OperationalError, - "Failed to obtain _iterdump() reference"); - } - return NULL; - } - - PyObject *retval = PyObject_CallOneArg(iterdump, (PyObject *)self); - Py_DECREF(iterdump); - return retval; -} - -/*[clinic input] -_sqlite3.Connection.backup as pysqlite_connection_backup - - target: object(type='pysqlite_Connection *', subclass_of='clinic_state()->ConnectionType') - * - pages: int = -1 - progress: object = None - name: str = "main" - sleep: double = 0.250 - -Makes a backup of the database. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_backup_impl(pysqlite_Connection *self, - pysqlite_Connection *target, int pages, - PyObject *progress, const char *name, - double sleep) -/*[clinic end generated code: output=306a3e6a38c36334 input=c6519d0f59d0fd7f]*/ -{ - int rc; - int sleep_ms = (int)(sleep * 1000.0); - sqlite3 *bck_conn; - sqlite3_backup *bck_handle; - - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - if (!pysqlite_check_connection(target)) { - return NULL; - } - - if (target == self) { - PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance"); - return NULL; - } - -#if SQLITE_VERSION_NUMBER < 3008008 - /* Since 3.8.8 this is already done, per commit - https://www.sqlite.org/src/info/169b5505498c0a7e */ - if (!sqlite3_get_autocommit(target->db)) { - PyErr_SetString(self->OperationalError, "target is in transaction"); - return NULL; - } -#endif - - if (progress != Py_None && !PyCallable_Check(progress)) { - PyErr_SetString(PyExc_TypeError, "progress argument must be a callable"); - return NULL; - } - - if (pages == 0) { - pages = -1; - } - - bck_conn = target->db; - - Py_BEGIN_ALLOW_THREADS - bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name); - Py_END_ALLOW_THREADS - - if (bck_handle == NULL) { - _pysqlite_seterror(self->state, bck_conn); - return NULL; - } - - do { - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_backup_step(bck_handle, pages); - Py_END_ALLOW_THREADS - - if (progress != Py_None) { - int remaining = sqlite3_backup_remaining(bck_handle); - int pagecount = sqlite3_backup_pagecount(bck_handle); - PyObject *res = PyObject_CallFunction(progress, "iii", rc, - remaining, pagecount); - if (res == NULL) { - /* Callback failed: abort backup and bail. */ - Py_BEGIN_ALLOW_THREADS - sqlite3_backup_finish(bck_handle); - Py_END_ALLOW_THREADS - return NULL; - } - Py_DECREF(res); - } - - /* Sleep for a while if there are still further pages to copy and - the engine could not make any progress */ - if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) { - Py_BEGIN_ALLOW_THREADS - sqlite3_sleep(sleep_ms); - Py_END_ALLOW_THREADS - } - } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED); - - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_backup_finish(bck_handle); - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - _pysqlite_seterror(self->state, bck_conn); - return NULL; - } - - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.Connection.create_collation as pysqlite_connection_create_collation - - cls: defining_class - name: str - callback as callable: object - / - -Creates a collation function. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_create_collation_impl(pysqlite_Connection *self, - PyTypeObject *cls, - const char *name, - PyObject *callable) -/*[clinic end generated code: output=32d339e97869c378 input=f67ecd2e31e61ad3]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - callback_context *ctx = NULL; - int rc; - int flags = SQLITE_UTF8; - if (callable == Py_None) { - rc = sqlite3_create_collation_v2(self->db, name, flags, - NULL, NULL, NULL); - } - else { - if (!PyCallable_Check(callable)) { - PyErr_SetString(PyExc_TypeError, "parameter must be callable"); - return NULL; - } - ctx = create_callback_context(cls, callable); - if (ctx == NULL) { - return NULL; - } - rc = sqlite3_create_collation_v2(self->db, name, flags, ctx, - &collation_callback, - &destructor_callback); - } - - if (rc != SQLITE_OK) { - /* Unlike other sqlite3_* functions, the destructor callback is _not_ - * called if sqlite3_create_collation_v2() fails, so we have to free - * the context before returning. - */ - if (callable != Py_None) { - free_callback_context(ctx); - } - _pysqlite_seterror(self->state, self->db); - return NULL; - } - - Py_RETURN_NONE; -} - -#ifdef PY_SQLITE_HAVE_SERIALIZE -/*[clinic input] -_sqlite3.Connection.serialize as serialize - - * - name: str = "main" - Which database to serialize. - -Serialize a database into a byte string. - -For an ordinary on-disk database file, the serialization is just a copy of the -disk file. For an in-memory database or a "temp" database, the serialization is -the same sequence of bytes which would be written to disk if that database -were backed up to disk. -[clinic start generated code]*/ - -static PyObject * -serialize_impl(pysqlite_Connection *self, const char *name) -/*[clinic end generated code: output=97342b0e55239dd3 input=d2eb5194a65abe2b]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - /* If SQLite has a contiguous memory representation of the database, we can - * avoid memory allocations, so we try with the no-copy flag first. - */ - sqlite3_int64 size; - unsigned int flags = SQLITE_SERIALIZE_NOCOPY; - const char *data; - - Py_BEGIN_ALLOW_THREADS - data = (const char *)sqlite3_serialize(self->db, name, &size, flags); - if (data == NULL) { - flags &= ~SQLITE_SERIALIZE_NOCOPY; - data = (const char *)sqlite3_serialize(self->db, name, &size, flags); - } - Py_END_ALLOW_THREADS - - if (data == NULL) { - PyErr_Format(self->OperationalError, "unable to serialize '%s'", - name); - return NULL; - } - PyObject *res = PyBytes_FromStringAndSize(data, (Py_ssize_t)size); - if (!(flags & SQLITE_SERIALIZE_NOCOPY)) { - sqlite3_free((void *)data); - } - return res; -} - -/*[clinic input] -_sqlite3.Connection.deserialize as deserialize - - data: Py_buffer(accept={buffer, str}) - The serialized database content. - / - * - name: str = "main" - Which database to reopen with the deserialization. - -Load a serialized database. - -The deserialize interface causes the database connection to disconnect from the -target database, and then reopen it as an in-memory database based on the given -serialized data. - -The deserialize interface will fail with SQLITE_BUSY if the database is -currently in a read transaction or is involved in a backup operation. -[clinic start generated code]*/ - -static PyObject * -deserialize_impl(pysqlite_Connection *self, Py_buffer *data, - const char *name) -/*[clinic end generated code: output=e394c798b98bad89 input=1be4ca1faacf28f2]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - /* Transfer ownership of the buffer to SQLite: - * - Move buffer from Py to SQLite - * - Tell SQLite to free buffer memory - * - Tell SQLite that it is permitted to grow the resulting database - * - * Make sure we don't overflow sqlite3_deserialize(); it accepts a signed - * 64-bit int as its data size argument. - * - * We can safely use sqlite3_malloc64 here, since it was introduced before - * the serialize APIs. - */ - if (data->len > 9223372036854775807) { // (1 << 63) - 1 - PyErr_SetString(PyExc_OverflowError, "'data' is too large"); - return NULL; - } - - sqlite3_int64 size = (sqlite3_int64)data->len; - unsigned char *buf = sqlite3_malloc64(size); - if (buf == NULL) { - return PyErr_NoMemory(); - } - - const unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE | - SQLITE_DESERIALIZE_RESIZEABLE; - int rc; - Py_BEGIN_ALLOW_THREADS - (void)memcpy(buf, data->buf, data->len); - rc = sqlite3_deserialize(self->db, name, buf, size, size, flags); - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - (void)_pysqlite_seterror(self->state, self->db); - return NULL; - } - Py_RETURN_NONE; -} -#endif // PY_SQLITE_HAVE_SERIALIZE - - -/*[clinic input] -_sqlite3.Connection.__enter__ as pysqlite_connection_enter - -Called when the connection is used as a context manager. - -Returns itself as a convenience to the caller. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_enter_impl(pysqlite_Connection *self) -/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/ -{ - if (!pysqlite_check_connection(self)) { - return NULL; - } - return Py_NewRef((PyObject *)self); -} - -/*[clinic input] -_sqlite3.Connection.__exit__ as pysqlite_connection_exit - - type as exc_type: object - value as exc_value: object - traceback as exc_tb: object - / - -Called when the connection is used as a context manager. - -If there was any exception, a rollback takes place; otherwise we commit. -[clinic start generated code]*/ - -static PyObject * -pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type, - PyObject *exc_value, PyObject *exc_tb) -/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/ -{ - int commit = 0; - PyObject* result; - - if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) { - commit = 1; - result = pysqlite_connection_commit_impl(self); - } - else { - result = pysqlite_connection_rollback_impl(self); - } - - if (result == NULL) { - if (commit) { - /* Commit failed; try to rollback in order to unlock the database. - * If rollback also fails, chain the exceptions. */ - PyObject *exc = PyErr_GetRaisedException(); - result = pysqlite_connection_rollback_impl(self); - if (result == NULL) { - _PyErr_ChainExceptions1(exc); - } - else { - Py_DECREF(result); - PyErr_SetRaisedException(exc); - } - } - return NULL; - } - Py_DECREF(result); - - Py_RETURN_FALSE; -} - -/*[clinic input] -_sqlite3.Connection.setlimit as setlimit - - category: int - The limit category to be set. - limit: int - The new limit. If the new limit is a negative number, the limit is - unchanged. - / - -Set connection run-time limits. - -Attempts to increase a limit above its hard upper bound are silently truncated -to the hard upper bound. Regardless of whether or not the limit was changed, -the prior value of the limit is returned. -[clinic start generated code]*/ - -static PyObject * -setlimit_impl(pysqlite_Connection *self, int category, int limit) -/*[clinic end generated code: output=0d208213f8d68ccd input=9bd469537e195635]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - - int old_limit = sqlite3_limit(self->db, category, limit); - if (old_limit < 0) { - PyErr_SetString(self->ProgrammingError, "'category' is out of bounds"); - return NULL; - } - return PyLong_FromLong(old_limit); -} - -/*[clinic input] -_sqlite3.Connection.getlimit as getlimit - - category: int - The limit category to be queried. - / - -Get connection run-time limits. -[clinic start generated code]*/ - -static PyObject * -getlimit_impl(pysqlite_Connection *self, int category) -/*[clinic end generated code: output=7c3f5d11f24cecb1 input=61e0849fb4fb058f]*/ -{ - return setlimit_impl(self, category, -1); -} - -static inline bool -is_int_config(const int op) -{ - switch (op) { - case SQLITE_DBCONFIG_ENABLE_FKEY: - case SQLITE_DBCONFIG_ENABLE_TRIGGER: -#if SQLITE_VERSION_NUMBER >= 3012002 - case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER: -#endif -#if SQLITE_VERSION_NUMBER >= 3013000 - case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION: -#endif -#if SQLITE_VERSION_NUMBER >= 3016000 - case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE: -#endif -#if SQLITE_VERSION_NUMBER >= 3020000 - case SQLITE_DBCONFIG_ENABLE_QPSG: -#endif -#if SQLITE_VERSION_NUMBER >= 3022000 - case SQLITE_DBCONFIG_TRIGGER_EQP: -#endif -#if SQLITE_VERSION_NUMBER >= 3024000 - case SQLITE_DBCONFIG_RESET_DATABASE: -#endif -#if SQLITE_VERSION_NUMBER >= 3026000 - case SQLITE_DBCONFIG_DEFENSIVE: -#endif -#if SQLITE_VERSION_NUMBER >= 3028000 - case SQLITE_DBCONFIG_WRITABLE_SCHEMA: -#endif -#if SQLITE_VERSION_NUMBER >= 3029000 - case SQLITE_DBCONFIG_DQS_DDL: - case SQLITE_DBCONFIG_DQS_DML: - case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE: -#endif -#if SQLITE_VERSION_NUMBER >= 3030000 - case SQLITE_DBCONFIG_ENABLE_VIEW: -#endif -#if SQLITE_VERSION_NUMBER >= 3031000 - case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT: - case SQLITE_DBCONFIG_TRUSTED_SCHEMA: -#endif - return true; - default: - return false; - } -} - -/*[clinic input] -_sqlite3.Connection.setconfig as setconfig - - op: int - The configuration verb; one of the sqlite3.SQLITE_DBCONFIG codes. - enable: bool = True - / - -Set a boolean connection configuration option. -[clinic start generated code]*/ - -static PyObject * -setconfig_impl(pysqlite_Connection *self, int op, int enable) -/*[clinic end generated code: output=c60b13e618aff873 input=a10f1539c2d7da6b]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - if (!is_int_config(op)) { - return PyErr_Format(PyExc_ValueError, "unknown config 'op': %d", op); - } - - int actual; - int rc = sqlite3_db_config(self->db, op, enable, &actual); - if (rc != SQLITE_OK) { - (void)_pysqlite_seterror(self->state, self->db); - return NULL; - } - if (enable != actual) { - PyErr_SetString(self->state->OperationalError, "Unable to set config"); - return NULL; - } - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.Connection.getconfig as getconfig -> bool - - op: int - The configuration verb; one of the sqlite3.SQLITE_DBCONFIG codes. - / - -Query a boolean connection configuration option. -[clinic start generated code]*/ - -static int -getconfig_impl(pysqlite_Connection *self, int op) -/*[clinic end generated code: output=25ac05044c7b78a3 input=b0526d7e432e3f2f]*/ -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return -1; - } - if (!is_int_config(op)) { - PyErr_Format(PyExc_ValueError, "unknown config 'op': %d", op); - return -1; - } - - int current; - int rc = sqlite3_db_config(self->db, op, -1, ¤t); - if (rc != SQLITE_OK) { - (void)_pysqlite_seterror(self->state, self->db); - return -1; - } - return current; -} - -static PyObject * -get_autocommit(pysqlite_Connection *self, void *Py_UNUSED(ctx)) -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return NULL; - } - if (self->autocommit == AUTOCOMMIT_ENABLED) { - Py_RETURN_TRUE; - } - if (self->autocommit == AUTOCOMMIT_DISABLED) { - Py_RETURN_FALSE; - } - return PyLong_FromLong(LEGACY_TRANSACTION_CONTROL); -} - -static int -set_autocommit(pysqlite_Connection *self, PyObject *val, void *Py_UNUSED(ctx)) -{ - if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { - return -1; - } - if (!autocommit_converter(val, &self->autocommit)) { - return -1; - } - if (self->autocommit == AUTOCOMMIT_ENABLED) { - if (!sqlite3_get_autocommit(self->db)) { - if (connection_exec_stmt(self, "COMMIT") < 0) { - return -1; - } - } - } - else if (self->autocommit == AUTOCOMMIT_DISABLED) { - if (sqlite3_get_autocommit(self->db)) { - if (connection_exec_stmt(self, "BEGIN") < 0) { - return -1; - } - } - } - return 0; -} - - -static const char connection_doc[] = -PyDoc_STR("SQLite database connection object."); - -static PyGetSetDef connection_getset[] = { - {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level}, - {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0}, - {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0}, - {"autocommit", (getter)get_autocommit, (setter)set_autocommit}, - {NULL} -}; - -static PyMethodDef connection_methods[] = { - PYSQLITE_CONNECTION_BACKUP_METHODDEF - PYSQLITE_CONNECTION_CLOSE_METHODDEF - PYSQLITE_CONNECTION_COMMIT_METHODDEF - PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF - PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF - PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF - PYSQLITE_CONNECTION_CURSOR_METHODDEF - PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF - PYSQLITE_CONNECTION_ENTER_METHODDEF - PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF - PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF - PYSQLITE_CONNECTION_EXECUTE_METHODDEF - PYSQLITE_CONNECTION_EXIT_METHODDEF - PYSQLITE_CONNECTION_INTERRUPT_METHODDEF - PYSQLITE_CONNECTION_ITERDUMP_METHODDEF - PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF - PYSQLITE_CONNECTION_ROLLBACK_METHODDEF - PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF - PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF - PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF - SETLIMIT_METHODDEF - GETLIMIT_METHODDEF - SERIALIZE_METHODDEF - DESERIALIZE_METHODDEF - CREATE_WINDOW_FUNCTION_METHODDEF - BLOBOPEN_METHODDEF - SETCONFIG_METHODDEF - GETCONFIG_METHODDEF - {NULL, NULL} -}; - -static struct PyMemberDef connection_members[] = -{ - {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY}, - {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY}, - {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY}, - {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY}, - {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY}, - {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY}, - {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY}, - {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY}, - {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY}, - {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY}, - {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)}, - {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)}, - {NULL} -}; - -static PyType_Slot connection_slots[] = { - {Py_tp_finalize, connection_finalize}, - {Py_tp_dealloc, connection_dealloc}, - {Py_tp_doc, (void *)connection_doc}, - {Py_tp_methods, connection_methods}, - {Py_tp_members, connection_members}, - {Py_tp_getset, connection_getset}, - {Py_tp_init, pysqlite_connection_init}, - {Py_tp_call, pysqlite_connection_call}, - {Py_tp_traverse, connection_traverse}, - {Py_tp_clear, connection_clear}, - {0, NULL}, -}; - -static PyType_Spec connection_spec = { - .name = MODULE_NAME ".Connection", - .basicsize = sizeof(pysqlite_Connection), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE), - .slots = connection_slots, -}; - -int -pysqlite_connection_setup_types(PyObject *module) -{ - PyObject *type = PyType_FromModuleAndSpec(module, &connection_spec, NULL); - if (type == NULL) { - return -1; - } - pysqlite_state *state = pysqlite_get_state(module); - state->ConnectionType = (PyTypeObject *)type; - return 0; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/connection.h b/contrib/tools/python3/src/Modules/_sqlite/connection.h deleted file mode 100644 index 1df92065a58..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/connection.h +++ /dev/null @@ -1,114 +0,0 @@ -/* connection.h - definitions for the connection type - * - * Copyright (C) 2004-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PYSQLITE_CONNECTION_H -#define PYSQLITE_CONNECTION_H -#define PY_SSIZE_T_CLEAN -#include "Python.h" -#include "pythread.h" -#include "structmember.h" - -#include "module.h" - -#include "sqlite3.h" - -typedef struct _callback_context -{ - PyObject *callable; - PyObject *module; - pysqlite_state *state; -} callback_context; - -enum autocommit_mode { - AUTOCOMMIT_LEGACY = LEGACY_TRANSACTION_CONTROL, - AUTOCOMMIT_ENABLED = 1, - AUTOCOMMIT_DISABLED = 0, -}; - -typedef struct -{ - PyObject_HEAD - sqlite3 *db; - pysqlite_state *state; - - /* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a - * bitwise combination thereof makes sense */ - int detect_types; - - /* NULL for autocommit, otherwise a string with the isolation level */ - const char *isolation_level; - enum autocommit_mode autocommit; - - /* 1 if a check should be performed for each API call if the connection is - * used from the same thread it was created in */ - int check_same_thread; - - int initialized; - - /* thread identification of the thread the connection was created in */ - unsigned long thread_ident; - - PyObject *statement_cache; - - /* Lists of weak references to cursors and blobs used within this connection */ - PyObject *cursors; - PyObject *blobs; - - /* Counters for how many cursors were created in the connection. May be - * reset to 0 at certain intervals */ - int created_cursors; - - PyObject* row_factory; - - /* Determines how bytestrings from SQLite are converted to Python objects: - * - PyUnicode_Type: Python Unicode objects are constructed from UTF-8 bytestrings - * - PyBytes_Type: The bytestrings are returned as-is. - * - Any custom callable: Any object returned from the callable called with the bytestring - * as single parameter. - */ - PyObject* text_factory; - - // Remember contexts used by the trace, progress, and authoriser callbacks - callback_context *trace_ctx; - callback_context *progress_ctx; - callback_context *authorizer_ctx; - - /* Exception objects: borrowed refs. */ - PyObject* Warning; - PyObject* Error; - PyObject* InterfaceError; - PyObject* DatabaseError; - PyObject* DataError; - PyObject* OperationalError; - PyObject* IntegrityError; - PyObject* InternalError; - PyObject* ProgrammingError; - PyObject* NotSupportedError; -} pysqlite_Connection; - -int pysqlite_check_thread(pysqlite_Connection* self); -int pysqlite_check_connection(pysqlite_Connection* con); - -int pysqlite_connection_setup_types(PyObject *module); - -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/cursor.c b/contrib/tools/python3/src/Modules/_sqlite/cursor.c deleted file mode 100644 index caeedbddb8d..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/cursor.c +++ /dev/null @@ -1,1366 +0,0 @@ -/* cursor.c - the cursor type - * - * Copyright (C) 2004-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include "cursor.h" -#include "microprotocols.h" -#include "module.h" -#include "util.h" - -typedef enum { - TYPE_LONG, - TYPE_FLOAT, - TYPE_UNICODE, - TYPE_BUFFER, - TYPE_UNKNOWN -} parameter_type; - -#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self))) -#include "clinic/cursor.c.h" -#undef clinic_state - -static inline int -check_cursor_locked(pysqlite_Cursor *cur) -{ - if (cur->locked) { - PyErr_SetString(cur->connection->ProgrammingError, - "Recursive use of cursors not allowed."); - return 0; - } - return 1; -} - -/*[clinic input] -module _sqlite3 -class _sqlite3.Cursor "pysqlite_Cursor *" "clinic_state()->CursorType" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c5b8115c5cf30f1]*/ - -/* - * Registers a cursor with the connection. - * - * 0 => error; 1 => ok - */ -static int -register_cursor(pysqlite_Connection *connection, PyObject *cursor) -{ - PyObject *weakref = PyWeakref_NewRef((PyObject *)cursor, NULL); - if (weakref == NULL) { - return 0; - } - - if (PyList_Append(connection->cursors, weakref) < 0) { - Py_CLEAR(weakref); - return 0; - } - - Py_DECREF(weakref); - return 1; -} - -/*[clinic input] -_sqlite3.Cursor.__init__ as pysqlite_cursor_init - - connection: object(type='pysqlite_Connection *', subclass_of='clinic_state()->ConnectionType') - / - -[clinic start generated code]*/ - -static int -pysqlite_cursor_init_impl(pysqlite_Cursor *self, - pysqlite_Connection *connection) -/*[clinic end generated code: output=ac59dce49a809ca8 input=23d4265b534989fb]*/ -{ - if (!check_cursor_locked(self)) { - return -1; - } - - Py_INCREF(connection); - Py_XSETREF(self->connection, connection); - Py_CLEAR(self->statement); - Py_CLEAR(self->row_cast_map); - - Py_INCREF(Py_None); - Py_XSETREF(self->description, Py_None); - - Py_INCREF(Py_None); - Py_XSETREF(self->lastrowid, Py_None); - - self->arraysize = 1; - self->closed = 0; - self->rowcount = -1L; - - Py_INCREF(Py_None); - Py_XSETREF(self->row_factory, Py_None); - - if (!pysqlite_check_thread(self->connection)) { - return -1; - } - - if (!register_cursor(connection, (PyObject *)self)) { - return -1; - } - - self->initialized = 1; - - return 0; -} - -static inline int -stmt_reset(pysqlite_Statement *self) -{ - int rc = SQLITE_OK; - - if (self->st != NULL) { - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_reset(self->st); - Py_END_ALLOW_THREADS - } - - return rc; -} - -static int -cursor_traverse(pysqlite_Cursor *self, visitproc visit, void *arg) -{ - Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->connection); - Py_VISIT(self->description); - Py_VISIT(self->row_cast_map); - Py_VISIT(self->lastrowid); - Py_VISIT(self->row_factory); - Py_VISIT(self->statement); - return 0; -} - -static int -cursor_clear(pysqlite_Cursor *self) -{ - Py_CLEAR(self->connection); - Py_CLEAR(self->description); - Py_CLEAR(self->row_cast_map); - Py_CLEAR(self->lastrowid); - Py_CLEAR(self->row_factory); - if (self->statement) { - /* Reset the statement if the user has not closed the cursor */ - stmt_reset(self->statement); - Py_CLEAR(self->statement); - } - - return 0; -} - -static void -cursor_dealloc(pysqlite_Cursor *self) -{ - PyTypeObject *tp = Py_TYPE(self); - PyObject_GC_UnTrack(self); - if (self->in_weakreflist != NULL) { - PyObject_ClearWeakRefs((PyObject*)self); - } - tp->tp_clear((PyObject *)self); - tp->tp_free(self); - Py_DECREF(tp); -} - -static PyObject * -_pysqlite_get_converter(pysqlite_state *state, const char *keystr, - Py_ssize_t keylen) -{ - PyObject *key; - PyObject *upcase_key; - PyObject *retval; - - key = PyUnicode_FromStringAndSize(keystr, keylen); - if (!key) { - return NULL; - } - upcase_key = PyObject_CallMethodNoArgs(key, state->str_upper); - Py_DECREF(key); - if (!upcase_key) { - return NULL; - } - - retval = PyDict_GetItemWithError(state->converters, upcase_key); - Py_DECREF(upcase_key); - - return retval; -} - -static int -pysqlite_build_row_cast_map(pysqlite_Cursor* self) -{ - int i; - const char* pos; - const char* decltype; - PyObject* converter; - - if (!self->connection->detect_types) { - return 0; - } - - Py_XSETREF(self->row_cast_map, PyList_New(0)); - if (!self->row_cast_map) { - return -1; - } - - for (i = 0; i < sqlite3_column_count(self->statement->st); i++) { - converter = NULL; - - if (self->connection->detect_types & PARSE_COLNAMES) { - const char *colname = sqlite3_column_name(self->statement->st, i); - if (colname == NULL) { - PyErr_NoMemory(); - Py_CLEAR(self->row_cast_map); - return -1; - } - const char *type_start = NULL; - for (pos = colname; *pos != 0; pos++) { - if (*pos == '[') { - type_start = pos + 1; - } - else if (*pos == ']' && type_start != NULL) { - pysqlite_state *state = self->connection->state; - converter = _pysqlite_get_converter(state, type_start, - pos - type_start); - if (!converter && PyErr_Occurred()) { - Py_CLEAR(self->row_cast_map); - return -1; - } - break; - } - } - } - - if (!converter && self->connection->detect_types & PARSE_DECLTYPES) { - decltype = sqlite3_column_decltype(self->statement->st, i); - if (decltype) { - for (pos = decltype;;pos++) { - /* Converter names are split at '(' and blanks. - * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and - * 'NUMBER(10)' to be treated as 'NUMBER', for example. - * In other words, it will work as people expect it to work.*/ - if (*pos == ' ' || *pos == '(' || *pos == 0) { - pysqlite_state *state = self->connection->state; - converter = _pysqlite_get_converter(state, decltype, - pos - decltype); - if (!converter && PyErr_Occurred()) { - Py_CLEAR(self->row_cast_map); - return -1; - } - break; - } - } - } - } - - if (!converter) { - converter = Py_None; - } - - if (PyList_Append(self->row_cast_map, converter) != 0) { - Py_CLEAR(self->row_cast_map); - return -1; - } - } - - return 0; -} - -static PyObject * -_pysqlite_build_column_name(pysqlite_Cursor *self, const char *colname) -{ - const char* pos; - Py_ssize_t len; - - if (self->connection->detect_types & PARSE_COLNAMES) { - for (pos = colname; *pos; pos++) { - if (*pos == '[') { - if ((pos != colname) && (*(pos-1) == ' ')) { - pos--; - } - break; - } - } - len = pos - colname; - } - else { - len = strlen(colname); - } - return PyUnicode_FromStringAndSize(colname, len); -} - -/* - * Returns a row from the currently active SQLite statement - * - * Precondidition: - * - sqlite3_step() has been called before and it returned SQLITE_ROW. - */ -static PyObject * -_pysqlite_fetch_one_row(pysqlite_Cursor* self) -{ - int i, numcols; - PyObject* row; - int coltype; - PyObject* converter; - PyObject* converted; - Py_ssize_t nbytes; - char buf[200]; - const char* colname; - PyObject* error_msg; - - Py_BEGIN_ALLOW_THREADS - numcols = sqlite3_data_count(self->statement->st); - Py_END_ALLOW_THREADS - - row = PyTuple_New(numcols); - if (!row) - return NULL; - - sqlite3 *db = self->connection->db; - for (i = 0; i < numcols; i++) { - if (self->connection->detect_types - && self->row_cast_map != NULL - && i < PyList_GET_SIZE(self->row_cast_map)) - { - converter = PyList_GET_ITEM(self->row_cast_map, i); - } - else { - converter = Py_None; - } - - /* - * Note, sqlite3_column_bytes() must come after sqlite3_column_blob() - * or sqlite3_column_text(). - * - * See https://sqlite.org/c3ref/column_blob.html for details. - */ - if (converter != Py_None) { - const void *blob = sqlite3_column_blob(self->statement->st, i); - if (blob == NULL) { - if (sqlite3_errcode(db) == SQLITE_NOMEM) { - PyErr_NoMemory(); - goto error; - } - converted = Py_NewRef(Py_None); - } - else { - nbytes = sqlite3_column_bytes(self->statement->st, i); - PyObject *item = PyBytes_FromStringAndSize(blob, nbytes); - if (item == NULL) { - goto error; - } - converted = PyObject_CallOneArg(converter, item); - Py_DECREF(item); - } - } else { - Py_BEGIN_ALLOW_THREADS - coltype = sqlite3_column_type(self->statement->st, i); - Py_END_ALLOW_THREADS - if (coltype == SQLITE_NULL) { - converted = Py_NewRef(Py_None); - } else if (coltype == SQLITE_INTEGER) { - converted = PyLong_FromLongLong(sqlite3_column_int64(self->statement->st, i)); - } else if (coltype == SQLITE_FLOAT) { - converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i)); - } else if (coltype == SQLITE_TEXT) { - const char *text = (const char*)sqlite3_column_text(self->statement->st, i); - if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) { - PyErr_NoMemory(); - goto error; - } - - nbytes = sqlite3_column_bytes(self->statement->st, i); - if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) { - converted = PyUnicode_FromStringAndSize(text, nbytes); - if (!converted && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { - PyErr_Clear(); - colname = sqlite3_column_name(self->statement->st, i); - if (colname == NULL) { - PyErr_NoMemory(); - goto error; - } - PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'", - colname , text); - error_msg = PyUnicode_Decode(buf, strlen(buf), "ascii", "replace"); - - PyObject *exc = self->connection->OperationalError; - if (!error_msg) { - PyErr_SetString(exc, "Could not decode to UTF-8"); - } else { - PyErr_SetObject(exc, error_msg); - Py_DECREF(error_msg); - } - } - } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) { - converted = PyBytes_FromStringAndSize(text, nbytes); - } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) { - converted = PyByteArray_FromStringAndSize(text, nbytes); - } else { - converted = PyObject_CallFunction(self->connection->text_factory, "y#", text, nbytes); - } - } else { - /* coltype == SQLITE_BLOB */ - const void *blob = sqlite3_column_blob(self->statement->st, i); - if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) { - PyErr_NoMemory(); - goto error; - } - - nbytes = sqlite3_column_bytes(self->statement->st, i); - converted = PyBytes_FromStringAndSize(blob, nbytes); - } - } - - if (!converted) { - goto error; - } - PyTuple_SET_ITEM(row, i, converted); - } - - if (PyErr_Occurred()) - goto error; - - return row; - -error: - Py_DECREF(row); - return NULL; -} - -/* - * Checks if a cursor object is usable. - * - * 0 => error; 1 => ok - */ -static int check_cursor(pysqlite_Cursor* cur) -{ - if (!cur->initialized) { - pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(cur)); - PyErr_SetString(state->ProgrammingError, - "Base Cursor.__init__ not called."); - return 0; - } - - if (cur->closed) { - PyErr_SetString(cur->connection->state->ProgrammingError, - "Cannot operate on a closed cursor."); - return 0; - } - - return (pysqlite_check_thread(cur->connection) - && pysqlite_check_connection(cur->connection) - && check_cursor_locked(cur)); -} - -static int -begin_transaction(pysqlite_Connection *self) -{ - assert(self->isolation_level != NULL); - int rc; - - Py_BEGIN_ALLOW_THREADS - sqlite3_stmt *statement; - char begin_stmt[16] = "BEGIN "; -#ifdef Py_DEBUG - size_t len = strlen(self->isolation_level); - assert(len <= 9); -#endif - (void)strcat(begin_stmt, self->isolation_level); - rc = sqlite3_prepare_v2(self->db, begin_stmt, -1, &statement, NULL); - if (rc == SQLITE_OK) { - (void)sqlite3_step(statement); - rc = sqlite3_finalize(statement); - } - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - (void)_pysqlite_seterror(self->state, self->db); - return -1; - } - - return 0; -} - -static PyObject * -get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation) -{ - PyObject *args[] = { NULL, operation, }; // Borrowed ref. - PyObject *cache = self->connection->statement_cache; - size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; - return PyObject_Vectorcall(cache, args + 1, nargsf, NULL); -} - -static inline int -stmt_step(sqlite3_stmt *statement) -{ - int rc; - - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_step(statement); - Py_END_ALLOW_THREADS - - return rc; -} - -static int -bind_param(pysqlite_state *state, pysqlite_Statement *self, int pos, - PyObject *parameter) -{ - int rc = SQLITE_OK; - const char *string; - Py_ssize_t buflen; - parameter_type paramtype; - - if (parameter == Py_None) { - rc = sqlite3_bind_null(self->st, pos); - goto final; - } - - if (PyLong_CheckExact(parameter)) { - paramtype = TYPE_LONG; - } else if (PyFloat_CheckExact(parameter)) { - paramtype = TYPE_FLOAT; - } else if (PyUnicode_CheckExact(parameter)) { - paramtype = TYPE_UNICODE; - } else if (PyLong_Check(parameter)) { - paramtype = TYPE_LONG; - } else if (PyFloat_Check(parameter)) { - paramtype = TYPE_FLOAT; - } else if (PyUnicode_Check(parameter)) { - paramtype = TYPE_UNICODE; - } else if (PyObject_CheckBuffer(parameter)) { - paramtype = TYPE_BUFFER; - } else { - paramtype = TYPE_UNKNOWN; - } - - switch (paramtype) { - case TYPE_LONG: { - sqlite_int64 value = _pysqlite_long_as_int64(parameter); - if (value == -1 && PyErr_Occurred()) - rc = -1; - else - rc = sqlite3_bind_int64(self->st, pos, value); - break; - } - case TYPE_FLOAT: { - double value = PyFloat_AsDouble(parameter); - if (value == -1 && PyErr_Occurred()) { - rc = -1; - } - else { - rc = sqlite3_bind_double(self->st, pos, value); - } - break; - } - case TYPE_UNICODE: - string = PyUnicode_AsUTF8AndSize(parameter, &buflen); - if (string == NULL) - return -1; - if (buflen > INT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "string longer than INT_MAX bytes"); - return -1; - } - rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT); - break; - case TYPE_BUFFER: { - Py_buffer view; - if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) { - return -1; - } - if (view.len > INT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "BLOB longer than INT_MAX bytes"); - PyBuffer_Release(&view); - return -1; - } - rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT); - PyBuffer_Release(&view); - break; - } - case TYPE_UNKNOWN: - PyErr_Format(state->ProgrammingError, - "Error binding parameter %d: type '%s' is not supported", - pos, Py_TYPE(parameter)->tp_name); - rc = -1; - } - -final: - return rc; -} - -/* returns 0 if the object is one of Python's internal ones that don't need to be adapted */ -static inline int -need_adapt(pysqlite_state *state, PyObject *obj) -{ - if (state->BaseTypeAdapted) { - return 1; - } - - if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj) - || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) { - return 0; - } else { - return 1; - } -} - -static void -bind_parameters(pysqlite_state *state, pysqlite_Statement *self, - PyObject *parameters) -{ - PyObject* current_param; - PyObject* adapted; - const char* binding_name; - int i; - int rc; - int num_params_needed; - Py_ssize_t num_params; - - Py_BEGIN_ALLOW_THREADS - num_params_needed = sqlite3_bind_parameter_count(self->st); - Py_END_ALLOW_THREADS - - if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) { - /* parameters passed as sequence */ - if (PyTuple_CheckExact(parameters)) { - num_params = PyTuple_GET_SIZE(parameters); - } else if (PyList_CheckExact(parameters)) { - num_params = PyList_GET_SIZE(parameters); - } else { - num_params = PySequence_Size(parameters); - if (num_params == -1) { - return; - } - } - if (num_params != num_params_needed) { - PyErr_Format(state->ProgrammingError, - "Incorrect number of bindings supplied. The current " - "statement uses %d, and there are %zd supplied.", - num_params_needed, num_params); - return; - } - for (i = 0; i < num_params; i++) { - const char *name = sqlite3_bind_parameter_name(self->st, i+1); - if (name != NULL) { - int ret = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, - "Binding %d ('%s') is a named parameter, but you " - "supplied a sequence which requires nameless (qmark) " - "placeholders. Starting with Python 3.14 an " - "sqlite3.ProgrammingError will be raised.", - i+1, name); - if (ret < 0) { - return; - } - } - - if (PyTuple_CheckExact(parameters)) { - PyObject *item = PyTuple_GET_ITEM(parameters, i); - current_param = Py_NewRef(item); - } else if (PyList_CheckExact(parameters)) { - PyObject *item = PyList_GetItem(parameters, i); - current_param = Py_XNewRef(item); - } else { - current_param = PySequence_GetItem(parameters, i); - } - if (!current_param) { - return; - } - - if (!need_adapt(state, current_param)) { - adapted = current_param; - } else { - PyObject *protocol = (PyObject *)state->PrepareProtocolType; - adapted = pysqlite_microprotocols_adapt(state, current_param, - protocol, - current_param); - Py_DECREF(current_param); - if (!adapted) { - return; - } - } - - rc = bind_param(state, self, i + 1, adapted); - Py_DECREF(adapted); - - if (rc != SQLITE_OK) { - PyObject *exc = PyErr_GetRaisedException(); - sqlite3 *db = sqlite3_db_handle(self->st); - _pysqlite_seterror(state, db); - _PyErr_ChainExceptions1(exc); - return; - } - } - } else if (PyDict_Check(parameters)) { - /* parameters passed as dictionary */ - for (i = 1; i <= num_params_needed; i++) { - PyObject *binding_name_obj; - Py_BEGIN_ALLOW_THREADS - binding_name = sqlite3_bind_parameter_name(self->st, i); - Py_END_ALLOW_THREADS - if (!binding_name) { - PyErr_Format(state->ProgrammingError, - "Binding %d has no name, but you supplied a " - "dictionary (which has only names).", i); - return; - } - - binding_name++; /* skip first char (the colon) */ - binding_name_obj = PyUnicode_FromString(binding_name); - if (!binding_name_obj) { - return; - } - if (PyDict_CheckExact(parameters)) { - PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj); - current_param = Py_XNewRef(item); - } else { - current_param = PyObject_GetItem(parameters, binding_name_obj); - } - Py_DECREF(binding_name_obj); - if (!current_param) { - if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_LookupError)) { - PyErr_Format(state->ProgrammingError, - "You did not supply a value for binding " - "parameter :%s.", binding_name); - } - return; - } - - if (!need_adapt(state, current_param)) { - adapted = current_param; - } else { - PyObject *protocol = (PyObject *)state->PrepareProtocolType; - adapted = pysqlite_microprotocols_adapt(state, current_param, - protocol, - current_param); - Py_DECREF(current_param); - if (!adapted) { - return; - } - } - - rc = bind_param(state, self, i, adapted); - Py_DECREF(adapted); - - if (rc != SQLITE_OK) { - PyObject *exc = PyErr_GetRaisedException(); - sqlite3 *db = sqlite3_db_handle(self->st); - _pysqlite_seterror(state, db); - _PyErr_ChainExceptions1(exc); - return; - } - } - } else { - PyErr_SetString(state->ProgrammingError, - "parameters are of unsupported type"); - } -} - -PyObject * -_pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument) -{ - PyObject* parameters_list = NULL; - PyObject* parameters_iter = NULL; - PyObject* parameters = NULL; - int i; - int rc; - int numcols; - PyObject* column_name; - - if (!check_cursor(self)) { - goto error; - } - - self->locked = 1; - - if (multiple) { - if (PyIter_Check(second_argument)) { - /* iterator */ - parameters_iter = Py_NewRef(second_argument); - } else { - /* sequence */ - parameters_iter = PyObject_GetIter(second_argument); - if (!parameters_iter) { - goto error; - } - } - } else { - parameters_list = PyList_New(0); - if (!parameters_list) { - goto error; - } - - if (second_argument == NULL) { - second_argument = PyTuple_New(0); - if (!second_argument) { - goto error; - } - } else { - Py_INCREF(second_argument); - } - if (PyList_Append(parameters_list, second_argument) != 0) { - Py_DECREF(second_argument); - goto error; - } - Py_DECREF(second_argument); - - parameters_iter = PyObject_GetIter(parameters_list); - if (!parameters_iter) { - goto error; - } - } - - /* reset description */ - Py_INCREF(Py_None); - Py_SETREF(self->description, Py_None); - - if (self->statement) { - // Reset pending statements on this cursor. - (void)stmt_reset(self->statement); - } - - PyObject *stmt = get_statement_from_cache(self, operation); - Py_XSETREF(self->statement, (pysqlite_Statement *)stmt); - if (!self->statement) { - goto error; - } - - pysqlite_state *state = self->connection->state; - if (multiple && sqlite3_stmt_readonly(self->statement->st)) { - PyErr_SetString(state->ProgrammingError, - "executemany() can only execute DML statements."); - goto error; - } - - if (sqlite3_stmt_busy(self->statement->st)) { - Py_SETREF(self->statement, - pysqlite_statement_create(self->connection, operation)); - if (self->statement == NULL) { - goto error; - } - } - - (void)stmt_reset(self->statement); - self->rowcount = self->statement->is_dml ? 0L : -1L; - - /* We start a transaction implicitly before a DML statement. - SELECT is the only exception. See #9924. */ - if (self->connection->autocommit == AUTOCOMMIT_LEGACY - && self->connection->isolation_level - && self->statement->is_dml - && sqlite3_get_autocommit(self->connection->db)) - { - if (begin_transaction(self->connection) < 0) { - goto error; - } - } - - assert(!sqlite3_stmt_busy(self->statement->st)); - while (1) { - parameters = PyIter_Next(parameters_iter); - if (!parameters) { - break; - } - - bind_parameters(state, self->statement, parameters); - if (PyErr_Occurred()) { - goto error; - } - - rc = stmt_step(self->statement->st); - if (rc != SQLITE_DONE && rc != SQLITE_ROW) { - if (PyErr_Occurred()) { - /* there was an error that occurred in a user-defined callback */ - if (state->enable_callback_tracebacks) { - PyErr_Print(); - } else { - PyErr_Clear(); - } - } - _pysqlite_seterror(state, self->connection->db); - goto error; - } - - if (pysqlite_build_row_cast_map(self) != 0) { - _PyErr_FormatFromCause(state->OperationalError, - "Error while building row_cast_map"); - goto error; - } - - assert(rc == SQLITE_ROW || rc == SQLITE_DONE); - Py_BEGIN_ALLOW_THREADS - numcols = sqlite3_column_count(self->statement->st); - Py_END_ALLOW_THREADS - if (self->description == Py_None && numcols > 0) { - Py_SETREF(self->description, PyTuple_New(numcols)); - if (!self->description) { - goto error; - } - for (i = 0; i < numcols; i++) { - const char *colname; - colname = sqlite3_column_name(self->statement->st, i); - if (colname == NULL) { - PyErr_NoMemory(); - goto error; - } - column_name = _pysqlite_build_column_name(self, colname); - if (column_name == NULL) { - goto error; - } - PyObject *descriptor = PyTuple_Pack(7, column_name, - Py_None, Py_None, Py_None, - Py_None, Py_None, Py_None); - Py_DECREF(column_name); - if (descriptor == NULL) { - goto error; - } - PyTuple_SET_ITEM(self->description, i, descriptor); - } - } - - if (rc == SQLITE_DONE) { - if (self->statement->is_dml) { - self->rowcount += (long)sqlite3_changes(self->connection->db); - } - stmt_reset(self->statement); - } - Py_XDECREF(parameters); - } - - if (!multiple) { - sqlite_int64 lastrowid; - - Py_BEGIN_ALLOW_THREADS - lastrowid = sqlite3_last_insert_rowid(self->connection->db); - Py_END_ALLOW_THREADS - - Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid)); - // Fall through on error. - } - -error: - Py_XDECREF(parameters); - Py_XDECREF(parameters_iter); - Py_XDECREF(parameters_list); - - self->locked = 0; - - if (PyErr_Occurred()) { - if (self->statement) { - (void)stmt_reset(self->statement); - Py_CLEAR(self->statement); - } - self->rowcount = -1L; - return NULL; - } - if (self->statement && !sqlite3_stmt_busy(self->statement->st)) { - Py_CLEAR(self->statement); - } - return Py_NewRef((PyObject *)self); -} - -/*[clinic input] -_sqlite3.Cursor.execute as pysqlite_cursor_execute - - sql: unicode - parameters: object(c_default = 'NULL') = () - / - -Executes an SQL statement. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_execute_impl(pysqlite_Cursor *self, PyObject *sql, - PyObject *parameters) -/*[clinic end generated code: output=d81b4655c7c0bbad input=a8e0200a11627f94]*/ -{ - return _pysqlite_query_execute(self, 0, sql, parameters); -} - -/*[clinic input] -_sqlite3.Cursor.executemany as pysqlite_cursor_executemany - - sql: unicode - seq_of_parameters: object - / - -Repeatedly executes an SQL statement. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_executemany_impl(pysqlite_Cursor *self, PyObject *sql, - PyObject *seq_of_parameters) -/*[clinic end generated code: output=2c65a3c4733fb5d8 input=0d0a52e5eb7ccd35]*/ -{ - return _pysqlite_query_execute(self, 1, sql, seq_of_parameters); -} - -/*[clinic input] -_sqlite3.Cursor.executescript as pysqlite_cursor_executescript - - sql_script: str - / - -Executes multiple SQL statements at once. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_executescript_impl(pysqlite_Cursor *self, - const char *sql_script) -/*[clinic end generated code: output=8fd726dde1c65164 input=78f093be415a8a2c]*/ -{ - if (!check_cursor(self)) { - return NULL; - } - - size_t sql_len = strlen(sql_script); - int max_length = sqlite3_limit(self->connection->db, - SQLITE_LIMIT_SQL_LENGTH, -1); - if (sql_len > (unsigned)max_length) { - PyErr_SetString(self->connection->DataError, - "query string is too large"); - return NULL; - } - - // Commit if needed - sqlite3 *db = self->connection->db; - if (self->connection->autocommit == AUTOCOMMIT_LEGACY - && !sqlite3_get_autocommit(db)) - { - int rc = SQLITE_OK; - - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL); - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - goto error; - } - } - - while (1) { - int rc; - const char *tail; - - Py_BEGIN_ALLOW_THREADS - sqlite3_stmt *stmt; - rc = sqlite3_prepare_v2(db, sql_script, (int)sql_len + 1, &stmt, - &tail); - if (rc == SQLITE_OK) { - do { - rc = sqlite3_step(stmt); - } while (rc == SQLITE_ROW); - rc = sqlite3_finalize(stmt); - } - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - goto error; - } - - if (*tail == (char)0) { - break; - } - sql_len -= (tail - sql_script); - sql_script = tail; - } - - return Py_NewRef((PyObject *)self); - -error: - _pysqlite_seterror(self->connection->state, db); - return NULL; -} - -static PyObject * -pysqlite_cursor_iternext(pysqlite_Cursor *self) -{ - if (!check_cursor(self)) { - return NULL; - } - - if (self->statement == NULL) { - return NULL; - } - - sqlite3_stmt *stmt = self->statement->st; - assert(stmt != NULL); - assert(sqlite3_data_count(stmt) != 0); - - self->locked = 1; // GH-80254: Prevent recursive use of cursors. - PyObject *row = _pysqlite_fetch_one_row(self); - self->locked = 0; - if (row == NULL) { - return NULL; - } - int rc = stmt_step(stmt); - if (rc == SQLITE_DONE) { - if (self->statement->is_dml) { - self->rowcount = (long)sqlite3_changes(self->connection->db); - } - (void)stmt_reset(self->statement); - Py_CLEAR(self->statement); - } - else if (rc != SQLITE_ROW) { - (void)_pysqlite_seterror(self->connection->state, - self->connection->db); - (void)stmt_reset(self->statement); - Py_CLEAR(self->statement); - Py_DECREF(row); - return NULL; - } - if (!Py_IsNone(self->row_factory)) { - PyObject *factory = self->row_factory; - PyObject *args[] = { (PyObject *)self, row, }; - PyObject *new_row = PyObject_Vectorcall(factory, args, 2, NULL); - Py_SETREF(row, new_row); - } - return row; -} - -/*[clinic input] -_sqlite3.Cursor.fetchone as pysqlite_cursor_fetchone - -Fetches one row from the resultset. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_fetchone_impl(pysqlite_Cursor *self) -/*[clinic end generated code: output=4bd2eabf5baaddb0 input=e78294ec5980fdba]*/ -{ - PyObject* row; - - row = pysqlite_cursor_iternext(self); - if (!row && !PyErr_Occurred()) { - Py_RETURN_NONE; - } - - return row; -} - -/*[clinic input] -_sqlite3.Cursor.fetchmany as pysqlite_cursor_fetchmany - - size as maxrows: int(c_default='self->arraysize') = 1 - The default value is set by the Cursor.arraysize attribute. - -Fetches several rows from the resultset. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_fetchmany_impl(pysqlite_Cursor *self, int maxrows) -/*[clinic end generated code: output=a8ef31fea64d0906 input=c26e6ca3f34debd0]*/ -{ - PyObject* row; - PyObject* list; - int counter = 0; - - list = PyList_New(0); - if (!list) { - return NULL; - } - - while ((row = pysqlite_cursor_iternext(self))) { - if (PyList_Append(list, row) < 0) { - Py_DECREF(row); - break; - } - Py_DECREF(row); - - if (++counter == maxrows) { - break; - } - } - - if (PyErr_Occurred()) { - Py_DECREF(list); - return NULL; - } else { - return list; - } -} - -/*[clinic input] -_sqlite3.Cursor.fetchall as pysqlite_cursor_fetchall - -Fetches all rows from the resultset. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_fetchall_impl(pysqlite_Cursor *self) -/*[clinic end generated code: output=d5da12aca2da4b27 input=f5d401086a8df25a]*/ -{ - PyObject* row; - PyObject* list; - - list = PyList_New(0); - if (!list) { - return NULL; - } - - while ((row = pysqlite_cursor_iternext(self))) { - if (PyList_Append(list, row) < 0) { - Py_DECREF(row); - break; - } - Py_DECREF(row); - } - - if (PyErr_Occurred()) { - Py_DECREF(list); - return NULL; - } else { - return list; - } -} - -/*[clinic input] -_sqlite3.Cursor.setinputsizes as pysqlite_cursor_setinputsizes - - sizes: object - / - -Required by DB-API. Does nothing in sqlite3. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_setinputsizes(pysqlite_Cursor *self, PyObject *sizes) -/*[clinic end generated code: output=893c817afe9d08ad input=de7950a3aec79bdf]*/ -{ - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.Cursor.setoutputsize as pysqlite_cursor_setoutputsize - - size: object - column: object = None - / - -Required by DB-API. Does nothing in sqlite3. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size, - PyObject *column) -/*[clinic end generated code: output=018d7e9129d45efe input=607a6bece8bbb273]*/ -{ - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.Cursor.close as pysqlite_cursor_close - -Closes the cursor. -[clinic start generated code]*/ - -static PyObject * -pysqlite_cursor_close_impl(pysqlite_Cursor *self) -/*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/ -{ - if (!check_cursor_locked(self)) { - return NULL; - } - - if (!self->connection) { - PyTypeObject *tp = Py_TYPE(self); - pysqlite_state *state = pysqlite_get_state_by_type(tp); - PyErr_SetString(state->ProgrammingError, - "Base Cursor.__init__ not called."); - return NULL; - } - if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { - return NULL; - } - - if (self->statement) { - (void)stmt_reset(self->statement); - Py_CLEAR(self->statement); - } - - self->closed = 1; - - Py_RETURN_NONE; -} - -static PyMethodDef cursor_methods[] = { - PYSQLITE_CURSOR_CLOSE_METHODDEF - PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF - PYSQLITE_CURSOR_EXECUTESCRIPT_METHODDEF - PYSQLITE_CURSOR_EXECUTE_METHODDEF - PYSQLITE_CURSOR_FETCHALL_METHODDEF - PYSQLITE_CURSOR_FETCHMANY_METHODDEF - PYSQLITE_CURSOR_FETCHONE_METHODDEF - PYSQLITE_CURSOR_SETINPUTSIZES_METHODDEF - PYSQLITE_CURSOR_SETOUTPUTSIZE_METHODDEF - {NULL, NULL} -}; - -static struct PyMemberDef cursor_members[] = -{ - {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY}, - {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY}, - {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0}, - {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY}, - {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY}, - {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, - {"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Cursor, in_weakreflist), READONLY}, - {NULL} -}; - -static const char cursor_doc[] = -PyDoc_STR("SQLite database cursor class."); - -static PyType_Slot cursor_slots[] = { - {Py_tp_dealloc, cursor_dealloc}, - {Py_tp_doc, (void *)cursor_doc}, - {Py_tp_iter, PyObject_SelfIter}, - {Py_tp_iternext, pysqlite_cursor_iternext}, - {Py_tp_methods, cursor_methods}, - {Py_tp_members, cursor_members}, - {Py_tp_init, pysqlite_cursor_init}, - {Py_tp_traverse, cursor_traverse}, - {Py_tp_clear, cursor_clear}, - {0, NULL}, -}; - -static PyType_Spec cursor_spec = { - .name = MODULE_NAME ".Cursor", - .basicsize = sizeof(pysqlite_Cursor), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE), - .slots = cursor_slots, -}; - -int -pysqlite_cursor_setup_types(PyObject *module) -{ - PyObject *type = PyType_FromModuleAndSpec(module, &cursor_spec, NULL); - if (type == NULL) { - return -1; - } - pysqlite_state *state = pysqlite_get_state(module); - state->CursorType = (PyTypeObject *)type; - return 0; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/cursor.h b/contrib/tools/python3/src/Modules/_sqlite/cursor.h deleted file mode 100644 index 0bcdddc3e29..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/cursor.h +++ /dev/null @@ -1,53 +0,0 @@ -/* cursor.h - definitions for the cursor type - * - * Copyright (C) 2004-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PYSQLITE_CURSOR_H -#define PYSQLITE_CURSOR_H -#define PY_SSIZE_T_CLEAN -#include "Python.h" - -#include "statement.h" -#include "connection.h" -#include "module.h" - -typedef struct -{ - PyObject_HEAD - pysqlite_Connection* connection; - PyObject* description; - PyObject* row_cast_map; - int arraysize; - PyObject* lastrowid; - long rowcount; - PyObject* row_factory; - pysqlite_Statement* statement; - int closed; - int locked; - int initialized; - - PyObject* in_weakreflist; /* List of weak references */ -} pysqlite_Cursor; - -int pysqlite_cursor_setup_types(PyObject *module); - -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/microprotocols.c b/contrib/tools/python3/src/Modules/_sqlite/microprotocols.c deleted file mode 100644 index 148220d0f91..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/microprotocols.c +++ /dev/null @@ -1,144 +0,0 @@ -/* microprotocols.c - minimalist and non-validating protocols implementation - * - * Copyright (C) 2003-2004 Federico Di Gregorio <[email protected]> - * - * This file is part of psycopg and was adapted for pysqlite. Federico Di - * Gregorio gave the permission to use it within pysqlite under the following - * license: - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include <Python.h> - -#include "cursor.h" -#include "microprotocols.h" -#include "prepare_protocol.h" - - -/* pysqlite_microprotocols_init - initialize the adapters dictionary */ - -int -pysqlite_microprotocols_init(PyObject *module) -{ - /* create adapters dictionary and put it in module namespace */ - pysqlite_state *state = pysqlite_get_state(module); - state->psyco_adapters = PyDict_New(); - if (state->psyco_adapters == NULL) { - return -1; - } - - return PyModule_AddObjectRef(module, "adapters", state->psyco_adapters); -} - - -/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */ - -int -pysqlite_microprotocols_add(pysqlite_state *state, PyTypeObject *type, - PyObject *proto, PyObject *cast) -{ - PyObject* key; - int rc; - - assert(type != NULL); - assert(proto != NULL); - key = PyTuple_Pack(2, (PyObject *)type, proto); - if (!key) { - return -1; - } - - rc = PyDict_SetItem(state->psyco_adapters, key, cast); - Py_DECREF(key); - - return rc; -} - -/* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */ - -PyObject * -pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj, - PyObject *proto, PyObject *alt) -{ - PyObject *adapter, *key, *adapted; - - /* we don't check for exact type conformance as specified in PEP 246 - because the PrepareProtocolType type is abstract and there is no - way to get a quotable object to be its instance */ - - /* look for an adapter in the registry */ - key = PyTuple_Pack(2, (PyObject *)Py_TYPE(obj), proto); - if (!key) { - return NULL; - } - adapter = PyDict_GetItemWithError(state->psyco_adapters, key); - Py_DECREF(key); - if (adapter) { - Py_INCREF(adapter); - adapted = PyObject_CallOneArg(adapter, obj); - Py_DECREF(adapter); - return adapted; - } - if (PyErr_Occurred()) { - return NULL; - } - - /* try to have the protocol adapt this object */ - if (_PyObject_LookupAttr(proto, state->str___adapt__, &adapter) < 0) { - return NULL; - } - if (adapter) { - adapted = PyObject_CallOneArg(adapter, obj); - Py_DECREF(adapter); - - if (adapted == Py_None) { - Py_DECREF(adapted); - } - else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) { - return adapted; - } - else { - PyErr_Clear(); - } - } - - /* and finally try to have the object adapt itself */ - if (_PyObject_LookupAttr(obj, state->str___conform__, &adapter) < 0) { - return NULL; - } - if (adapter) { - adapted = PyObject_CallOneArg(adapter, proto); - Py_DECREF(adapter); - - if (adapted == Py_None) { - Py_DECREF(adapted); - } - else if (adapted || !PyErr_ExceptionMatches(PyExc_TypeError)) { - return adapted; - } - else { - PyErr_Clear(); - } - } - - if (alt) { - return Py_NewRef(alt); - } - /* else set the right exception and return NULL */ - PyErr_SetString(state->ProgrammingError, "can't adapt"); - return NULL; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/microprotocols.h b/contrib/tools/python3/src/Modules/_sqlite/microprotocols.h deleted file mode 100644 index 6bde9d01f45..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/microprotocols.h +++ /dev/null @@ -1,43 +0,0 @@ -/* microprotocols.c - definitions for minimalist and non-validating protocols - * - * Copyright (C) 2003-2004 Federico Di Gregorio <[email protected]> - * - * This file is part of psycopg and was adapted for pysqlite. Federico Di - * Gregorio gave the permission to use it within pysqlite under the following - * license: - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PSYCOPG_MICROPROTOCOLS_H -#define PSYCOPG_MICROPROTOCOLS_H 1 - -#define PY_SSIZE_T_CLEAN -#include <Python.h> - -/** exported functions **/ - -/* used by module.c to init the microprotocols system */ -extern int pysqlite_microprotocols_init(PyObject *module); -extern int pysqlite_microprotocols_add(pysqlite_state *state, - PyTypeObject *type, PyObject *proto, - PyObject *cast); -extern PyObject *pysqlite_microprotocols_adapt(pysqlite_state *state, - PyObject *obj, PyObject *proto, - PyObject *alt); - -#endif /* !defined(PSYCOPG_MICROPROTOCOLS_H) */ diff --git a/contrib/tools/python3/src/Modules/_sqlite/module.c b/contrib/tools/python3/src/Modules/_sqlite/module.c deleted file mode 100644 index 27bd42f4595..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/module.c +++ /dev/null @@ -1,807 +0,0 @@ -/* module.c - the module itself - * - * Copyright (C) 2004-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include "connection.h" -#include "statement.h" -#include "cursor.h" -#include "prepare_protocol.h" -#include "microprotocols.h" -#include "row.h" -#include "blob.h" - -#if SQLITE_VERSION_NUMBER < 3007015 -#error "SQLite 3.7.15 or higher required" -#endif - -#define clinic_state() (pysqlite_get_state(module)) -#include "clinic/module.c.h" -#undef clinic_state - -/*[clinic input] -module _sqlite3 -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81e330492d57488e]*/ - -// NB: This needs to be in sync with the Connection.__init__ docstring. -PyDoc_STRVAR(module_connect_doc, -"connect($module, /, database, timeout=5.0, detect_types=0,\n" -" isolation_level='', check_same_thread=True,\n" -" factory=ConnectionType, cached_statements=128, uri=False, *,\n" -" autocommit=sqlite3.LEGACY_TRANSACTION_CONTROL)\n" -"--\n" -"\n" -"Opens a connection to the SQLite database file database.\n" -"\n" -"You can use \":memory:\" to open a database connection to a database that resides\n" -"in RAM instead of on disk."); - -#define PYSQLITE_CONNECT_METHODDEF \ - {"connect", _PyCFunction_CAST(module_connect), METH_FASTCALL|METH_KEYWORDS, module_connect_doc}, - -static PyObject * -module_connect(PyObject *module, PyObject *const *args, Py_ssize_t nargsf, - PyObject *kwnames) -{ - pysqlite_state *state = pysqlite_get_state(module); - PyObject *factory = (PyObject *)state->ConnectionType; - - static const int FACTORY_POS = 5; - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - if (nargs > FACTORY_POS) { - factory = args[FACTORY_POS]; - } - else if (kwnames != NULL) { - for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) { - PyObject *item = PyTuple_GET_ITEM(kwnames, i); // borrowed ref. - if (PyUnicode_CompareWithASCIIString(item, "factory") == 0) { - factory = args[nargs + i]; - break; - } - } - } - - return PyObject_Vectorcall(factory, args, nargsf, kwnames); -} - -/*[clinic input] -_sqlite3.complete_statement as pysqlite_complete_statement - - statement: str - -Checks if a string contains a complete SQL statement. -[clinic start generated code]*/ - -static PyObject * -pysqlite_complete_statement_impl(PyObject *module, const char *statement) -/*[clinic end generated code: output=e55f1ff1952df558 input=ac45d257375bb828]*/ -{ - if (sqlite3_complete(statement)) { - return Py_NewRef(Py_True); - } else { - return Py_NewRef(Py_False); - } -} - -/*[clinic input] -_sqlite3.register_adapter as pysqlite_register_adapter - - type: object(type='PyTypeObject *') - adapter as caster: object - / - -Register a function to adapt Python objects to SQLite values. -[clinic start generated code]*/ - -static PyObject * -pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type, - PyObject *caster) -/*[clinic end generated code: output=a287e8db18e8af23 input=29a5e0f213030242]*/ -{ - int rc; - - /* a basic type is adapted; there's a performance optimization if that's not the case - * (99 % of all usages) */ - if (type == &PyLong_Type || type == &PyFloat_Type - || type == &PyUnicode_Type || type == &PyByteArray_Type) { - pysqlite_state *state = pysqlite_get_state(module); - state->BaseTypeAdapted = 1; - } - - pysqlite_state *state = pysqlite_get_state(module); - PyObject *protocol = (PyObject *)state->PrepareProtocolType; - rc = pysqlite_microprotocols_add(state, type, protocol, caster); - if (rc == -1) { - return NULL; - } - - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.register_converter as pysqlite_register_converter - - typename as orig_name: unicode - converter as callable: object - / - -Register a function to convert SQLite values to Python objects. -[clinic start generated code]*/ - -static PyObject * -pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name, - PyObject *callable) -/*[clinic end generated code: output=a2f2bfeed7230062 input=159a444971b40378]*/ -{ - PyObject* name = NULL; - PyObject* retval = NULL; - - /* convert the name to upper case */ - pysqlite_state *state = pysqlite_get_state(module); - name = PyObject_CallMethodNoArgs(orig_name, state->str_upper); - if (!name) { - goto error; - } - - if (PyDict_SetItem(state->converters, name, callable) != 0) { - goto error; - } - - retval = Py_NewRef(Py_None); -error: - Py_XDECREF(name); - return retval; -} - -/*[clinic input] -_sqlite3.enable_callback_tracebacks as pysqlite_enable_callback_trace - - enable: int - / - -Enable or disable callback functions throwing errors to stderr. -[clinic start generated code]*/ - -static PyObject * -pysqlite_enable_callback_trace_impl(PyObject *module, int enable) -/*[clinic end generated code: output=4ff1d051c698f194 input=cb79d3581eb77c40]*/ -{ - pysqlite_state *state = pysqlite_get_state(module); - state->enable_callback_tracebacks = enable; - - Py_RETURN_NONE; -} - -/*[clinic input] -_sqlite3.adapt as pysqlite_adapt - - obj: object - proto: object(c_default='(PyObject *)clinic_state()->PrepareProtocolType') = PrepareProtocolType - alt: object = NULL - / - -Adapt given object to given protocol. -[clinic start generated code]*/ - -static PyObject * -pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto, - PyObject *alt) -/*[clinic end generated code: output=0c3927c5fcd23dd9 input=a53dc9993e81e15f]*/ -{ - pysqlite_state *state = pysqlite_get_state(module); - return pysqlite_microprotocols_adapt(state, obj, proto, alt); -} - -static int converters_init(PyObject* module) -{ - pysqlite_state *state = pysqlite_get_state(module); - state->converters = PyDict_New(); - if (state->converters == NULL) { - return -1; - } - - return PyModule_AddObjectRef(module, "converters", state->converters); -} - -static int -load_functools_lru_cache(PyObject *module) -{ - pysqlite_state *state = pysqlite_get_state(module); - state->lru_cache = _PyImport_GetModuleAttrString("functools", "lru_cache"); - if (state->lru_cache == NULL) { - return -1; - } - return 0; -} - -static PyMethodDef module_methods[] = { - PYSQLITE_ADAPT_METHODDEF - PYSQLITE_COMPLETE_STATEMENT_METHODDEF - PYSQLITE_CONNECT_METHODDEF - PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF - PYSQLITE_REGISTER_ADAPTER_METHODDEF - PYSQLITE_REGISTER_CONVERTER_METHODDEF - {NULL, NULL} -}; - -/* SQLite C API result codes. See also: - * - https://www.sqlite.org/c3ref/c_abort_rollback.html - * - https://sqlite.org/changes.html#version_3_3_8 - * - https://sqlite.org/changes.html#version_3_7_16 - * - https://sqlite.org/changes.html#version_3_7_17 - * - https://sqlite.org/changes.html#version_3_8_0 - * - https://sqlite.org/changes.html#version_3_8_3 - * - https://sqlite.org/changes.html#version_3_14 - * - * Note: the SQLite changelogs rarely mention new result codes, so in order to - * keep the 'error_codes' table in sync with SQLite, we must manually inspect - * sqlite3.h for every release. - * - * We keep the SQLITE_VERSION_NUMBER checks in order to easily declutter the - * code when we adjust the SQLite version requirement. - */ -static const struct { - const char *name; - long value; -} error_codes[] = { -#define DECLARE_ERROR_CODE(code) {#code, code} - // Primary result code list - DECLARE_ERROR_CODE(SQLITE_ABORT), - DECLARE_ERROR_CODE(SQLITE_AUTH), - DECLARE_ERROR_CODE(SQLITE_BUSY), - DECLARE_ERROR_CODE(SQLITE_CANTOPEN), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT), - DECLARE_ERROR_CODE(SQLITE_CORRUPT), - DECLARE_ERROR_CODE(SQLITE_DONE), - DECLARE_ERROR_CODE(SQLITE_EMPTY), - DECLARE_ERROR_CODE(SQLITE_ERROR), - DECLARE_ERROR_CODE(SQLITE_FORMAT), - DECLARE_ERROR_CODE(SQLITE_FULL), - DECLARE_ERROR_CODE(SQLITE_INTERNAL), - DECLARE_ERROR_CODE(SQLITE_INTERRUPT), - DECLARE_ERROR_CODE(SQLITE_IOERR), - DECLARE_ERROR_CODE(SQLITE_LOCKED), - DECLARE_ERROR_CODE(SQLITE_MISMATCH), - DECLARE_ERROR_CODE(SQLITE_MISUSE), - DECLARE_ERROR_CODE(SQLITE_NOLFS), - DECLARE_ERROR_CODE(SQLITE_NOMEM), - DECLARE_ERROR_CODE(SQLITE_NOTADB), - DECLARE_ERROR_CODE(SQLITE_NOTFOUND), - DECLARE_ERROR_CODE(SQLITE_OK), - DECLARE_ERROR_CODE(SQLITE_PERM), - DECLARE_ERROR_CODE(SQLITE_PROTOCOL), - DECLARE_ERROR_CODE(SQLITE_RANGE), - DECLARE_ERROR_CODE(SQLITE_READONLY), - DECLARE_ERROR_CODE(SQLITE_ROW), - DECLARE_ERROR_CODE(SQLITE_SCHEMA), - DECLARE_ERROR_CODE(SQLITE_TOOBIG), -#if SQLITE_VERSION_NUMBER >= 3007017 - DECLARE_ERROR_CODE(SQLITE_NOTICE), - DECLARE_ERROR_CODE(SQLITE_WARNING), -#endif - // Extended result code list - DECLARE_ERROR_CODE(SQLITE_ABORT_ROLLBACK), - DECLARE_ERROR_CODE(SQLITE_BUSY_RECOVERY), - DECLARE_ERROR_CODE(SQLITE_CANTOPEN_FULLPATH), - DECLARE_ERROR_CODE(SQLITE_CANTOPEN_ISDIR), - DECLARE_ERROR_CODE(SQLITE_CANTOPEN_NOTEMPDIR), - DECLARE_ERROR_CODE(SQLITE_CORRUPT_VTAB), - DECLARE_ERROR_CODE(SQLITE_IOERR_ACCESS), - DECLARE_ERROR_CODE(SQLITE_IOERR_BLOCKED), - DECLARE_ERROR_CODE(SQLITE_IOERR_CHECKRESERVEDLOCK), - DECLARE_ERROR_CODE(SQLITE_IOERR_CLOSE), - DECLARE_ERROR_CODE(SQLITE_IOERR_DELETE), - DECLARE_ERROR_CODE(SQLITE_IOERR_DELETE_NOENT), - DECLARE_ERROR_CODE(SQLITE_IOERR_DIR_CLOSE), - DECLARE_ERROR_CODE(SQLITE_IOERR_DIR_FSYNC), - DECLARE_ERROR_CODE(SQLITE_IOERR_FSTAT), - DECLARE_ERROR_CODE(SQLITE_IOERR_FSYNC), - DECLARE_ERROR_CODE(SQLITE_IOERR_LOCK), - DECLARE_ERROR_CODE(SQLITE_IOERR_NOMEM), - DECLARE_ERROR_CODE(SQLITE_IOERR_RDLOCK), - DECLARE_ERROR_CODE(SQLITE_IOERR_READ), - DECLARE_ERROR_CODE(SQLITE_IOERR_SEEK), - DECLARE_ERROR_CODE(SQLITE_IOERR_SHMLOCK), - DECLARE_ERROR_CODE(SQLITE_IOERR_SHMMAP), - DECLARE_ERROR_CODE(SQLITE_IOERR_SHMOPEN), - DECLARE_ERROR_CODE(SQLITE_IOERR_SHMSIZE), - DECLARE_ERROR_CODE(SQLITE_IOERR_SHORT_READ), - DECLARE_ERROR_CODE(SQLITE_IOERR_TRUNCATE), - DECLARE_ERROR_CODE(SQLITE_IOERR_UNLOCK), - DECLARE_ERROR_CODE(SQLITE_IOERR_WRITE), - DECLARE_ERROR_CODE(SQLITE_LOCKED_SHAREDCACHE), - DECLARE_ERROR_CODE(SQLITE_READONLY_CANTLOCK), - DECLARE_ERROR_CODE(SQLITE_READONLY_RECOVERY), -#if SQLITE_VERSION_NUMBER >= 3007016 - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_CHECK), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_COMMITHOOK), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_FOREIGNKEY), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_FUNCTION), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_NOTNULL), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_PRIMARYKEY), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_TRIGGER), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_UNIQUE), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_VTAB), - DECLARE_ERROR_CODE(SQLITE_READONLY_ROLLBACK), -#endif -#if SQLITE_VERSION_NUMBER >= 3007017 - DECLARE_ERROR_CODE(SQLITE_IOERR_MMAP), - DECLARE_ERROR_CODE(SQLITE_NOTICE_RECOVER_ROLLBACK), - DECLARE_ERROR_CODE(SQLITE_NOTICE_RECOVER_WAL), -#endif -#if SQLITE_VERSION_NUMBER >= 3008000 - DECLARE_ERROR_CODE(SQLITE_BUSY_SNAPSHOT), - DECLARE_ERROR_CODE(SQLITE_IOERR_GETTEMPPATH), - DECLARE_ERROR_CODE(SQLITE_WARNING_AUTOINDEX), -#endif -#if SQLITE_VERSION_NUMBER >= 3008001 - DECLARE_ERROR_CODE(SQLITE_CANTOPEN_CONVPATH), - DECLARE_ERROR_CODE(SQLITE_IOERR_CONVPATH), -#endif -#if SQLITE_VERSION_NUMBER >= 3008002 - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_ROWID), -#endif -#if SQLITE_VERSION_NUMBER >= 3008003 - DECLARE_ERROR_CODE(SQLITE_READONLY_DBMOVED), -#endif -#if SQLITE_VERSION_NUMBER >= 3008007 - DECLARE_ERROR_CODE(SQLITE_AUTH_USER), -#endif -#if SQLITE_VERSION_NUMBER >= 3009000 - DECLARE_ERROR_CODE(SQLITE_IOERR_VNODE), -#endif -#if SQLITE_VERSION_NUMBER >= 3010000 - DECLARE_ERROR_CODE(SQLITE_IOERR_AUTH), -#endif -#if SQLITE_VERSION_NUMBER >= 3014001 - DECLARE_ERROR_CODE(SQLITE_OK_LOAD_PERMANENTLY), -#endif -#if SQLITE_VERSION_NUMBER >= 3021000 - DECLARE_ERROR_CODE(SQLITE_IOERR_BEGIN_ATOMIC), - DECLARE_ERROR_CODE(SQLITE_IOERR_COMMIT_ATOMIC), - DECLARE_ERROR_CODE(SQLITE_IOERR_ROLLBACK_ATOMIC), -#endif -#if SQLITE_VERSION_NUMBER >= 3022000 - DECLARE_ERROR_CODE(SQLITE_ERROR_MISSING_COLLSEQ), - DECLARE_ERROR_CODE(SQLITE_ERROR_RETRY), - DECLARE_ERROR_CODE(SQLITE_READONLY_CANTINIT), - DECLARE_ERROR_CODE(SQLITE_READONLY_DIRECTORY), -#endif -#if SQLITE_VERSION_NUMBER >= 3024000 - DECLARE_ERROR_CODE(SQLITE_CORRUPT_SEQUENCE), - DECLARE_ERROR_CODE(SQLITE_LOCKED_VTAB), -#endif -#if SQLITE_VERSION_NUMBER >= 3025000 - DECLARE_ERROR_CODE(SQLITE_CANTOPEN_DIRTYWAL), - DECLARE_ERROR_CODE(SQLITE_ERROR_SNAPSHOT), -#endif -#if SQLITE_VERSION_NUMBER >= 3031000 - DECLARE_ERROR_CODE(SQLITE_CANTOPEN_SYMLINK), - DECLARE_ERROR_CODE(SQLITE_CONSTRAINT_PINNED), - DECLARE_ERROR_CODE(SQLITE_OK_SYMLINK), -#endif -#if SQLITE_VERSION_NUMBER >= 3032000 - DECLARE_ERROR_CODE(SQLITE_BUSY_TIMEOUT), - DECLARE_ERROR_CODE(SQLITE_CORRUPT_INDEX), - DECLARE_ERROR_CODE(SQLITE_IOERR_DATA), -#endif -#if SQLITE_VERSION_NUMBER >= 3034000 - DECLARE_ERROR_CODE(SQLITE_IOERR_CORRUPTFS), -#endif -#undef DECLARE_ERROR_CODE - {NULL, 0}, -}; - -static int -add_error_constants(PyObject *module) -{ - for (int i = 0; error_codes[i].name != NULL; i++) { - const char *name = error_codes[i].name; - const long value = error_codes[i].value; - if (PyModule_AddIntConstant(module, name, value) < 0) { - return -1; - } - } - return 0; -} - -const char * -pysqlite_error_name(int rc) -{ - for (int i = 0; error_codes[i].name != NULL; i++) { - if (error_codes[i].value == rc) { - return error_codes[i].name; - } - } - // No error code matched. - return NULL; -} - -static int -add_integer_constants(PyObject *module) { -#define ADD_INT(ival) \ - do { \ - if (PyModule_AddIntConstant(module, #ival, ival) < 0) { \ - return -1; \ - } \ - } while (0); \ - - ADD_INT(PARSE_DECLTYPES); - ADD_INT(PARSE_COLNAMES); - ADD_INT(SQLITE_DENY); - ADD_INT(SQLITE_IGNORE); - ADD_INT(SQLITE_CREATE_INDEX); - ADD_INT(SQLITE_CREATE_TABLE); - ADD_INT(SQLITE_CREATE_TEMP_INDEX); - ADD_INT(SQLITE_CREATE_TEMP_TABLE); - ADD_INT(SQLITE_CREATE_TEMP_TRIGGER); - ADD_INT(SQLITE_CREATE_TEMP_VIEW); - ADD_INT(SQLITE_CREATE_TRIGGER); - ADD_INT(SQLITE_CREATE_VIEW); - ADD_INT(SQLITE_DELETE); - ADD_INT(SQLITE_DROP_INDEX); - ADD_INT(SQLITE_DROP_TABLE); - ADD_INT(SQLITE_DROP_TEMP_INDEX); - ADD_INT(SQLITE_DROP_TEMP_TABLE); - ADD_INT(SQLITE_DROP_TEMP_TRIGGER); - ADD_INT(SQLITE_DROP_TEMP_VIEW); - ADD_INT(SQLITE_DROP_TRIGGER); - ADD_INT(SQLITE_DROP_VIEW); - ADD_INT(SQLITE_INSERT); - ADD_INT(SQLITE_PRAGMA); - ADD_INT(SQLITE_READ); - ADD_INT(SQLITE_SELECT); - ADD_INT(SQLITE_TRANSACTION); - ADD_INT(SQLITE_UPDATE); - ADD_INT(SQLITE_ATTACH); - ADD_INT(SQLITE_DETACH); - ADD_INT(SQLITE_ALTER_TABLE); - ADD_INT(SQLITE_REINDEX); - ADD_INT(SQLITE_ANALYZE); - ADD_INT(SQLITE_CREATE_VTABLE); - ADD_INT(SQLITE_DROP_VTABLE); - ADD_INT(SQLITE_FUNCTION); - ADD_INT(SQLITE_SAVEPOINT); -#if SQLITE_VERSION_NUMBER >= 3008003 - ADD_INT(SQLITE_RECURSIVE); -#endif - // Run-time limit categories - ADD_INT(SQLITE_LIMIT_LENGTH); - ADD_INT(SQLITE_LIMIT_SQL_LENGTH); - ADD_INT(SQLITE_LIMIT_COLUMN); - ADD_INT(SQLITE_LIMIT_EXPR_DEPTH); - ADD_INT(SQLITE_LIMIT_COMPOUND_SELECT); - ADD_INT(SQLITE_LIMIT_VDBE_OP); - ADD_INT(SQLITE_LIMIT_FUNCTION_ARG); - ADD_INT(SQLITE_LIMIT_ATTACHED); - ADD_INT(SQLITE_LIMIT_LIKE_PATTERN_LENGTH); - ADD_INT(SQLITE_LIMIT_VARIABLE_NUMBER); - ADD_INT(SQLITE_LIMIT_TRIGGER_DEPTH); -#if SQLITE_VERSION_NUMBER >= 3008007 - ADD_INT(SQLITE_LIMIT_WORKER_THREADS); -#endif - - /* - * Database connection configuration options. - * See https://www.sqlite.org/c3ref/c_dbconfig_defensive.html - */ - ADD_INT(SQLITE_DBCONFIG_ENABLE_FKEY); - ADD_INT(SQLITE_DBCONFIG_ENABLE_TRIGGER); -#if SQLITE_VERSION_NUMBER >= 3012002 - ADD_INT(SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER); -#endif -#if SQLITE_VERSION_NUMBER >= 3013000 - ADD_INT(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION); -#endif -#if SQLITE_VERSION_NUMBER >= 3016000 - ADD_INT(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE); -#endif -#if SQLITE_VERSION_NUMBER >= 3020000 - ADD_INT(SQLITE_DBCONFIG_ENABLE_QPSG); -#endif -#if SQLITE_VERSION_NUMBER >= 3022000 - ADD_INT(SQLITE_DBCONFIG_TRIGGER_EQP); -#endif -#if SQLITE_VERSION_NUMBER >= 3024000 - ADD_INT(SQLITE_DBCONFIG_RESET_DATABASE); -#endif -#if SQLITE_VERSION_NUMBER >= 3026000 - ADD_INT(SQLITE_DBCONFIG_DEFENSIVE); -#endif -#if SQLITE_VERSION_NUMBER >= 3028000 - ADD_INT(SQLITE_DBCONFIG_WRITABLE_SCHEMA); -#endif -#if SQLITE_VERSION_NUMBER >= 3029000 - ADD_INT(SQLITE_DBCONFIG_DQS_DDL); - ADD_INT(SQLITE_DBCONFIG_DQS_DML); - ADD_INT(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE); -#endif -#if SQLITE_VERSION_NUMBER >= 3030000 - ADD_INT(SQLITE_DBCONFIG_ENABLE_VIEW); -#endif -#if SQLITE_VERSION_NUMBER >= 3031000 - ADD_INT(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT); - ADD_INT(SQLITE_DBCONFIG_TRUSTED_SCHEMA); -#endif -#undef ADD_INT - return 0; -} - -/* Convert SQLite default threading mode (as set by the compile-time constant - * SQLITE_THREADSAFE) to the corresponding DB-API 2.0 (PEP 249) threadsafety - * level. */ -static int -get_threadsafety(pysqlite_state *state) -{ - int mode = sqlite3_threadsafe(); - switch (mode) { - case 0: // Single-thread mode; threads may not share the module. - return 0; - case 1: // Serialized mode; threads may share the module, - return 3; // connections, and cursors. - case 2: // Multi-thread mode; threads may share the module, but not - return 1; // connections. - default: - PyErr_Format(state->InterfaceError, - "Unable to interpret SQLite threadsafety mode. Got %d, " - "expected 0, 1, or 2", mode); - return -1; - } -} - -static int -module_traverse(PyObject *module, visitproc visit, void *arg) -{ - pysqlite_state *state = pysqlite_get_state(module); - - // Exceptions - Py_VISIT(state->DataError); - Py_VISIT(state->DatabaseError); - Py_VISIT(state->Error); - Py_VISIT(state->IntegrityError); - Py_VISIT(state->InterfaceError); - Py_VISIT(state->InternalError); - Py_VISIT(state->NotSupportedError); - Py_VISIT(state->OperationalError); - Py_VISIT(state->ProgrammingError); - Py_VISIT(state->Warning); - - // Types - Py_VISIT(state->BlobType); - Py_VISIT(state->ConnectionType); - Py_VISIT(state->CursorType); - Py_VISIT(state->PrepareProtocolType); - Py_VISIT(state->RowType); - Py_VISIT(state->StatementType); - - // Misc - Py_VISIT(state->converters); - Py_VISIT(state->lru_cache); - Py_VISIT(state->psyco_adapters); - - return 0; -} - -static int -module_clear(PyObject *module) -{ - pysqlite_state *state = pysqlite_get_state(module); - - // Exceptions - Py_CLEAR(state->DataError); - Py_CLEAR(state->DatabaseError); - Py_CLEAR(state->Error); - Py_CLEAR(state->IntegrityError); - Py_CLEAR(state->InterfaceError); - Py_CLEAR(state->InternalError); - Py_CLEAR(state->NotSupportedError); - Py_CLEAR(state->OperationalError); - Py_CLEAR(state->ProgrammingError); - Py_CLEAR(state->Warning); - - // Types - Py_CLEAR(state->BlobType); - Py_CLEAR(state->ConnectionType); - Py_CLEAR(state->CursorType); - Py_CLEAR(state->PrepareProtocolType); - Py_CLEAR(state->RowType); - Py_CLEAR(state->StatementType); - - // Misc - Py_CLEAR(state->converters); - Py_CLEAR(state->lru_cache); - Py_CLEAR(state->psyco_adapters); - - // Interned strings - Py_CLEAR(state->str___adapt__); - Py_CLEAR(state->str___conform__); - Py_CLEAR(state->str_executescript); - Py_CLEAR(state->str_finalize); - Py_CLEAR(state->str_inverse); - Py_CLEAR(state->str_step); - Py_CLEAR(state->str_upper); - Py_CLEAR(state->str_value); - - return 0; -} - -static void -module_free(void *module) -{ - module_clear((PyObject *)module); -} - -#define ADD_TYPE(module, type) \ -do { \ - if (PyModule_AddType(module, type) < 0) { \ - goto error; \ - } \ -} while (0) - -#define ADD_EXCEPTION(module, state, exc, base) \ -do { \ - state->exc = PyErr_NewException(MODULE_NAME "." #exc, base, NULL); \ - if (state->exc == NULL) { \ - goto error; \ - } \ - ADD_TYPE(module, (PyTypeObject *)state->exc); \ -} while (0) - -#define ADD_INTERNED(state, string) \ -do { \ - PyObject *tmp = PyUnicode_InternFromString(#string); \ - if (tmp == NULL) { \ - goto error; \ - } \ - state->str_ ## string = tmp; \ -} while (0) - -static int -module_exec(PyObject *module) -{ - if (sqlite3_libversion_number() < 3007015) { - PyErr_SetString(PyExc_ImportError, MODULE_NAME ": SQLite 3.7.15 or higher required"); - return -1; - } - - int rc = sqlite3_initialize(); - if (rc != SQLITE_OK) { - PyErr_SetString(PyExc_ImportError, sqlite3_errstr(rc)); - return -1; - } - - if ((pysqlite_row_setup_types(module) < 0) || - (pysqlite_cursor_setup_types(module) < 0) || - (pysqlite_connection_setup_types(module) < 0) || - (pysqlite_statement_setup_types(module) < 0) || - (pysqlite_prepare_protocol_setup_types(module) < 0) || - (pysqlite_blob_setup_types(module) < 0) - ) { - goto error; - } - - pysqlite_state *state = pysqlite_get_state(module); - ADD_TYPE(module, state->BlobType); - ADD_TYPE(module, state->ConnectionType); - ADD_TYPE(module, state->CursorType); - ADD_TYPE(module, state->PrepareProtocolType); - ADD_TYPE(module, state->RowType); - - /*** Create DB-API Exception hierarchy */ - ADD_EXCEPTION(module, state, Error, PyExc_Exception); - ADD_EXCEPTION(module, state, Warning, PyExc_Exception); - - /* Error subclasses */ - ADD_EXCEPTION(module, state, InterfaceError, state->Error); - ADD_EXCEPTION(module, state, DatabaseError, state->Error); - - /* DatabaseError subclasses */ - ADD_EXCEPTION(module, state, InternalError, state->DatabaseError); - ADD_EXCEPTION(module, state, OperationalError, state->DatabaseError); - ADD_EXCEPTION(module, state, ProgrammingError, state->DatabaseError); - ADD_EXCEPTION(module, state, IntegrityError, state->DatabaseError); - ADD_EXCEPTION(module, state, DataError, state->DatabaseError); - ADD_EXCEPTION(module, state, NotSupportedError, state->DatabaseError); - - /* Add interned strings */ - ADD_INTERNED(state, __adapt__); - ADD_INTERNED(state, __conform__); - ADD_INTERNED(state, executescript); - ADD_INTERNED(state, finalize); - ADD_INTERNED(state, inverse); - ADD_INTERNED(state, step); - ADD_INTERNED(state, upper); - ADD_INTERNED(state, value); - - /* Set error constants */ - if (add_error_constants(module) < 0) { - goto error; - } - - /* Set integer constants */ - if (add_integer_constants(module) < 0) { - goto error; - } - - if (PyModule_AddStringConstant(module, "_deprecated_version", PYSQLITE_VERSION) < 0) { - goto error; - } - - if (PyModule_AddStringConstant(module, "sqlite_version", sqlite3_libversion())) { - goto error; - } - - if (PyModule_AddIntMacro(module, LEGACY_TRANSACTION_CONTROL) < 0) { - goto error; - } - - int threadsafety = get_threadsafety(state); - if (threadsafety < 0) { - goto error; - } - if (PyModule_AddIntConstant(module, "threadsafety", threadsafety) < 0) { - goto error; - } - - /* initialize microprotocols layer */ - if (pysqlite_microprotocols_init(module) < 0) { - goto error; - } - - /* initialize the default converters */ - if (converters_init(module) < 0) { - goto error; - } - - if (load_functools_lru_cache(module) < 0) { - goto error; - } - - return 0; - -error: - sqlite3_shutdown(); - return -1; -} - -static struct PyModuleDef_Slot module_slots[] = { - {Py_mod_exec, module_exec}, - {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, - {0, NULL}, -}; - -struct PyModuleDef _sqlite3module = { - .m_base = PyModuleDef_HEAD_INIT, - .m_name = "_sqlite3", - .m_size = sizeof(pysqlite_state), - .m_methods = module_methods, - .m_slots = module_slots, - .m_traverse = module_traverse, - .m_clear = module_clear, - .m_free = module_free, -}; - -PyMODINIT_FUNC -PyInit__sqlite3(void) -{ - return PyModuleDef_Init(&_sqlite3module); -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/module.h b/contrib/tools/python3/src/Modules/_sqlite/module.h deleted file mode 100644 index daa22091d38..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/module.h +++ /dev/null @@ -1,99 +0,0 @@ -/* module.h - definitions for the module - * - * Copyright (C) 2004-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PYSQLITE_MODULE_H -#define PYSQLITE_MODULE_H -#define PY_SSIZE_T_CLEAN -#include "Python.h" - -#define LEGACY_TRANSACTION_CONTROL -1 - -#define PYSQLITE_VERSION "2.6.0" -#define MODULE_NAME "sqlite3" - -typedef struct { - PyObject *DataError; - PyObject *DatabaseError; - PyObject *Error; - PyObject *IntegrityError; - PyObject *InterfaceError; - PyObject *InternalError; - PyObject *NotSupportedError; - PyObject *OperationalError; - PyObject *ProgrammingError; - PyObject *Warning; - - - /* A dictionary, mapping column types (INTEGER, VARCHAR, etc.) to converter - * functions, that convert the SQL value to the appropriate Python value. - * The key is uppercase. - */ - PyObject *converters; - - PyObject *lru_cache; - PyObject *psyco_adapters; // The adapters registry - int BaseTypeAdapted; - int enable_callback_tracebacks; - - PyTypeObject *BlobType; - PyTypeObject *ConnectionType; - PyTypeObject *CursorType; - PyTypeObject *PrepareProtocolType; - PyTypeObject *RowType; - PyTypeObject *StatementType; - - /* Pointers to interned strings */ - PyObject *str___adapt__; - PyObject *str___conform__; - PyObject *str_executescript; - PyObject *str_finalize; - PyObject *str_inverse; - PyObject *str_step; - PyObject *str_upper; - PyObject *str_value; -} pysqlite_state; - -extern pysqlite_state pysqlite_global_state; - -static inline pysqlite_state * -pysqlite_get_state(PyObject *module) -{ - pysqlite_state *state = (pysqlite_state *)PyModule_GetState(module); - assert(state != NULL); - return state; -} - -extern struct PyModuleDef _sqlite3module; -static inline pysqlite_state * -pysqlite_get_state_by_type(PyTypeObject *tp) -{ - PyObject *module = PyType_GetModuleByDef(tp, &_sqlite3module); - assert(module != NULL); - return pysqlite_get_state(module); -} - -extern const char *pysqlite_error_name(int rc); - -#define PARSE_DECLTYPES 1 -#define PARSE_COLNAMES 2 -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/prepare_protocol.c b/contrib/tools/python3/src/Modules/_sqlite/prepare_protocol.c deleted file mode 100644 index 44533225665..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/prepare_protocol.c +++ /dev/null @@ -1,77 +0,0 @@ -/* prepare_protocol.c - the protocol for preparing values for SQLite - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include "prepare_protocol.h" - -static int -pysqlite_prepare_protocol_init(pysqlite_PrepareProtocol *self, PyObject *args, - PyObject *kwargs) -{ - return 0; -} - -static int -pysqlite_prepare_protocol_traverse(PyObject *self, visitproc visit, void *arg) -{ - Py_VISIT(Py_TYPE(self)); - return 0; -} - -static void -pysqlite_prepare_protocol_dealloc(pysqlite_PrepareProtocol *self) -{ - PyTypeObject *tp = Py_TYPE(self); - PyObject_GC_UnTrack(self); - tp->tp_free(self); - Py_DECREF(tp); -} - -PyDoc_STRVAR(doc, "PEP 246 style object adaption protocol type."); - -static PyType_Slot type_slots[] = { - {Py_tp_dealloc, pysqlite_prepare_protocol_dealloc}, - {Py_tp_init, pysqlite_prepare_protocol_init}, - {Py_tp_traverse, pysqlite_prepare_protocol_traverse}, - {Py_tp_doc, (void *)doc}, - {0, NULL}, -}; - -static PyType_Spec type_spec = { - .name = MODULE_NAME ".PrepareProtocol", - .basicsize = sizeof(pysqlite_PrepareProtocol), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_IMMUTABLETYPE), - .slots = type_slots, -}; - -int -pysqlite_prepare_protocol_setup_types(PyObject *module) -{ - PyObject *type = PyType_FromModuleAndSpec(module, &type_spec, NULL); - if (type == NULL) { - return -1; - } - pysqlite_state *state = pysqlite_get_state(module); - state->PrepareProtocolType = (PyTypeObject *)type; - return 0; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/prepare_protocol.h b/contrib/tools/python3/src/Modules/_sqlite/prepare_protocol.h deleted file mode 100644 index afc55a8c1c4..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/prepare_protocol.h +++ /dev/null @@ -1,35 +0,0 @@ -/* prepare_protocol.h - the protocol for preparing values for SQLite - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PYSQLITE_PREPARE_PROTOCOL_H -#define PYSQLITE_PREPARE_PROTOCOL_H -#include "module.h" - -typedef struct -{ - PyObject_HEAD -} pysqlite_PrepareProtocol; - -int pysqlite_prepare_protocol_setup_types(PyObject *module); - -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/row.c b/contrib/tools/python3/src/Modules/_sqlite/row.c deleted file mode 100644 index 1a1943285ce..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/row.c +++ /dev/null @@ -1,276 +0,0 @@ -/* row.c - an enhanced tuple for database rows - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include "row.h" -#include "cursor.h" - -#define clinic_state() (pysqlite_get_state_by_type(type)) -#include "clinic/row.c.h" -#undef clinic_state - -/*[clinic input] -module _sqlite3 -class _sqlite3.Row "pysqlite_Row *" "clinic_state()->RowType" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=966c53403d7f3a40]*/ - -static int -row_clear(pysqlite_Row *self) -{ - Py_CLEAR(self->data); - Py_CLEAR(self->description); - return 0; -} - -static int -row_traverse(pysqlite_Row *self, visitproc visit, void *arg) -{ - Py_VISIT(Py_TYPE(self)); - Py_VISIT(self->data); - Py_VISIT(self->description); - return 0; -} - -static void -pysqlite_row_dealloc(PyObject *self) -{ - PyTypeObject *tp = Py_TYPE(self); - PyObject_GC_UnTrack(self); - tp->tp_clear(self); - tp->tp_free(self); - Py_DECREF(tp); -} - -/*[clinic input] -@classmethod -_sqlite3.Row.__new__ as pysqlite_row_new - - cursor: object(type='pysqlite_Cursor *', subclass_of='clinic_state()->CursorType') - data: object(subclass_of='&PyTuple_Type') - / - -[clinic start generated code]*/ - -static PyObject * -pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor, - PyObject *data) -/*[clinic end generated code: output=10d58b09a819a4c1 input=b9e954ca31345dbf]*/ -{ - pysqlite_Row *self; - - assert(type != NULL && type->tp_alloc != NULL); - - self = (pysqlite_Row *) type->tp_alloc(type, 0); - if (self == NULL) - return NULL; - - self->data = Py_NewRef(data); - self->description = Py_NewRef(cursor->description); - - return (PyObject *) self; -} - -PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx) -{ - PyObject *item = PyTuple_GetItem(self->data, idx); - return Py_XNewRef(item); -} - -static int -equal_ignore_case(PyObject *left, PyObject *right) -{ - int eq = PyObject_RichCompareBool(left, right, Py_EQ); - if (eq) { /* equal or error */ - return eq; - } - if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) { - return 0; - } - if (!PyUnicode_IS_ASCII(left) || !PyUnicode_IS_ASCII(right)) { - return 0; - } - - Py_ssize_t len = PyUnicode_GET_LENGTH(left); - if (PyUnicode_GET_LENGTH(right) != len) { - return 0; - } - const Py_UCS1 *p1 = PyUnicode_1BYTE_DATA(left); - const Py_UCS1 *p2 = PyUnicode_1BYTE_DATA(right); - for (; len; len--, p1++, p2++) { - if (Py_TOLOWER(*p1) != Py_TOLOWER(*p2)) { - return 0; - } - } - return 1; -} - -static PyObject * -pysqlite_row_subscript(pysqlite_Row *self, PyObject *idx) -{ - Py_ssize_t _idx; - Py_ssize_t nitems, i; - - if (PyLong_Check(idx)) { - _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError); - if (_idx == -1 && PyErr_Occurred()) - return NULL; - if (_idx < 0) - _idx += PyTuple_GET_SIZE(self->data); - - PyObject *item = PyTuple_GetItem(self->data, _idx); - return Py_XNewRef(item); - } else if (PyUnicode_Check(idx)) { - nitems = PyTuple_Size(self->description); - - for (i = 0; i < nitems; i++) { - PyObject *obj; - obj = PyTuple_GET_ITEM(self->description, i); - obj = PyTuple_GET_ITEM(obj, 0); - int eq = equal_ignore_case(idx, obj); - if (eq < 0) { - return NULL; - } - if (eq) { - /* found item */ - PyObject *item = PyTuple_GetItem(self->data, i); - return Py_XNewRef(item); - } - } - - PyErr_SetString(PyExc_IndexError, "No item with that key"); - return NULL; - } - else if (PySlice_Check(idx)) { - return PyObject_GetItem(self->data, idx); - } - else { - PyErr_SetString(PyExc_IndexError, "Index must be int or string"); - return NULL; - } -} - -static Py_ssize_t -pysqlite_row_length(pysqlite_Row* self) -{ - return PyTuple_GET_SIZE(self->data); -} - -/*[clinic input] -_sqlite3.Row.keys as pysqlite_row_keys - -Returns the keys of the row. -[clinic start generated code]*/ - -static PyObject * -pysqlite_row_keys_impl(pysqlite_Row *self) -/*[clinic end generated code: output=efe3dfb3af6edc07 input=7549a122827c5563]*/ -{ - PyObject* list; - Py_ssize_t nitems, i; - - list = PyList_New(0); - if (!list) { - return NULL; - } - nitems = PyTuple_Size(self->description); - - for (i = 0; i < nitems; i++) { - if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) { - Py_DECREF(list); - return NULL; - } - } - - return list; -} - -static PyObject* pysqlite_iter(pysqlite_Row* self) -{ - return PyObject_GetIter(self->data); -} - -static Py_hash_t pysqlite_row_hash(pysqlite_Row *self) -{ - return PyObject_Hash(self->description) ^ PyObject_Hash(self->data); -} - -static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid) -{ - if (opid != Py_EQ && opid != Py_NE) - Py_RETURN_NOTIMPLEMENTED; - - pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self)); - if (PyObject_TypeCheck(_other, state->RowType)) { - pysqlite_Row *other = (pysqlite_Row *)_other; - int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ); - if (eq < 0) { - return NULL; - } - if (eq) { - return PyObject_RichCompare(self->data, other->data, opid); - } - return PyBool_FromLong(opid != Py_EQ); - } - Py_RETURN_NOTIMPLEMENTED; -} - -static PyMethodDef row_methods[] = { - PYSQLITE_ROW_KEYS_METHODDEF - {NULL, NULL} -}; - -static PyType_Slot row_slots[] = { - {Py_tp_dealloc, pysqlite_row_dealloc}, - {Py_tp_hash, pysqlite_row_hash}, - {Py_tp_methods, row_methods}, - {Py_tp_richcompare, pysqlite_row_richcompare}, - {Py_tp_iter, pysqlite_iter}, - {Py_mp_length, pysqlite_row_length}, - {Py_mp_subscript, pysqlite_row_subscript}, - {Py_sq_length, pysqlite_row_length}, - {Py_sq_item, pysqlite_row_item}, - {Py_tp_new, pysqlite_row_new}, - {Py_tp_traverse, row_traverse}, - {Py_tp_clear, row_clear}, - {0, NULL}, -}; - -static PyType_Spec row_spec = { - .name = MODULE_NAME ".Row", - .basicsize = sizeof(pysqlite_Row), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE), - .slots = row_slots, -}; - -int -pysqlite_row_setup_types(PyObject *module) -{ - PyObject *type = PyType_FromModuleAndSpec(module, &row_spec, NULL); - if (type == NULL) { - return -1; - } - pysqlite_state *state = pysqlite_get_state(module); - state->RowType = (PyTypeObject *)type; - return 0; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/row.h b/contrib/tools/python3/src/Modules/_sqlite/row.h deleted file mode 100644 index b5190981758..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/row.h +++ /dev/null @@ -1,38 +0,0 @@ -/* row.h - an enhanced tuple for database rows - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PYSQLITE_ROW_H -#define PYSQLITE_ROW_H -#define PY_SSIZE_T_CLEAN -#include "Python.h" - -typedef struct _Row -{ - PyObject_HEAD - PyObject* data; - PyObject* description; -} pysqlite_Row; - -int pysqlite_row_setup_types(PyObject *module); - -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/statement.c b/contrib/tools/python3/src/Modules/_sqlite/statement.c deleted file mode 100644 index 229bfc3b504..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/statement.c +++ /dev/null @@ -1,205 +0,0 @@ -/* statement.c - the statement type - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include "connection.h" -#include "statement.h" -#include "util.h" - -/* prototypes */ -static const char *lstrip_sql(const char *sql); - -pysqlite_Statement * -pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql) -{ - pysqlite_state *state = connection->state; - assert(PyUnicode_Check(sql)); - Py_ssize_t size; - const char *sql_cstr = PyUnicode_AsUTF8AndSize(sql, &size); - if (sql_cstr == NULL) { - return NULL; - } - - sqlite3 *db = connection->db; - int max_length = sqlite3_limit(db, SQLITE_LIMIT_SQL_LENGTH, -1); - if (size > max_length) { - PyErr_SetString(connection->DataError, - "query string is too large"); - return NULL; - } - if (strlen(sql_cstr) != (size_t)size) { - PyErr_SetString(connection->ProgrammingError, - "the query contains a null character"); - return NULL; - } - - sqlite3_stmt *stmt; - const char *tail; - int rc; - Py_BEGIN_ALLOW_THREADS - rc = sqlite3_prepare_v2(db, sql_cstr, (int)size + 1, &stmt, &tail); - Py_END_ALLOW_THREADS - - if (rc != SQLITE_OK) { - _pysqlite_seterror(state, db); - return NULL; - } - - if (lstrip_sql(tail) != NULL) { - PyErr_SetString(connection->ProgrammingError, - "You can only execute one statement at a time."); - goto error; - } - - /* Determine if the statement is a DML statement. - SELECT is the only exception. See #9924. */ - int is_dml = 0; - const char *p = lstrip_sql(sql_cstr); - if (p != NULL) { - is_dml = (PyOS_strnicmp(p, "insert", 6) == 0) - || (PyOS_strnicmp(p, "update", 6) == 0) - || (PyOS_strnicmp(p, "delete", 6) == 0) - || (PyOS_strnicmp(p, "replace", 7) == 0); - } - - pysqlite_Statement *self = PyObject_GC_New(pysqlite_Statement, - state->StatementType); - if (self == NULL) { - goto error; - } - - self->st = stmt; - self->is_dml = is_dml; - - PyObject_GC_Track(self); - return self; - -error: - (void)sqlite3_finalize(stmt); - return NULL; -} - -static void -stmt_dealloc(pysqlite_Statement *self) -{ - PyTypeObject *tp = Py_TYPE(self); - PyObject_GC_UnTrack(self); - if (self->st) { - Py_BEGIN_ALLOW_THREADS - sqlite3_finalize(self->st); - Py_END_ALLOW_THREADS - self->st = 0; - } - tp->tp_free(self); - Py_DECREF(tp); -} - -static int -stmt_traverse(pysqlite_Statement *self, visitproc visit, void *arg) -{ - Py_VISIT(Py_TYPE(self)); - return 0; -} - -/* - * Strip leading whitespace and comments from incoming SQL (null terminated C - * string) and return a pointer to the first non-whitespace, non-comment - * character. - * - * This is used to check if somebody tries to execute more than one SQL query - * with one execute()/executemany() command, which the DB-API don't allow. - * - * It is also used to harden DML query detection. - */ -static inline const char * -lstrip_sql(const char *sql) -{ - // This loop is borrowed from the SQLite source code. - for (const char *pos = sql; *pos; pos++) { - switch (*pos) { - case ' ': - case '\t': - case '\f': - case '\n': - case '\r': - // Skip whitespace. - break; - case '-': - // Skip line comments. - if (pos[1] == '-') { - pos += 2; - while (pos[0] && pos[0] != '\n') { - pos++; - } - if (pos[0] == '\0') { - return NULL; - } - continue; - } - return pos; - case '/': - // Skip C style comments. - if (pos[1] == '*') { - pos += 2; - while (pos[0] && (pos[0] != '*' || pos[1] != '/')) { - pos++; - } - if (pos[0] == '\0') { - return NULL; - } - pos++; - continue; - } - return pos; - default: - return pos; - } - } - - return NULL; -} - -static PyType_Slot stmt_slots[] = { - {Py_tp_dealloc, stmt_dealloc}, - {Py_tp_traverse, stmt_traverse}, - {0, NULL}, -}; - -static PyType_Spec stmt_spec = { - .name = MODULE_NAME ".Statement", - .basicsize = sizeof(pysqlite_Statement), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), - .slots = stmt_slots, -}; - -int -pysqlite_statement_setup_types(PyObject *module) -{ - PyObject *type = PyType_FromModuleAndSpec(module, &stmt_spec, NULL); - if (type == NULL) { - return -1; - } - pysqlite_state *state = pysqlite_get_state(module); - state->StatementType = (PyTypeObject *)type; - return 0; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/statement.h b/contrib/tools/python3/src/Modules/_sqlite/statement.h deleted file mode 100644 index 11a6464b1a1..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/statement.h +++ /dev/null @@ -1,43 +0,0 @@ -/* statement.h - definitions for the statement type - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PYSQLITE_STATEMENT_H -#define PYSQLITE_STATEMENT_H -#define PY_SSIZE_T_CLEAN -#include "Python.h" - -#include "connection.h" -#include "sqlite3.h" - -typedef struct -{ - PyObject_HEAD - sqlite3_stmt* st; - int is_dml; -} pysqlite_Statement; - -pysqlite_Statement *pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql); - -int pysqlite_statement_setup_types(PyObject *module); - -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/util.c b/contrib/tools/python3/src/Modules/_sqlite/util.c deleted file mode 100644 index 2b3bbfefa3c..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/util.c +++ /dev/null @@ -1,167 +0,0 @@ -/* util.c - various utility functions - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#include "module.h" -#include "connection.h" - -// Returns non-NULL if a new exception should be raised -static PyObject * -get_exception_class(pysqlite_state *state, int errorcode) -{ - switch (errorcode) { - case SQLITE_OK: - PyErr_Clear(); - return NULL; - case SQLITE_INTERNAL: - case SQLITE_NOTFOUND: - return state->InternalError; - case SQLITE_NOMEM: - return PyErr_NoMemory(); - case SQLITE_ERROR: - case SQLITE_PERM: - case SQLITE_ABORT: - case SQLITE_BUSY: - case SQLITE_LOCKED: - case SQLITE_READONLY: - case SQLITE_INTERRUPT: - case SQLITE_IOERR: - case SQLITE_FULL: - case SQLITE_CANTOPEN: - case SQLITE_PROTOCOL: - case SQLITE_EMPTY: - case SQLITE_SCHEMA: - return state->OperationalError; - case SQLITE_CORRUPT: - return state->DatabaseError; - case SQLITE_TOOBIG: - return state->DataError; - case SQLITE_CONSTRAINT: - case SQLITE_MISMATCH: - return state->IntegrityError; - case SQLITE_MISUSE: - case SQLITE_RANGE: - return state->InterfaceError; - default: - return state->DatabaseError; - } -} - -static void -raise_exception(PyObject *type, int errcode, const char *errmsg) -{ - PyObject *exc = NULL; - PyObject *args[] = { PyUnicode_FromString(errmsg), }; - if (args[0] == NULL) { - goto exit; - } - exc = PyObject_Vectorcall(type, args, 1, NULL); - Py_DECREF(args[0]); - if (exc == NULL) { - goto exit; - } - - PyObject *code = PyLong_FromLong(errcode); - if (code == NULL) { - goto exit; - } - int rc = PyObject_SetAttrString(exc, "sqlite_errorcode", code); - Py_DECREF(code); - if (rc < 0) { - goto exit; - } - - const char *error_name = pysqlite_error_name(errcode); - PyObject *name; - if (error_name) { - name = PyUnicode_FromString(error_name); - } - else { - name = PyUnicode_InternFromString("unknown"); - } - if (name == NULL) { - goto exit; - } - rc = PyObject_SetAttrString(exc, "sqlite_errorname", name); - Py_DECREF(name); - if (rc < 0) { - goto exit; - } - - PyErr_SetObject(type, exc); - -exit: - Py_XDECREF(exc); -} - -/** - * Checks the SQLite error code and sets the appropriate DB-API exception. - * Returns the error code (0 means no error occurred). - */ -int -_pysqlite_seterror(pysqlite_state *state, sqlite3 *db) -{ - int errorcode = sqlite3_errcode(db); - PyObject *exc_class = get_exception_class(state, errorcode); - if (exc_class == NULL) { - // No new exception need be raised; just pass the error code - return errorcode; - } - - /* Create and set the exception. */ - int extended_errcode = sqlite3_extended_errcode(db); - const char *errmsg = sqlite3_errmsg(db); - raise_exception(exc_class, extended_errcode, errmsg); - return extended_errcode; -} - -#ifdef WORDS_BIGENDIAN -# define IS_LITTLE_ENDIAN 0 -#else -# define IS_LITTLE_ENDIAN 1 -#endif - -sqlite_int64 -_pysqlite_long_as_int64(PyObject * py_val) -{ - int overflow; - long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow); - if (value == -1 && PyErr_Occurred()) - return -1; - if (!overflow) { -# if SIZEOF_LONG_LONG > 8 - if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL) -# endif - return value; - } - else if (sizeof(value) < sizeof(sqlite_int64)) { - sqlite_int64 int64val; - if (_PyLong_AsByteArray((PyLongObject *)py_val, - (unsigned char *)&int64val, sizeof(int64val), - IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) { - return int64val; - } - } - PyErr_SetString(PyExc_OverflowError, - "Python int too large to convert to SQLite INTEGER"); - return -1; -} diff --git a/contrib/tools/python3/src/Modules/_sqlite/util.h b/contrib/tools/python3/src/Modules/_sqlite/util.h deleted file mode 100644 index a22bcd82d2a..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/util.h +++ /dev/null @@ -1,40 +0,0 @@ -/* util.h - various utility functions - * - * Copyright (C) 2005-2010 Gerhard Häring <[email protected]> - * - * This file is part of pysqlite. - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - */ - -#ifndef PYSQLITE_UTIL_H -#define PYSQLITE_UTIL_H -#define PY_SSIZE_T_CLEAN -#include "Python.h" -#include "pythread.h" -#include "sqlite3.h" -#include "connection.h" - -/** - * Checks the SQLite error code and sets the appropriate DB-API exception. - * Returns the error code (0 means no error occurred). - */ -int _pysqlite_seterror(pysqlite_state *state, sqlite3 *db); - -sqlite_int64 _pysqlite_long_as_int64(PyObject * value); - -#endif diff --git a/contrib/tools/python3/src/Modules/_sqlite/ya.make b/contrib/tools/python3/src/Modules/_sqlite/ya.make deleted file mode 100644 index f262ffabffc..00000000000 --- a/contrib/tools/python3/src/Modules/_sqlite/ya.make +++ /dev/null @@ -1,47 +0,0 @@ -# Generated by devtools/yamaker. - -LIBRARY() - -VERSION(3.12.2) - -ORIGINAL_SOURCE(https://github.com/python/cpython/archive/v3.12.2.tar.gz) - -LICENSE(Python-2.0) - -PEERDIR( - contrib/libs/sqlite3 -) - -ADDINCL( - contrib/libs/sqlite3 - contrib/tools/python3/src/Include - contrib/tools/python3/src/Include/internal -) - -PYTHON3_ADDINCL() - -NO_COMPILER_WARNINGS() - -NO_RUNTIME() - -CFLAGS( - -DMODULE_NAME=sqlite3 -) - -SRCS( - blob.c - connection.c - cursor.c - microprotocols.c - module.c - prepare_protocol.c - row.c - statement.c - util.c -) - -PY_REGISTER( - _sqlite3 -) - -END() |
