Skip to content

Commit 0474d06

Browse files
author
Erlend Egeberg Aasland
authored
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.
1 parent fa2c0b8 commit 0474d06

File tree

2 files changed

+59
-52
lines changed

2 files changed

+59
-52
lines changed

Modules/_sqlite/clinic/connection.c.h

+13-14
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ PyDoc_STRVAR(pysqlite_connection_set_authorizer__doc__,
321321

322322
static PyObject *
323323
pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
324-
PyObject *authorizer_cb);
324+
PyObject *callable);
325325

326326
static PyObject *
327327
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
330330
static const char * const _keywords[] = {"authorizer_callback", NULL};
331331
static _PyArg_Parser _parser = {NULL, _keywords, "set_authorizer", 0};
332332
PyObject *argsbuf[1];
333-
PyObject *authorizer_cb;
333+
PyObject *callable;
334334

335335
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
336336
if (!args) {
337337
goto exit;
338338
}
339-
authorizer_cb = args[0];
340-
return_value = pysqlite_connection_set_authorizer_impl(self, authorizer_cb);
339+
callable = args[0];
340+
return_value = pysqlite_connection_set_authorizer_impl(self, callable);
341341

342342
exit:
343343
return return_value;
@@ -354,8 +354,7 @@ PyDoc_STRVAR(pysqlite_connection_set_progress_handler__doc__,
354354

355355
static PyObject *
356356
pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
357-
PyObject *progress_handler,
358-
int n);
357+
PyObject *callable, int n);
359358

360359
static PyObject *
361360
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
364363
static const char * const _keywords[] = {"progress_handler", "n", NULL};
365364
static _PyArg_Parser _parser = {NULL, _keywords, "set_progress_handler", 0};
366365
PyObject *argsbuf[2];
367-
PyObject *progress_handler;
366+
PyObject *callable;
368367
int n;
369368

370369
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
371370
if (!args) {
372371
goto exit;
373372
}
374-
progress_handler = args[0];
373+
callable = args[0];
375374
n = _PyLong_AsInt(args[1]);
376375
if (n == -1 && PyErr_Occurred()) {
377376
goto exit;
378377
}
379-
return_value = pysqlite_connection_set_progress_handler_impl(self, progress_handler, n);
378+
return_value = pysqlite_connection_set_progress_handler_impl(self, callable, n);
380379

381380
exit:
382381
return return_value;
@@ -395,7 +394,7 @@ PyDoc_STRVAR(pysqlite_connection_set_trace_callback__doc__,
395394

396395
static PyObject *
397396
pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
398-
PyObject *trace_callback);
397+
PyObject *callable);
399398

400399
static PyObject *
401400
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
404403
static const char * const _keywords[] = {"trace_callback", NULL};
405404
static _PyArg_Parser _parser = {NULL, _keywords, "set_trace_callback", 0};
406405
PyObject *argsbuf[1];
407-
PyObject *trace_callback;
406+
PyObject *callable;
408407

409408
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
410409
if (!args) {
411410
goto exit;
412411
}
413-
trace_callback = args[0];
414-
return_value = pysqlite_connection_set_trace_callback_impl(self, trace_callback);
412+
callable = args[0];
413+
return_value = pysqlite_connection_set_trace_callback_impl(self, callable);
415414

416415
exit:
417416
return return_value;
@@ -817,4 +816,4 @@ pysqlite_connection_exit(pysqlite_Connection *self, PyObject *const *args, Py_ss
817816
#ifndef PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
818817
#define PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
819818
#endif /* !defined(PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF) */
820-
/*[clinic end generated code: output=a7a899c4e41381ac input=a9049054013a1b77]*/
819+
/*[clinic end generated code: output=9c0dfc6c1ebf9039 input=a9049054013a1b77]*/

Modules/_sqlite/connection.c

+46-38
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ _pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv
654654
PyGILState_Release(threadstate);
655655
}
656656

657-
static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
657+
static void
658+
step_callback(sqlite3_context *context, int argc, sqlite3_value **params)
658659
{
659660
PyGILState_STATE threadstate = PyGILState_Ensure();
660661

@@ -702,7 +703,7 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_
702703
}
703704

704705
static void
705-
_pysqlite_final_callback(sqlite3_context *context)
706+
final_callback(sqlite3_context *context)
706707
{
707708
PyGILState_STATE threadstate = PyGILState_Ensure();
708709

@@ -800,7 +801,7 @@ free_callback_context(callback_context *ctx)
800801
}
801802

802803
static void
803-
_destructor(void *ctx)
804+
destructor_callback(void *ctx)
804805
{
805806
if (ctx != NULL) {
806807
// 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,
858859
_pysqlite_func_callback,
859860
NULL,
860861
NULL,
861-
&_destructor); // will decref func
862+
&destructor_callback); // will decref func
862863

863864
if (rc != SQLITE_OK) {
864865
/* Workaround for SQLite bug: no error code or string is available here */
@@ -897,9 +898,9 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
897898
}
898899
rc = sqlite3_create_function_v2(self->db, name, n_arg, SQLITE_UTF8, ctx,
899900
0,
900-
&_pysqlite_step_callback,
901-
&_pysqlite_final_callback,
902-
&_destructor); // will decref func
901+
&step_callback,
902+
&final_callback,
903+
&destructor_callback); // will decref func
903904
if (rc != SQLITE_OK) {
904905
/* Workaround for SQLite bug: no error code or string is available here */
905906
PyErr_SetString(self->OperationalError, "Error creating aggregate");
@@ -908,14 +909,18 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
908909
Py_RETURN_NONE;
909910
}
910911

911-
static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
912+
static int
913+
authorizer_callback(void *ctx, int action, const char *arg1,
914+
const char *arg2 , const char *dbname,
915+
const char *access_attempt_source)
912916
{
913917
PyGILState_STATE gilstate = PyGILState_Ensure();
914918

915919
PyObject *ret;
916920
int rc;
917921

918-
ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
922+
ret = PyObject_CallFunction((PyObject*)ctx, "issss", action, arg1, arg2,
923+
dbname, access_attempt_source);
919924

920925
if (ret == NULL) {
921926
pysqlite_state *state = pysqlite_get_state(NULL);
@@ -952,13 +957,14 @@ static int _authorizer_callback(void* user_arg, int action, const char* arg1, co
952957
return rc;
953958
}
954959

955-
static int _progress_handler(void* user_arg)
960+
static int
961+
progress_callback(void *ctx)
956962
{
957963
PyGILState_STATE gilstate = PyGILState_Ensure();
958964

959965
int rc;
960966
PyObject *ret;
961-
ret = _PyObject_CallNoArg((PyObject*)user_arg);
967+
ret = _PyObject_CallNoArg((PyObject*)ctx);
962968

963969
if (!ret) {
964970
/* abort query if error occurred */
@@ -989,9 +995,12 @@ static int _progress_handler(void* user_arg)
989995
* may change in future releases. Callback implementations should return zero
990996
* to ensure future compatibility.
991997
*/
992-
static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
998+
static int
999+
trace_callback(unsigned int type, void *ctx, void *prepared_statement,
1000+
void *statement_string)
9931001
#else
994-
static void _trace_callback(void* user_arg, const char* statement_string)
1002+
static void
1003+
trace_callback(void *ctx, const char *statement_string)
9951004
#endif
9961005
{
9971006
#ifdef HAVE_TRACE_V2
@@ -1007,7 +1016,7 @@ static void _trace_callback(void* user_arg, const char* statement_string)
10071016
py_statement = PyUnicode_DecodeUTF8(statement_string,
10081017
strlen(statement_string), "replace");
10091018
if (py_statement) {
1010-
ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
1019+
ret = PyObject_CallOneArg((PyObject*)ctx, py_statement);
10111020
Py_DECREF(py_statement);
10121021
}
10131022

@@ -1032,29 +1041,29 @@ static void _trace_callback(void* user_arg, const char* statement_string)
10321041
/*[clinic input]
10331042
_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
10341043
1035-
authorizer_callback as authorizer_cb: object
1044+
authorizer_callback as callable: object
10361045
10371046
Sets authorizer callback. Non-standard.
10381047
[clinic start generated code]*/
10391048

10401049
static PyObject *
10411050
pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1042-
PyObject *authorizer_cb)
1043-
/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1051+
PyObject *callable)
1052+
/*[clinic end generated code: output=c193601e9e8a5116 input=ec104f130b82050b]*/
10441053
{
10451054
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
10461055
return NULL;
10471056
}
10481057

10491058
int rc;
1050-
if (authorizer_cb == Py_None) {
1059+
if (callable == Py_None) {
10511060
rc = sqlite3_set_authorizer(self->db, NULL, NULL);
10521061
Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
10531062
}
10541063
else {
1055-
Py_INCREF(authorizer_cb);
1056-
Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
1057-
rc = sqlite3_set_authorizer(self->db, _authorizer_callback, authorizer_cb);
1064+
Py_INCREF(callable);
1065+
Py_XSETREF(self->function_pinboard_authorizer_cb, callable);
1066+
rc = sqlite3_set_authorizer(self->db, authorizer_callback, callable);
10581067
}
10591068
if (rc != SQLITE_OK) {
10601069
PyErr_SetString(self->OperationalError,
@@ -1068,38 +1077,37 @@ pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
10681077
/*[clinic input]
10691078
_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
10701079
1071-
progress_handler: object
1080+
progress_handler as callable: object
10721081
n: int
10731082
10741083
Sets progress handler callback. Non-standard.
10751084
[clinic start generated code]*/
10761085

10771086
static PyObject *
10781087
pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1079-
PyObject *progress_handler,
1080-
int n)
1081-
/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
1088+
PyObject *callable, int n)
1089+
/*[clinic end generated code: output=ba14008a483d7a53 input=3cf56d045f130a84]*/
10821090
{
10831091
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
10841092
return NULL;
10851093
}
10861094

1087-
if (progress_handler == Py_None) {
1095+
if (callable == Py_None) {
10881096
/* None clears the progress handler previously set */
10891097
sqlite3_progress_handler(self->db, 0, 0, (void*)0);
10901098
Py_XSETREF(self->function_pinboard_progress_handler, NULL);
10911099
} else {
1092-
sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
1093-
Py_INCREF(progress_handler);
1094-
Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
1100+
sqlite3_progress_handler(self->db, n, progress_callback, callable);
1101+
Py_INCREF(callable);
1102+
Py_XSETREF(self->function_pinboard_progress_handler, callable);
10951103
}
10961104
Py_RETURN_NONE;
10971105
}
10981106

10991107
/*[clinic input]
11001108
_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
11011109
1102-
trace_callback: object
1110+
trace_callback as callable: object
11031111
11041112
Sets a trace callback called for each SQL statement (passed as unicode).
11051113
@@ -1108,14 +1116,14 @@ Non-standard.
11081116

11091117
static PyObject *
11101118
pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1111-
PyObject *trace_callback)
1112-
/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
1119+
PyObject *callable)
1120+
/*[clinic end generated code: output=c9fd551e359165d3 input=d76eabbb633057bc]*/
11131121
{
11141122
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
11151123
return NULL;
11161124
}
11171125

1118-
if (trace_callback == Py_None) {
1126+
if (callable == Py_None) {
11191127
/*
11201128
* None clears the trace callback previously set
11211129
*
@@ -1131,12 +1139,12 @@ pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
11311139
Py_XSETREF(self->function_pinboard_trace_callback, NULL);
11321140
} else {
11331141
#ifdef HAVE_TRACE_V2
1134-
sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1142+
sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, trace_callback, callable);
11351143
#else
1136-
sqlite3_trace(self->db, _trace_callback, trace_callback);
1144+
sqlite3_trace(self->db, trace_callback, callable);
11371145
#endif
1138-
Py_INCREF(trace_callback);
1139-
Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
1146+
Py_INCREF(callable);
1147+
Py_XSETREF(self->function_pinboard_trace_callback, callable);
11401148
}
11411149

11421150
Py_RETURN_NONE;
@@ -1726,7 +1734,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
17261734
}
17271735
rc = sqlite3_create_collation_v2(self->db, name, flags, ctx,
17281736
&pysqlite_collation_callback,
1729-
&_destructor);
1737+
&destructor_callback);
17301738
}
17311739

17321740
if (rc != SQLITE_OK) {

0 commit comments

Comments
 (0)