bpo-43434: Move sqlite3.connect audit events to sqlite3.Connection.__init__ (GH-25818)

This commit is contained in:
Erlend Egeberg Aasland 2021-05-02 23:25:17 +02:00 committed by GitHub
parent 37e0c7850d
commit c96cc089f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 13 deletions

View file

@ -367,13 +367,14 @@ def test_sqlite3():
print(event, *args) print(event, *args)
sys.addaudithook(hook) sys.addaudithook(hook)
cx = sqlite3.connect(":memory:") cx1 = sqlite3.connect(":memory:")
cx2 = sqlite3.Connection(":memory:")
# Configured without --enable-loadable-sqlite-extensions # Configured without --enable-loadable-sqlite-extensions
if hasattr(sqlite3.Connection, "enable_load_extension"): if hasattr(sqlite3.Connection, "enable_load_extension"):
cx.enable_load_extension(False) cx1.enable_load_extension(False)
try: try:
cx.load_extension("test") cx1.load_extension("test")
except sqlite3.OperationalError: except sqlite3.OperationalError:
pass pass
else: else:

View file

@ -158,7 +158,7 @@ class AuditTest(unittest.TestCase):
if support.verbose: if support.verbose:
print(*events, sep='\n') print(*events, sep='\n')
actual = [ev[0] for ev in events] 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"): if hasattr(sqlite3.Connection, "enable_load_extension"):
expected += [ expected += [

View file

@ -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.

View file

@ -86,6 +86,10 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
return -1; return -1;
} }
if (PySys_Audit("sqlite3.connect", "O", database_obj) < 0) {
return -1;
}
database = PyBytes_AsString(database_obj); database = PyBytes_AsString(database_obj);
self->initialized = 1; self->initialized = 1;
@ -179,6 +183,10 @@ pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
self->ProgrammingError = pysqlite_ProgrammingError; self->ProgrammingError = pysqlite_ProgrammingError;
self->NotSupportedError = pysqlite_NotSupportedError; self->NotSupportedError = pysqlite_NotSupportedError;
if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
return -1;
}
return 0; return 0;
} }

View file

@ -91,20 +91,11 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
factory = (PyObject*)pysqlite_ConnectionType; factory = (PyObject*)pysqlite_ConnectionType;
} }
if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
return NULL;
}
result = PyObject_Call(factory, args, kwargs); result = PyObject_Call(factory, args, kwargs);
if (result == NULL) { if (result == NULL) {
return NULL; return NULL;
} }
if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
Py_DECREF(result);
return NULL;
}
return result; return result;
} }