]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43258: Don't allocate sqlite3 aggregate context for empty queries (GH-24569)
authorErlend Egeberg Aasland <erlend.aasland@innova.no>
Fri, 19 Feb 2021 11:20:32 +0000 (12:20 +0100)
committerGitHub <noreply@github.com>
Fri, 19 Feb 2021 11:20:32 +0000 (13:20 +0200)
Lib/sqlite3/test/userfunctions.py
Misc/NEWS.d/next/Library/2021-02-18-23-30-52.bpo-43258.LeU-q8.rst [new file with mode: 0644]
Modules/_sqlite/connection.c

index 2285abd4fd8a5791334df4daa19fd10782127bd7..749ea049c834a8f5f70936bc0857f990628e2352 100644 (file)
@@ -429,6 +429,11 @@ class AggregateTests(unittest.TestCase):
         val = cur.fetchone()[0]
         self.assertEqual(val, 60)
 
+    def test_aggr_no_match(self):
+        cur = self.con.execute("select mysum(i) from (select 1 as i) where i == 0")
+        val = cur.fetchone()[0]
+        self.assertIsNone(val)
+
 class AuthorizerTests(unittest.TestCase):
     @staticmethod
     def authorizer_cb(action, arg1, arg2, dbname, source):
diff --git a/Misc/NEWS.d/next/Library/2021-02-18-23-30-52.bpo-43258.LeU-q8.rst b/Misc/NEWS.d/next/Library/2021-02-18-23-30-52.bpo-43258.LeU-q8.rst
new file mode 100644 (file)
index 0000000..0529214
--- /dev/null
@@ -0,0 +1,2 @@
+Prevent needless allocation of :mod:`sqlite3` aggregate function context
+when no rows match an aggregate query. Patch by Erlend E. Aasland.
index 63fcb0055de2c9d026c2ec4039238f7480186dfd..39b55fc60da4228998d4d635d09df0e684987638 100644 (file)
@@ -708,8 +708,12 @@ void _pysqlite_final_callback(sqlite3_context* context)
 
     threadstate = PyGILState_Ensure();
 
-    aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
-    if (!*aggregate_instance) {
+    aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
+    if (aggregate_instance == NULL) {
+        /* No rows matched the query; the step handler was never called. */
+        goto error;
+    }
+    else if (!*aggregate_instance) {
         /* this branch is executed if there was an exception in the aggregate's
          * __init__ */