summaryrefslogtreecommitdiffstats
path: root/Modules/_sqlite
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2021-11-01 22:50:53 (GMT)
committerGitHub <noreply@github.com>2021-11-01 22:50:53 (GMT)
commitb6b38a82267ff70d2abaf2a8371327268887c97d (patch)
treeec057a13b8978411a6b29a4a3062bb2411cd22f0 /Modules/_sqlite
parente2063d6a1ebc3568e90a14ed163fa291b5977ae8 (diff)
downloadcpython-b6b38a82267ff70d2abaf2a8371327268887c97d.zip
cpython-b6b38a82267ff70d2abaf2a8371327268887c97d.tar.gz
cpython-b6b38a82267ff70d2abaf2a8371327268887c97d.tar.bz2
bpo-45243: Add support for setting/getting `sqlite3` connection limits (GH-28463)
Diffstat (limited to 'Modules/_sqlite')
-rw-r--r--Modules/_sqlite/clinic/connection.c.h79
-rw-r--r--Modules/_sqlite/connection.c53
-rw-r--r--Modules/_sqlite/module.c15
3 files changed, 146 insertions, 1 deletions
diff --git a/Modules/_sqlite/clinic/connection.c.h b/Modules/_sqlite/clinic/connection.c.h
index e9e3064..f9323eb 100644
--- a/Modules/_sqlite/clinic/connection.c.h
+++ b/Modules/_sqlite/clinic/connection.c.h
@@ -750,6 +750,83 @@ 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)(void(*)(void))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;
+}
+
#ifndef PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
#define PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
#endif /* !defined(PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF) */
@@ -757,4 +834,4 @@ exit:
#ifndef PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
#define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
#endif /* !defined(PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF) */
-/*[clinic end generated code: output=7567e5d716309258 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=0c3901153a3837a5 input=a9049054013a1b77]*/
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 94c38ad..f913267 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -1896,6 +1896,57 @@ pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
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 const char connection_doc[] =
PyDoc_STR("SQLite database connection object.");
@@ -1927,6 +1978,8 @@ static PyMethodDef connection_methods[] = {
PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
+ SETLIMIT_METHODDEF
+ GETLIMIT_METHODDEF
{NULL, NULL}
};
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index 912851b..8666af1 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -396,6 +396,21 @@ add_integer_constants(PyObject *module) {
#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
#undef ADD_INT
return 0;
}