From 8a372a357ed10ae582946792846b3890ed271d9a Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 9 Mar 2022 09:33:29 +0100 Subject: [PATCH 1/5] Skip test_trace_too_much_expanded_sql on SQLite pre 3.14.0 --- Lib/test/test_sqlite3/test_hooks.py | 7 ++++--- Modules/_sqlite/connection.c | 14 +++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_sqlite3/test_hooks.py b/Lib/test/test_sqlite3/test_hooks.py index 38126b605469af..19367029fffd86 100644 --- a/Lib/test/test_sqlite3/test_hooks.py +++ b/Lib/test/test_sqlite3/test_hooks.py @@ -303,8 +303,6 @@ def trace(statement): con2.close() self.assertEqual(traced_statements, queries) - @unittest.skipIf(sqlite.sqlite_version_info < (3, 14, 0), - "Requires SQLite 3.14.0 or newer") def test_trace_expanded_sql(self): expected = [ "create table t(t)", @@ -335,7 +333,10 @@ def test_trace_too_much_expanded_sql(self): bad_param = "a" * (nextra + 1) unexpanded_query = template + "?" - with self.check_stmt_trace(cx, [unexpanded_query]): + expected = [unexpanded_query] + if sqlite.sqlite_version_info < (3, 14, 0): + expected = [] + with self.check_stmt_trace(cx, expected): cx.execute(unexpanded_query, (bad_param,)) expanded_query = f"{template}'{ok_param}'" diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 511e8a2077b41c..e269f89a4448ef 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -1094,6 +1094,9 @@ trace_callback(void *ctx, const char *sql) PyGILState_STATE gilstate = PyGILState_Ensure(); assert(ctx != NULL); + pysqlite_state *state = ((callback_context *)ctx)->state; + assert(state != NULL); + PyObject *py_statement = NULL; #ifdef HAVE_TRACE_V2 assert(stmt != NULL); @@ -1105,8 +1108,6 @@ trace_callback(void *ctx, const char *sql) goto exit; } - pysqlite_state *state = ((callback_context *)ctx)->state; - assert(state != NULL); PyErr_SetString(state->DataError, "Expanded SQL string exceeds the maximum string " "length"); @@ -1120,7 +1121,14 @@ trace_callback(void *ctx, const char *sql) sqlite3_free((void *)expanded_sql); } #else - py_statement = PyUnicode_FromString(sql); + if (sql == NULL) { + PyErr_SetString(state->DataError, + "Expanded SQL string exceeds the maximum string length"); + print_or_clear_traceback((callback_context *)ctx); + } + else { + py_statement = PyUnicode_FromString(sql); + } #endif if (py_statement) { PyObject *callable = ((callback_context *)ctx)->callable; From 260f2422b1f9b3b0413b0fc83032fbcd6e9e389f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 9 Mar 2022 12:33:29 +0100 Subject: [PATCH 2/5] Remove What's New entry --- Doc/whatsnew/3.11.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 262c1eb2c9daba..9b82de7f4a1a22 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -322,10 +322,6 @@ sqlite3 Instead we leave it to the SQLite library to handle these cases. (Contributed by Erlend E. Aasland in :issue:`44092`.) -* For SQLite 3.14.0 and newer, bound parameters are expanded in the statement - string passed to the trace callback. See :meth:`~sqlite3.Connection.set_trace_callback`. - (Contributed by Erlend E. Aasland in :issue:`45138`.) - sys --- From 86f284da9818c1988e8274db73a7e62b365fca56 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 9 Mar 2022 12:37:26 +0100 Subject: [PATCH 3/5] Amend NEWS entry --- .../next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst b/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst index 7b0b4402aeacea..3ea196dcd630d3 100644 --- a/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst +++ b/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst @@ -1,3 +1,2 @@ -For SQLite 3.14.0 and newer, bound parameters are expanded in the statement -string passed to the :mod:`sqlite3` trace callback. Patch by Erlend E. -Aasland. +Fix a regression in the :mod:`sqlite3` trace callback where bound parameters were +not expanded in the passed statement string. Patch by Erlend E. Aasland. From e11e0f9737c63863f470886e72581af90418b094 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 9 Mar 2022 12:40:30 +0100 Subject: [PATCH 4/5] Amend test comment --- Lib/test/test_sqlite3/test_hooks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_sqlite3/test_hooks.py b/Lib/test/test_sqlite3/test_hooks.py index 19367029fffd86..c36737cc828060 100644 --- a/Lib/test/test_sqlite3/test_hooks.py +++ b/Lib/test/test_sqlite3/test_hooks.py @@ -323,7 +323,8 @@ def test_trace_expanded_sql(self): ) def test_trace_too_much_expanded_sql(self): # If the expanded string is too large, we'll fall back to the - # unexpanded SQL statement. The resulting string length is limited by + # unexpanded SQL statement (for SQLite 3.14.0 and newer). + # The resulting string length is limited by the runtime limit # SQLITE_LIMIT_LENGTH. template = "select 'b' as \"a\" from sqlite_master where \"a\"=" category = sqlite.SQLITE_LIMIT_LENGTH From 0ab2575d282efe6ed2dbb5ed3e2051e0346e6b48 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 9 Mar 2022 12:48:22 +0100 Subject: [PATCH 5/5] Amend NEWS entry with regression info --- .../next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst b/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst index 3ea196dcd630d3..906ed4c4db43c8 100644 --- a/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst +++ b/Misc/NEWS.d/next/Library/2021-09-08-16-21-03.bpo-45138.yghUrK.rst @@ -1,2 +1,3 @@ -Fix a regression in the :mod:`sqlite3` trace callback where bound parameters were -not expanded in the passed statement string. Patch by Erlend E. Aasland. +Fix a regression in the :mod:`sqlite3` trace callback where bound parameters +were not expanded in the passed statement string. The regression was introduced +in Python 3.10 by :issue:`40318`. Patch by Erlend E. Aasland.