Skip to content

Commit

Permalink
bpo-43434: Move sqlite3.connect audit events to sqlite3.Connection.__…
Browse files Browse the repository at this point in the history
…init__ (GH-25818)
  • Loading branch information
Erlend Egeberg Aasland authored May 2, 2021
1 parent 37e0c78 commit c96cc08
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
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

0 comments on commit c96cc08

Please sign in to comment.