Skip to content

Commit c96cc08

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43434: Move sqlite3.connect audit events to sqlite3.Connection.__init__ (GH-25818)
1 parent 37e0c78 commit c96cc08

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

Lib/test/audit-tests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,14 @@ def hook(event, *args):
367367
print(event, *args)
368368

369369
sys.addaudithook(hook)
370-
cx = sqlite3.connect(":memory:")
370+
cx1 = sqlite3.connect(":memory:")
371+
cx2 = sqlite3.Connection(":memory:")
371372

372373
# Configured without --enable-loadable-sqlite-extensions
373374
if hasattr(sqlite3.Connection, "enable_load_extension"):
374-
cx.enable_load_extension(False)
375+
cx1.enable_load_extension(False)
375376
try:
376-
cx.load_extension("test")
377+
cx1.load_extension("test")
377378
except sqlite3.OperationalError:
378379
pass
379380
else:

Lib/test/test_audit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test_sqlite3(self):
158158
if support.verbose:
159159
print(*events, sep='\n')
160160
actual = [ev[0] for ev in events]
161-
expected = ["sqlite3.connect", "sqlite3.connect/handle"]
161+
expected = ["sqlite3.connect", "sqlite3.connect/handle"] * 2
162162

163163
if hasattr(sqlite3.Connection, "enable_load_extension"):
164164
expected += [
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Creating :class:`sqlite3.Connection` objects now also produces
2+
``sqlite3.connect`` and ``sqlite3.connect/handle`` :ref:`auditing events
3+
<auditing>`. Previously these events were only produced by
4+
:func:`sqlite3.connect` calls. Patch by Erlend E. Aasland.

Modules/_sqlite/connection.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
8686
return -1;
8787
}
8888

89+
if (PySys_Audit("sqlite3.connect", "O", database_obj) < 0) {
90+
return -1;
91+
}
92+
8993
database = PyBytes_AsString(database_obj);
9094

9195
self->initialized = 1;
@@ -179,6 +183,10 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
179183
self->ProgrammingError = pysqlite_ProgrammingError;
180184
self->NotSupportedError = pysqlite_NotSupportedError;
181185

186+
if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
187+
return -1;
188+
}
189+
182190
return 0;
183191
}
184192

Modules/_sqlite/module.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,11 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
9191
factory = (PyObject*)pysqlite_ConnectionType;
9292
}
9393

94-
if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
95-
return NULL;
96-
}
97-
9894
result = PyObject_Call(factory, args, kwargs);
9995
if (result == NULL) {
10096
return NULL;
10197
}
10298

103-
if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
104-
Py_DECREF(result);
105-
return NULL;
106-
}
107-
10899
return result;
109100
}
110101

0 commit comments

Comments
 (0)