From 3470ce0430268f847a7e4d027e5f69956dee8742 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 Feb 2021 22:17:00 +0100 Subject: [PATCH 1/3] Prevent needless memory allocations of SQLite aggregate context --- Modules/_sqlite/connection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 63fcb0055de2c9..d46af1a5eede37 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -708,7 +708,7 @@ void _pysqlite_final_callback(sqlite3_context* context) threadstate = PyGILState_Ensure(); - aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); + aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0); if (!*aggregate_instance) { /* this branch is executed if there was an exception in the aggregate's * __init__ */ From b7dd8c226a54ad93993396e3718fa9368e0fdc21 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 Feb 2021 22:33:49 +0100 Subject: [PATCH 2/3] Handle aggregate queries with no hits --- Lib/sqlite3/test/userfunctions.py | 5 +++++ Modules/_sqlite/connection.c | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/sqlite3/test/userfunctions.py b/Lib/sqlite3/test/userfunctions.py index 2285abd4fd8a57..749ea049c834a8 100644 --- a/Lib/sqlite3/test/userfunctions.py +++ b/Lib/sqlite3/test/userfunctions.py @@ -429,6 +429,11 @@ def test_aggr_check_aggr_sum(self): 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/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index d46af1a5eede37..39b55fc60da422 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -709,7 +709,11 @@ void _pysqlite_final_callback(sqlite3_context* context) threadstate = PyGILState_Ensure(); aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0); - if (!*aggregate_instance) { + 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__ */ From 9722ea952a117ad25a39eb29c927f90bd34cde75 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 Feb 2021 23:31:03 +0100 Subject: [PATCH 3/3] Add NEWS --- .../next/Library/2021-02-18-23-30-52.bpo-43258.LeU-q8.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-02-18-23-30-52.bpo-43258.LeU-q8.rst 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 index 00000000000000..0529214718c445 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-02-18-23-30-52.bpo-43258.LeU-q8.rst @@ -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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy