[Python-checkins] bpo-44991: Normalise `sqlite3` callback naming (GH-28088)

encukou webhook-mailer at python.org
Tue Sep 7 07:43:48 EDT 2021


https://github.com/python/cpython/commit/0474d06008f8c9eba660ed20d336ffdc5c4db121
commit: 0474d06008f8c9eba660ed20d336ffdc5c4db121
branch: main
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: encukou <encukou at gmail.com>
date: 2021-09-07T13:43:44+02:00
summary:

bpo-44991: Normalise `sqlite3` callback naming (GH-28088)

- all callbacks are now named xxx_callback
- normalise callable naming in set_*() functions
- normalise context argument naming in callbacks

The sqlite code is being "touched" in bpo-42064 (and related issues);
this style change makes it easier to work with and review.

files:
M Modules/_sqlite/clinic/connection.c.h
M Modules/_sqlite/connection.c

diff --git a/Modules/_sqlite/clinic/connection.c.h b/Modules/_sqlite/clinic/connection.c.h
index 1626e1c59c06cc..315d163dde668f 100644
--- a/Modules/_sqlite/clinic/connection.c.h
+++ b/Modules/_sqlite/clinic/connection.c.h
@@ -321,7 +321,7 @@ PyDoc_STRVAR(pysqlite_connection_set_authorizer__doc__,
 
 static PyObject *
 pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
-                                        PyObject *authorizer_cb);
+                                        PyObject *callable);
 
 static PyObject *
 pysqlite_connection_set_authorizer(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
@@ -330,14 +330,14 @@ pysqlite_connection_set_authorizer(pysqlite_Connection *self, PyObject *const *a
     static const char * const _keywords[] = {"authorizer_callback", NULL};
     static _PyArg_Parser _parser = {NULL, _keywords, "set_authorizer", 0};
     PyObject *argsbuf[1];
-    PyObject *authorizer_cb;
+    PyObject *callable;
 
     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
     if (!args) {
         goto exit;
     }
-    authorizer_cb = args[0];
-    return_value = pysqlite_connection_set_authorizer_impl(self, authorizer_cb);
+    callable = args[0];
+    return_value = pysqlite_connection_set_authorizer_impl(self, callable);
 
 exit:
     return return_value;
@@ -354,8 +354,7 @@ PyDoc_STRVAR(pysqlite_connection_set_progress_handler__doc__,
 
 static PyObject *
 pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
-                                              PyObject *progress_handler,
-                                              int n);
+                                              PyObject *callable, int n);
 
 static PyObject *
 pysqlite_connection_set_progress_handler(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
@@ -364,19 +363,19 @@ pysqlite_connection_set_progress_handler(pysqlite_Connection *self, PyObject *co
     static const char * const _keywords[] = {"progress_handler", "n", NULL};
     static _PyArg_Parser _parser = {NULL, _keywords, "set_progress_handler", 0};
     PyObject *argsbuf[2];
-    PyObject *progress_handler;
+    PyObject *callable;
     int n;
 
     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
     if (!args) {
         goto exit;
     }
-    progress_handler = args[0];
+    callable = args[0];
     n = _PyLong_AsInt(args[1]);
     if (n == -1 && PyErr_Occurred()) {
         goto exit;
     }
-    return_value = pysqlite_connection_set_progress_handler_impl(self, progress_handler, n);
+    return_value = pysqlite_connection_set_progress_handler_impl(self, callable, n);
 
 exit:
     return return_value;
@@ -395,7 +394,7 @@ PyDoc_STRVAR(pysqlite_connection_set_trace_callback__doc__,
 
 static PyObject *
 pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
-                                            PyObject *trace_callback);
+                                            PyObject *callable);
 
 static PyObject *
 pysqlite_connection_set_trace_callback(pysqlite_Connection *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
@@ -404,14 +403,14 @@ pysqlite_connection_set_trace_callback(pysqlite_Connection *self, PyObject *cons
     static const char * const _keywords[] = {"trace_callback", NULL};
     static _PyArg_Parser _parser = {NULL, _keywords, "set_trace_callback", 0};
     PyObject *argsbuf[1];
-    PyObject *trace_callback;
+    PyObject *callable;
 
     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
     if (!args) {
         goto exit;
     }
-    trace_callback = args[0];
-    return_value = pysqlite_connection_set_trace_callback_impl(self, trace_callback);
+    callable = args[0];
+    return_value = pysqlite_connection_set_trace_callback_impl(self, callable);
 
 exit:
     return return_value;
@@ -817,4 +816,4 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss
 #ifndef PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
     #define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
 #endif /* !defined(PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF) */
-/*[clinic end generated code: output=a7a899c4e41381ac input=a9049054013a1b77]*/
+/*[clinic end generated code: output=9c0dfc6c1ebf9039 input=a9049054013a1b77]*/
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 864877c0c34264..0780d41c5a0c39 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -654,7 +654,8 @@ _pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv
     PyGILState_Release(threadstate);
 }
 
-static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
+static void
+step_callback(sqlite3_context *context, int argc, sqlite3_value **params)
 {
     PyGILState_STATE threadstate = PyGILState_Ensure();
 
@@ -702,7 +703,7 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_
 }
 
 static void
-_pysqlite_final_callback(sqlite3_context *context)
+final_callback(sqlite3_context *context)
 {
     PyGILState_STATE threadstate = PyGILState_Ensure();
 
@@ -800,7 +801,7 @@ free_callback_context(callback_context *ctx)
 }
 
 static void
-_destructor(void *ctx)
+destructor_callback(void *ctx)
 {
     if (ctx != NULL) {
         // This function may be called without the GIL held, so we need to
@@ -858,7 +859,7 @@ pysqlite_connection_create_function_impl(pysqlite_Connection *self,
                                     _pysqlite_func_callback,
                                     NULL,
                                     NULL,
-                                    &_destructor);  // will decref func
+                                    &destructor_callback);  // will decref func
 
     if (rc != SQLITE_OK) {
         /* Workaround for SQLite bug: no error code or string is available here */
@@ -897,9 +898,9 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
     }
     rc = sqlite3_create_function_v2(self->db, name, n_arg, SQLITE_UTF8, ctx,
                                     0,
-                                    &_pysqlite_step_callback,
-                                    &_pysqlite_final_callback,
-                                    &_destructor); // will decref func
+                                    &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");
@@ -908,14 +909,18 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
     Py_RETURN_NONE;
 }
 
-static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
+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;
 
-    ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
+    ret = PyObject_CallFunction((PyObject*)ctx, "issss", action, arg1, arg2,
+                                dbname, access_attempt_source);
 
     if (ret == NULL) {
         pysqlite_state *state = pysqlite_get_state(NULL);
@@ -952,13 +957,14 @@ static int _authorizer_callback(void* user_arg, int action, const char* arg1, co
     return rc;
 }
 
-static int _progress_handler(void* user_arg)
+static int
+progress_callback(void *ctx)
 {
     PyGILState_STATE gilstate = PyGILState_Ensure();
 
     int rc;
     PyObject *ret;
-    ret = _PyObject_CallNoArg((PyObject*)user_arg);
+    ret = _PyObject_CallNoArg((PyObject*)ctx);
 
     if (!ret) {
         /* abort query if error occurred */
@@ -989,9 +995,12 @@ static int _progress_handler(void* user_arg)
  * may change in future releases. Callback implementations should return zero
  * to ensure future compatibility.
  */
-static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
+static int
+trace_callback(unsigned int type, void *ctx, void *prepared_statement,
+               void *statement_string)
 #else
-static void _trace_callback(void* user_arg, const char* statement_string)
+static void
+trace_callback(void *ctx, const char *statement_string)
 #endif
 {
 #ifdef HAVE_TRACE_V2
@@ -1007,7 +1016,7 @@ static void _trace_callback(void* user_arg, const char* statement_string)
     py_statement = PyUnicode_DecodeUTF8(statement_string,
             strlen(statement_string), "replace");
     if (py_statement) {
-        ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
+        ret = PyObject_CallOneArg((PyObject*)ctx, py_statement);
         Py_DECREF(py_statement);
     }
 
@@ -1032,29 +1041,29 @@ static void _trace_callback(void* user_arg, const char* statement_string)
 /*[clinic input]
 _sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
 
-    authorizer_callback as authorizer_cb: object
+    authorizer_callback as callable: 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]*/
+                                        PyObject *callable)
+/*[clinic end generated code: output=c193601e9e8a5116 input=ec104f130b82050b]*/
 {
     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
         return NULL;
     }
 
     int rc;
-    if (authorizer_cb == Py_None) {
+    if (callable == Py_None) {
         rc = sqlite3_set_authorizer(self->db, NULL, NULL);
         Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
     }
     else {
-        Py_INCREF(authorizer_cb);
-        Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
-        rc = sqlite3_set_authorizer(self->db, _authorizer_callback, authorizer_cb);
+        Py_INCREF(callable);
+        Py_XSETREF(self->function_pinboard_authorizer_cb, callable);
+        rc = sqlite3_set_authorizer(self->db, authorizer_callback, callable);
     }
     if (rc != SQLITE_OK) {
         PyErr_SetString(self->OperationalError,
@@ -1068,7 +1077,7 @@ pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
 /*[clinic input]
 _sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
 
-    progress_handler: object
+    progress_handler as callable: object
     n: int
 
 Sets progress handler callback. Non-standard.
@@ -1076,22 +1085,21 @@ Sets progress handler callback. Non-standard.
 
 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 *callable, int n)
+/*[clinic end generated code: output=ba14008a483d7a53 input=3cf56d045f130a84]*/
 {
     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
         return NULL;
     }
 
-    if (progress_handler == Py_None) {
+    if (callable == Py_None) {
         /* None clears the progress handler previously set */
         sqlite3_progress_handler(self->db, 0, 0, (void*)0);
         Py_XSETREF(self->function_pinboard_progress_handler, NULL);
     } else {
-        sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
-        Py_INCREF(progress_handler);
-        Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
+        sqlite3_progress_handler(self->db, n, progress_callback, callable);
+        Py_INCREF(callable);
+        Py_XSETREF(self->function_pinboard_progress_handler, callable);
     }
     Py_RETURN_NONE;
 }
@@ -1099,7 +1107,7 @@ pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
 /*[clinic input]
 _sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
 
-    trace_callback: object
+    trace_callback as callable: object
 
 Sets a trace callback called for each SQL statement (passed as unicode).
 
@@ -1108,14 +1116,14 @@ Non-standard.
 
 static PyObject *
 pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
-                                            PyObject *trace_callback)
-/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
+                                            PyObject *callable)
+/*[clinic end generated code: output=c9fd551e359165d3 input=d76eabbb633057bc]*/
 {
     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
         return NULL;
     }
 
-    if (trace_callback == Py_None) {
+    if (callable == Py_None) {
         /*
          * None clears the trace callback previously set
          *
@@ -1131,12 +1139,12 @@ pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
         Py_XSETREF(self->function_pinboard_trace_callback, NULL);
     } else {
 #ifdef HAVE_TRACE_V2
-        sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
+        sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, trace_callback, callable);
 #else
-        sqlite3_trace(self->db, _trace_callback, trace_callback);
+        sqlite3_trace(self->db, trace_callback, callable);
 #endif
-        Py_INCREF(trace_callback);
-        Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
+        Py_INCREF(callable);
+        Py_XSETREF(self->function_pinboard_trace_callback, callable);
     }
 
     Py_RETURN_NONE;
@@ -1726,7 +1734,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
         }
         rc = sqlite3_create_collation_v2(self->db, name, flags, ctx,
                                          &pysqlite_collation_callback,
-                                         &_destructor);
+                                         &destructor_callback);
     }
 
     if (rc != SQLITE_OK) {



More information about the Python-checkins mailing list