Skip to content

bpo-43434: Move sqlite3.connect audit events to sqlite3.Connection.__init__ #25818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,14 @@ def hook(event, *args):
print(event, *args)

sys.addaudithook(hook)
cx = sqlite3.connect(":memory:")
cx1 = sqlite3.connect(":memory:")
cx2 = sqlite3.Connection(":memory:")

# Configured without --enable-loadable-sqlite-extensions
if hasattr(sqlite3.Connection, "enable_load_extension"):
cx.enable_load_extension(False)
cx1.enable_load_extension(False)
try:
cx.load_extension("test")
cx1.load_extension("test")
except sqlite3.OperationalError:
pass
else:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_sqlite3(self):
if support.verbose:
print(*events, sep='\n')
actual = [ev[0] for ev in events]
expected = ["sqlite3.connect", "sqlite3.connect/handle"]
expected = ["sqlite3.connect", "sqlite3.connect/handle"] * 2

if hasattr(sqlite3.Connection, "enable_load_extension"):
expected += [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Creating :class:`sqlite3.Connection` objects now also produces
``sqlite3.connect`` and ``sqlite3.connect/handle`` :ref:`auditing events
<auditing>`. Previously these events were only produced by
:func:`sqlite3.connect` calls. Patch by Erlend E. Aasland.
8 changes: 8 additions & 0 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
return -1;
}

if (PySys_Audit("sqlite3.connect", "O", database_obj) < 0) {
return -1;
}

database = PyBytes_AsString(database_obj);

self->initialized = 1;
Expand Down Expand Up @@ -179,6 +183,10 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
self->ProgrammingError = pysqlite_ProgrammingError;
self->NotSupportedError = pysqlite_NotSupportedError;

if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
return -1;
}

return 0;
}

Expand Down
9 changes: 0 additions & 9 deletions Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,11 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
factory = (PyObject*)pysqlite_ConnectionType;
}

if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
return NULL;
}

result = PyObject_Call(factory, args, kwargs);
if (result == NULL) {
return NULL;
}

if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
Py_DECREF(result);
return NULL;
}

return result;
}

Expand Down