bpo-40956: Convert _sqlite3.Connection to Argument Clinic (GH-23341)
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 69203f8..1c8f37e 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -37,6 +37,13 @@
#define HAVE_TRACE_V2
#endif
+#include "clinic/connection.c.h"
+/*[clinic input]
+module _sqlite3
+class _sqlite3.Connection "pysqlite_Connection *" "pysqlite_ConnectionType"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=aa796073bd8f69db]*/
+
_Py_IDENTIFIER(cursor);
static const char * const begin_statements[] = {
@@ -269,16 +276,19 @@ int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObjec
return 0;
}
-PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
-{
- static char *kwlist[] = {"factory", NULL};
- PyObject* factory = NULL;
- PyObject* cursor;
+/*[clinic input]
+_sqlite3.Connection.cursor as pysqlite_connection_cursor
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
- &factory)) {
- return NULL;
- }
+ 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;
@@ -309,7 +319,15 @@ PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args,
return cursor;
}
-PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
+/*[clinic input]
+_sqlite3.Connection.close as pysqlite_connection_close
+
+Closes the connection.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_close_impl(pysqlite_Connection *self)
+/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
{
int rc;
@@ -431,7 +449,15 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
}
}
-PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
+/*[clinic input]
+_sqlite3.Connection.rollback as pysqlite_connection_rollback
+
+Roll back the current transaction.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_rollback_impl(pysqlite_Connection *self)
+/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
{
int rc;
const char* tail;
@@ -780,27 +806,31 @@ static void _destructor(void* args)
Py_DECREF((PyObject*)args);
}
-PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
-{
- static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL};
+/*[clinic input]
+_sqlite3.Connection.create_function as pysqlite_connection_create_function
- PyObject* func;
- char* name;
- int narg;
+ name: str
+ narg: int
+ func: object
+ *
+ deterministic: bool = False
+
+Creates a new function. Non-standard.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_create_function_impl(pysqlite_Connection *self,
+ const char *name, int narg,
+ PyObject *func, int deterministic)
+/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
+{
int rc;
- int deterministic = 0;
int flags = SQLITE_UTF8;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|$p", kwlist,
- &name, &narg, &func, &deterministic))
- {
- return NULL;
- }
-
if (deterministic) {
#if SQLITE_VERSION_NUMBER < 3008003
PyErr_SetString(pysqlite_NotSupportedError,
@@ -834,23 +864,28 @@ PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObjec
Py_RETURN_NONE;
}
-PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
-{
- PyObject* aggregate_class;
+/*[clinic input]
+_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
- int n_arg;
- char* name;
- static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
+ name: str
+ n_arg: int
+ aggregate_class: object
+
+Creates a new aggregate. Non-standard.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
+ const char *name, int n_arg,
+ PyObject *aggregate_class)
+/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
+{
int rc;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
- kwlist, &name, &n_arg, &aggregate_class)) {
- return NULL;
- }
Py_INCREF(aggregate_class);
rc = sqlite3_create_function_v2(self->db,
name,
@@ -982,22 +1017,25 @@ static void _trace_callback(void* user_arg, const char* statement_string)
#endif
}
-static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
-{
- PyObject* authorizer_cb;
+/*[clinic input]
+_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
- static char *kwlist[] = { "authorizer_callback", NULL };
+ authorizer_callback as authorizer_cb: object
+
+Sets authorizer callback. Non-standard.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
+ PyObject *authorizer_cb)
+/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
+{
int rc;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
- kwlist, &authorizer_cb)) {
- return NULL;
- }
-
rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
if (rc != SQLITE_OK) {
PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
@@ -1010,22 +1048,25 @@ static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, P
Py_RETURN_NONE;
}
-static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
+/*[clinic input]
+_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
+
+ progress_handler: object
+ n: int
+
+Sets progress handler callback. Non-standard.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
+ PyObject *progress_handler,
+ int n)
+/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
{
- PyObject* progress_handler;
- int n;
-
- static char *kwlist[] = { "progress_handler", "n", NULL };
-
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
- kwlist, &progress_handler, &n)) {
- return NULL;
- }
-
if (progress_handler == Py_None) {
/* None clears the progress handler previously set */
sqlite3_progress_handler(self->db, 0, 0, (void*)0);
@@ -1038,28 +1079,34 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s
Py_RETURN_NONE;
}
-/*
- * Ref.
- * - https://sqlite.org/c3ref/c_trace.html
- * - https://sqlite.org/c3ref/trace_v2.html
- */
-static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
+/*[clinic input]
+_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
+
+ trace_callback: object
+ /
+
+Sets a trace callback called for each SQL statement (passed as unicode).
+
+Non-standard.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_set_trace_callback(pysqlite_Connection *self,
+ PyObject *trace_callback)
+/*[clinic end generated code: output=efd1bf439e81696c input=05a4a14360e0e034]*/
{
- PyObject* trace_callback;
-
- static char *kwlist[] = { "trace_callback", NULL };
-
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
- kwlist, &trace_callback)) {
- return NULL;
- }
-
if (trace_callback == Py_None) {
- /* None clears the trace callback previously set */
+ /*
+ * 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
@@ -1080,19 +1127,26 @@ static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* sel
}
#ifndef SQLITE_OMIT_LOAD_EXTENSION
-static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
+/*[clinic input]
+_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
+
+ enable as onoff: int
+ /
+
+Enable dynamic loading of SQLite extension modules. Non-standard.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
+ int onoff)
+/*[clinic end generated code: output=9cac37190d388baf input=7df2986f1602d6bd]*/
{
int rc;
- int onoff;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
- if (!PyArg_ParseTuple(args, "i", &onoff)) {
- return NULL;
- }
-
rc = sqlite3_enable_load_extension(self->db, onoff);
if (rc != SQLITE_OK) {
@@ -1103,20 +1157,27 @@ static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObj
}
}
-static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
+/*[clinic input]
+_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
+
+ name as extension_name: str
+ /
+
+Load SQLite extension module. Non-standard.
+[clinic start generated code]*/
+
+static PyObject *
+pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
+ const char *extension_name)
+/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
{
int rc;
- char* extension_name;
char* errmsg;
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}
- if (!PyArg_ParseTuple(args, "s", &extension_name)) {
- return NULL;
- }
-
rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
if (rc != 0) {
PyErr_SetString(pysqlite_OperationalError, errmsg);
@@ -1421,8 +1482,15 @@ pysqlite_collation_callback(
return result;
}
+/*[clinic input]
+_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
+
+Abort any pending database operation. Non-standard.
+[clinic start generated code]*/
+
static PyObject *
-pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
+pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
+/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
{
PyObject* retval = NULL;
@@ -1443,8 +1511,17 @@ pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
* 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.
+
+Non-standard.
+[clinic start generated code]*/
+
static PyObject *
-pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
+pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
+/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
{
_Py_IDENTIFIER(_iterdump);
PyObject* retval = NULL;
@@ -1637,12 +1714,22 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *
}
}
+/*[clinic input]
+_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
+
+ name: unicode
+ callback as callable: object
+ /
+
+Creates a collation function. Non-standard.
+[clinic start generated code]*/
+
static PyObject *
-pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
+pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
+ PyObject *name, PyObject *callable)
+/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
{
- PyObject* callable;
PyObject* uppercase_name = 0;
- PyObject* name;
PyObject* retval;
Py_ssize_t i, len;
_Py_IDENTIFIER(upper);
@@ -1655,11 +1742,6 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
goto finally;
}
- if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
- &name, &callable)) {
- goto finally;
- }
-
uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
&PyId_upper, name);
if (!uppercase_name) {
@@ -1725,28 +1807,43 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
return retval;
}
-/* Called when the connection is used as a context manager. Returns itself as a
- * convenience to the caller. */
+/*[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(pysqlite_Connection* self, PyObject* args)
+pysqlite_connection_enter_impl(pysqlite_Connection *self)
+/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
{
Py_INCREF(self);
return (PyObject*)self;
}
-/** Called when the connection is used as a context manager. If there was any
- * exception, a rollback takes place; otherwise we commit. */
+/*[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(pysqlite_Connection* self, PyObject* args)
+pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
+ PyObject *exc_value, PyObject *exc_tb)
+/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
{
- PyObject* exc_type, *exc_value, *exc_tb;
const char* method_name;
PyObject* result;
- if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
- return NULL;
- }
-
if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
method_name = "commit";
} else {
@@ -1773,48 +1870,31 @@ static PyGetSetDef connection_getset[] = {
};
static PyMethodDef connection_methods[] = {
- {"cursor", (PyCFunction)(void(*)(void))pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
- PyDoc_STR("Return a cursor for the connection.")},
- {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
- PyDoc_STR("Closes the connection.")},
+ PYSQLITE_CONNECTION_CLOSE_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_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
{"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
PyDoc_STR("Commit the current transaction.")},
- {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
- PyDoc_STR("Roll back the current transaction.")},
- {"create_function", (PyCFunction)(void(*)(void))pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
- PyDoc_STR("Creates a new function. Non-standard.")},
- {"create_aggregate", (PyCFunction)(void(*)(void))pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
- PyDoc_STR("Creates a new aggregate. Non-standard.")},
- {"set_authorizer", (PyCFunction)(void(*)(void))pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
- PyDoc_STR("Sets authorizer callback. Non-standard.")},
- #ifndef SQLITE_OMIT_LOAD_EXTENSION
- {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
- PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
- {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
- PyDoc_STR("Load SQLite extension module. Non-standard.")},
- #endif
- {"set_progress_handler", (PyCFunction)(void(*)(void))pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
- PyDoc_STR("Sets progress handler callback. Non-standard.")},
- {"set_trace_callback", (PyCFunction)(void(*)(void))pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
- PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
{"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
PyDoc_STR("Executes a SQL statement. Non-standard.")},
{"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
{"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
- {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
- PyDoc_STR("Creates a collation function. Non-standard.")},
- {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
- PyDoc_STR("Abort any pending database operation. Non-standard.")},
- {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
- PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
{"backup", (PyCFunction)(void(*)(void))pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("Makes a backup of the database. Non-standard.")},
- {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
- PyDoc_STR("For context manager. Non-standard.")},
- {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
- PyDoc_STR("For context manager. Non-standard.")},
{NULL, NULL}
};