Skip to content

Commit 3b65464

Browse files
committed
Check for an exception after the statement is prepared
With STAT4, the query planner can call a collation when SQLite prepares a statement. Check PyErr_Occurred() after sqlite3_prepare_v2() and propagate the exception. Add a control to the authorizer test, so that it shows that SQLite calls the authorizer during the step.
1 parent 2392f68 commit 3b65464

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎Lib/test/test_sqlite3/test_hooks.py‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,18 @@ class CollationErrorTests(MemoryDatabaseMixin, unittest.TestCase):
165165
def setUp(self):
166166
super().setUp()
167167
self.events = []
168+
self.setUp_collation()
169+
self.con.execute("create table t(x, y)")
170+
self.con.executemany("insert into t values (?, ?)",
171+
[(1, "a"), (-2**63, "boom")])
172+
173+
def setUp_collation(self):
168174
def mycoll(x, y):
169175
if "boom" in (x, y):
170176
self.events.append("collation raised")
171177
raise KeyError("collation raised")
172178
return (x > y) - (x < y)
173179
self.con.create_collation("mycoll", mycoll)
174-
self.con.execute("create table t(x, y)")
175-
self.con.executemany("insert into t values (?, ?)",
176-
[(1, "a"), (-2**63, "boom")])
177180

178181
def assertCollationRaises(self):
179182
return self.assertRaisesRegex(KeyError, "collation raised")
@@ -284,7 +287,7 @@ def finalize(self):
284287
self.con.create_window_function("win", 1, Window)
285288
# For this query, SQLite evaluates the filter for all rows before it
286289
# calls the window function. The filter is false for the first row,
287-
# so the first call after the exception is value().
290+
# so the first call after the exception is value(), before step().
288291
with self.assertCollationRaises():
289292
self.con.execute("""
290293
select win(x) filter (where y = 'q' collate mycoll)
@@ -322,21 +325,48 @@ def finalize(self):
322325
self.assertEqual(self.events_after_exception(), [])
323326

324327
def test_authorizer_not_called(self):
325-
def authorizer(*args):
326-
self.events.append("authorizer")
328+
def authorizer(action, *args):
329+
self.events.append(action)
327330
return sqlite.SQLITE_OK
328-
# SQLite prepares a statement for pragma_table_info() during the
329-
# step, and it calls the authorizer for that statement.
331+
sql = """
332+
select t.x, p.name from t join pragma_table_info('t') p
333+
where t.y = 'q' collate mycoll
334+
"""
330335
self.con.executemany("insert into t values (?, ?)", [(3, "c")])
331336
self.con.set_authorizer(authorizer)
337+
# Control: if the comparison is true, SQLite prepares a statement for
338+
# pragma_table_info() during the step, and it calls the authorizer.
339+
self.con.create_collation("mycoll", lambda x, y: 0)
340+
self.con.execute(sql).fetchall()
341+
self.assertIn(sqlite.SQLITE_PRAGMA, self.events)
342+
self.events.clear()
343+
self.setUp_collation()
332344
with self.assertCollationRaises():
333-
self.con.execute("""
334-
select t.x, p.name from t join pragma_table_info('t') p
335-
where t.y = 'q' collate mycoll
336-
""")
345+
self.con.execute(sql)
337346
self.con.set_authorizer(None)
338347
self.assertEqual(self.events_after_exception(), [])
339348

349+
def test_prepare(self):
350+
# With STAT4, the query planner calls the collation when SQLite
351+
# prepares the statement.
352+
options = [o for (o,) in self.con.execute("pragma compile_options")]
353+
if "ENABLE_STAT4" not in options:
354+
self.skipTest("requires SQLite with SQLITE_ENABLE_STAT4")
355+
fail = False
356+
def statcoll(x, y):
357+
if fail:
358+
raise KeyError("collation raised")
359+
return (x > y) - (x < y)
360+
self.con.create_collation("statcoll", statcoll)
361+
self.con.execute("create table s(v text collate statcoll)")
362+
self.con.execute("create index s_v on s(v)")
363+
self.con.executemany("insert into s values (?)",
364+
[(str(i % 7),) for i in range(200)])
365+
self.con.execute("analyze")
366+
fail = True
367+
with self.assertCollationRaises():
368+
self.con.execute("select * from s where v = '3'")
369+
340370
def test_progress_handler_not_called(self):
341371
def progress():
342372
self.events.append("progress")

‎Modules/_sqlite/statement.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
6868
rc = sqlite3_prepare_v2(db, sql_cstr, (int)size + 1, &stmt, &tail);
6969
Py_END_ALLOW_THREADS
7070

71+
if (PyErr_Occurred()) {
72+
/* A callback left an exception set. For example, the query planner
73+
* can call a collation. */
74+
(void)sqlite3_finalize(stmt);
75+
return NULL;
76+
}
7177
if (rc != SQLITE_OK) {
7278
set_error_from_db(state, db);
7379
return NULL;

0 commit comments

Comments
 (0)