Skip to content

Commit b19f455

Browse files
author
Erlend Egeberg Aasland
authored
bpo-44491: Allow clearing the sqlite3 authoriser callback (GH-26863)
1 parent 18ba1ff commit b19f455

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

Doc/library/sqlite3.rst

+5
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ Connection Objects
430430
argument and the meaning of the second and third argument depending on the first
431431
one. All necessary constants are available in the :mod:`sqlite3` module.
432432

433+
Passing :const:`None` as *authorizer_callback* will disable the authorizer.
434+
435+
.. versionchanged:: 3.11
436+
Added support for disabling the authorizer using :const:`None`.
437+
433438

434439
.. method:: set_progress_handler(handler, n)
435440

Doc/whatsnew/3.11.rst

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ math
106106
Dickinson in :issue:`44339`.)
107107

108108

109+
sqlite3
110+
-------
111+
112+
* You can now disable the authorizer by passing :const:`None` to
113+
:meth:`~sqlite3.Connection.set_authorizer`.
114+
(Contributed by Erlend E. Aasland in :issue:`44491`.)
115+
116+
109117
Removed
110118
=======
111119
* :class:`smtpd.MailmanProxy` is now removed as it is unusable without

Lib/sqlite3/test/dbapi.py

+1
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ def test_check_connection_thread(self):
652652
lambda: self.con.rollback(),
653653
lambda: self.con.close(),
654654
lambda: self.con.set_trace_callback(None),
655+
lambda: self.con.set_authorizer(None),
655656
lambda: self.con.create_collation("foo", None),
656657
]
657658
for fn in fns:

Lib/sqlite3/test/userfunctions.py

+6
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ def test_column_access(self):
522522
self.con.execute("select c2 from t1")
523523
self.assertIn('prohibited', str(cm.exception))
524524

525+
def test_clear_authorizer(self):
526+
self.con.set_authorizer(None)
527+
self.con.execute("select * from t2")
528+
self.con.execute("select c2 from t1")
529+
530+
525531
class AuthorizerRaiseExceptionTests(AuthorizerTests):
526532
@staticmethod
527533
def authorizer_cb(action, arg1, arg2, dbname, source):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Allow clearing the :mod:`sqlite3` authorizer callback by passing
2+
:const:``None`` to :meth:`~sqlite3.Connection.set_authorizer`. Patch by
3+
Erlend E. Aasland.

Modules/_sqlite/connection.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -1053,20 +1053,24 @@ pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
10531053
PyObject *authorizer_cb)
10541054
/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
10551055
{
1056-
int rc;
1057-
10581056
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
10591057
return NULL;
10601058
}
10611059

1062-
rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1060+
int rc;
1061+
if (authorizer_cb == Py_None) {
1062+
rc = sqlite3_set_authorizer(self->db, NULL, NULL);
1063+
Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
1064+
}
1065+
else {
1066+
Py_INCREF(authorizer_cb);
1067+
Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
1068+
rc = sqlite3_set_authorizer(self->db, _authorizer_callback, authorizer_cb);
1069+
}
10631070
if (rc != SQLITE_OK) {
10641071
PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
10651072
Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
10661073
return NULL;
1067-
} else {
1068-
Py_INCREF(authorizer_cb);
1069-
Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
10701074
}
10711075
Py_RETURN_NONE;
10721076
}

0 commit comments

Comments
 (0)