Skip to content

Commit 2392f68

Browse files
committed
Guard all callbacks and keep the exception in finalize
The authorizer can run during a step, because table-valued pragma functions prepare statements. inverse() can run after the collation raised, for example when SQLite adds a row to an index that uses the collation. Do not call Python code from these callbacks while an exception is set. final_callback() calls finalize() with the earlier exception set aside. If finalize() raised, the earlier exception was lost. Now the module sets it again, so that it propagates. Make the comments generic, because a callback other than a collation can also leave an exception set. Make the tests check the order of the calls, and add tests for the authorizer, inverse() and finalize().
1 parent 59b1a3f commit 2392f68

4 files changed

Lines changed: 123 additions & 47 deletions

File tree

‎Lib/test/test_sqlite3/test_hooks.py‎

Lines changed: 96 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def setUp(self):
166166
super().setUp()
167167
self.events = []
168168
def mycoll(x, y):
169-
if x == "boom":
169+
if "boom" in (x, y):
170170
self.events.append("collation raised")
171171
raise KeyError("collation raised")
172172
return (x > y) - (x < y)
@@ -178,6 +178,9 @@ def mycoll(x, y):
178178
def assertCollationRaises(self):
179179
return self.assertRaisesRegex(KeyError, "collation raised")
180180

181+
def events_after_exception(self):
182+
return self.events[self.events.index("collation raised") + 1:]
183+
181184
def test_execute(self):
182185
for sql in self.step_fails, self.step_succeeds:
183186
with self.subTest(sql=sql), self.assertCollationRaises():
@@ -221,55 +224,118 @@ def test_executescript(self):
221224
"no such table"):
222225
self.con.execute("select * from u")
223226

224-
def test_converter_not_used(self):
227+
def test_converter_not_called(self):
228+
calls = []
229+
sqlite.register_converter("COLLTEST", calls.append)
230+
self.addCleanup(sqlite.converters.pop, "COLLTEST", None)
225231
with memory_database(detect_types=sqlite.PARSE_COLNAMES) as con:
226232
con.create_collation("mycoll", lambda x, y: 1 / 0)
227233
with self.assertRaises(ZeroDivisionError):
228-
con.execute("select 'a' = 'b' collate mycoll as \"x [conv]\"")
234+
con.execute("""
235+
select 'a' = 'b' collate mycoll as "x [colltest]"
236+
""")
237+
self.assertEqual(calls, [])
229238

230239
def test_function_not_called(self):
231-
calls = []
232-
self.con.create_function("f", 1, calls.append)
240+
def f(value):
241+
self.events.append("function")
242+
self.con.create_function("f", 1, f)
233243
with self.assertCollationRaises():
234244
self.con.execute("select f(y = 'q' collate mycoll) from t "
235245
"order by rowid desc")
236-
self.assertEqual(calls, [])
246+
self.assertEqual(self.events, ["collation raised"])
237247

238-
def test_aggregate_not_called(self):
239-
calls = []
248+
def test_aggregate_step_not_called(self):
249+
events = self.events
240250
class Agg:
241251
def step(self, value):
242-
calls.append(value)
252+
events.append("step")
243253
def finalize(self):
244-
return len(calls)
254+
events.append("finalize")
245255
self.con.create_aggregate("agg", 1, Agg)
246256
with self.assertCollationRaises():
247-
self.con.execute("select agg(y = 'q' collate mycoll) from t "
248-
"where rowid = 2")
249-
self.assertEqual(calls, [])
257+
self.con.execute("select agg(y = 'q' collate mycoll) from t")
258+
# SQLite calls finalize() to release the aggregate.
259+
self.assertEqual(self.events_after_exception(), ["finalize"])
250260

251-
def test_window_function_not_called(self):
252-
calls = []
261+
@with_tracebacks(ZeroDivisionError)
262+
def test_aggregate_finalize_error(self):
263+
class Agg:
264+
def step(self, value):
265+
pass
266+
def finalize(self):
267+
1 / 0
268+
self.con.create_aggregate("agg", 1, Agg)
269+
with self.assertCollationRaises():
270+
self.con.execute("select agg(y = 'q' collate mycoll) from t")
271+
272+
def test_window_function_value_not_called(self):
273+
events = self.events
253274
class Window:
254275
def step(self, value):
255-
calls.append("step")
276+
events.append("step")
256277
def inverse(self, value):
257-
calls.append("inverse")
278+
events.append("inverse")
258279
def value(self):
259-
calls.append("value")
280+
events.append("value")
260281
return 0
261282
def finalize(self):
262283
return 0
263284
self.con.create_window_function("win", 1, Window)
264-
# SQLite evaluates the filter for all rows before it calls the
265-
# window function. The filter is false for the first row, so the
266-
# first call after the exception is value().
285+
# For this query, SQLite evaluates the filter for all rows before it
286+
# calls the window function. The filter is false for the first row,
287+
# so the first call after the exception is value().
267288
with self.assertCollationRaises():
268289
self.con.execute("""
269290
select win(x) filter (where y = 'q' collate mycoll)
270291
over (order by rowid) from t
271-
""").fetchall()
272-
self.assertEqual(calls, [])
292+
""")
293+
self.assertEqual(self.events_after_exception(), [])
294+
295+
def test_window_function_inverse_not_called(self):
296+
events = self.events
297+
class Window:
298+
def step(self, value):
299+
events.append("step")
300+
def inverse(self, value):
301+
events.append("inverse")
302+
def value(self):
303+
events.append("value")
304+
return 0
305+
def finalize(self):
306+
return 0
307+
self.con.create_window_function("win", 1, Window)
308+
self.con.executemany("insert into t values (?, ?)",
309+
[(3, "c"), (4, "d")])
310+
self.con.execute("create table u(w, y)")
311+
# SQLite uses the collation when it adds a row to the index. For the
312+
# next row, the first call is inverse().
313+
self.con.execute("create index u_y on u(y collate mycoll)")
314+
with self.assertCollationRaises():
315+
self.con.execute("""
316+
insert into u
317+
select win(x) over (order by rowid
318+
rows between 1 preceding and current row),
319+
y
320+
from t order by rowid
321+
""")
322+
self.assertEqual(self.events_after_exception(), [])
323+
324+
def test_authorizer_not_called(self):
325+
def authorizer(*args):
326+
self.events.append("authorizer")
327+
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.
330+
self.con.executemany("insert into t values (?, ?)", [(3, "c")])
331+
self.con.set_authorizer(authorizer)
332+
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+
""")
337+
self.con.set_authorizer(None)
338+
self.assertEqual(self.events_after_exception(), [])
273339

274340
def test_progress_handler_not_called(self):
275341
def progress():
@@ -279,20 +345,22 @@ def progress():
279345
with self.assertCollationRaises():
280346
self.con.execute(self.step_succeeds + " desc")
281347
self.con.set_progress_handler(None, 1)
282-
self.assertEqual(self.events[-1], "collation raised")
283348
self.assertIn("progress", self.events)
349+
self.assertEqual(self.events_after_exception(), [])
284350

285351
def test_trace_callback_not_called(self):
286352
self.con.execute("create table u(x)")
287-
self.con.execute("create trigger tr after insert on u "
353+
# SQLite calls the trace callback again for each trigger program.
354+
self.con.execute("create trigger u_tr after insert on u "
288355
"begin select 1; end")
289356
traced = []
357+
sql = ("insert into u select y = 'q' collate mycoll from t "
358+
"order by rowid desc")
290359
self.con.set_trace_callback(traced.append)
291360
with self.assertCollationRaises():
292-
self.con.execute("insert into u select y = 'q' collate mycoll "
293-
"from t order by rowid desc")
361+
self.con.execute(sql)
294362
self.con.set_trace_callback(None)
295-
self.assertNotIn("-- TRIGGER tr", traced)
363+
self.assertEqual(traced, [sql])
296364

297365

298366
class AuthorizerTests(MemoryDatabaseMixin, unittest.TestCase):
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Fix a :exc:`SystemError` and crashes in :mod:`sqlite3` when a collation
2-
callback raises an exception. Now the exception from the collation always
2+
callback raises an exception. Now the exception from the collation
33
propagates. Before, :meth:`~sqlite3.Cursor.execute` raised
44
:exc:`~sqlite3.OperationalError` instead if the same step also failed. While
5-
the exception is set, the module no longer calls other callbacks, such as
6-
user-defined functions.
5+
the exception is set, the module no longer calls user-defined functions, the
6+
authorizer, the progress handler or the trace callback. It calls only the
7+
``finalize()`` method of an aggregate, to release the aggregate.

‎Modules/_sqlite/connection.c‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,12 @@ set_sqlite_error(sqlite3_context *context, const char *msg)
960960
print_or_clear_traceback(ctx);
961961
}
962962

963-
/* If a collation callback raises, the exception stays set until
964-
* sqlite3_step() returns (see collation_callback()). Other callbacks in the
965-
* same step must not call Python code while the exception is set. Return 1
966-
* if an exception is set. If context is not NULL, also make the SQL function
967-
* fail, so that SQLite stops the step. */
963+
/* A callback can leave an exception set until sqlite3_step() returns. For
964+
* example, a collation callback cannot report an error to SQLite (see
965+
* collation_callback()). Other callbacks in the same step must not call
966+
* Python code while the exception is set. Return 1 if an exception is set.
967+
* If context is not NULL, also make the SQL function fail, so that SQLite
968+
* stops the step. */
968969
static int
969970
exception_is_pending(sqlite3_context *context)
970971
{
@@ -1110,7 +1111,6 @@ final_callback(sqlite3_context *context)
11101111
}
11111112
if (!ok) {
11121113
int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError);
1113-
_PyErr_ChainExceptions1(exc);
11141114

11151115
/* Note: contrary to the step, value, and inverse callbacks, SQLite
11161116
* does _not_, as of SQLite 3.38.0, propagate errors to sqlite3_step()
@@ -1120,9 +1120,8 @@ final_callback(sqlite3_context *context)
11201120
? "user-defined aggregate's 'finalize' method not defined"
11211121
: "user-defined aggregate's 'finalize' method raised error");
11221122
}
1123-
else {
1124-
PyErr_SetRaisedException(exc);
1125-
}
1123+
// Set the exception of the earlier callback again, so that it propagates.
1124+
PyErr_SetRaisedException(exc);
11261125

11271126
error:
11281127
PyGILState_Release(threadstate);
@@ -1283,6 +1282,10 @@ static void
12831282
inverse_callback(sqlite3_context *context, int argc, sqlite3_value **params)
12841283
{
12851284
PyGILState_STATE gilstate = PyGILState_Ensure();
1285+
if (exception_is_pending(context)) {
1286+
PyGILState_Release(gilstate);
1287+
return;
1288+
}
12861289

12871290
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
12881291
incref_callback_context(ctx);
@@ -1484,6 +1487,10 @@ authorizer_callback(void *ctx_vp, int action, const char *arg1,
14841487
const char *access_attempt_source)
14851488
{
14861489
PyGILState_STATE gilstate = PyGILState_Ensure();
1490+
if (exception_is_pending(NULL)) {
1491+
PyGILState_Release(gilstate);
1492+
return SQLITE_DENY;
1493+
}
14871494

14881495
PyObject *ret;
14891496
int rc = SQLITE_DENY;

‎Modules/_sqlite/cursor.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,10 @@ get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
541541
return PyObject_Vectorcall(cache, args + 1, nargsf, NULL);
542542
}
543543

544-
/* A collation callback cannot report an error to SQLite. If it raises, the
545-
* exception stays set until sqlite3_step() returns, and SQLite continues
546-
* the step with the wrong result of the comparison. Thus, after a step, check
547-
* PyErr_Occurred() first, and propagate that exception. */
544+
/* A callback can leave an exception set until sqlite3_step() returns. For
545+
* example, a collation callback cannot report an error to SQLite, so SQLite
546+
* continues the step with a wrong result of the comparison. Thus, after a
547+
* step, check PyErr_Occurred() first, and propagate that exception. */
548548
static inline int
549549
stmt_step(sqlite3_stmt *statement)
550550
{
@@ -915,7 +915,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
915915

916916
rc = stmt_step(self->statement->st);
917917
if (PyErr_Occurred()) {
918-
/* A collation callback raised. */
918+
/* A callback left an exception set. */
919919
goto error;
920920
}
921921
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
@@ -1126,7 +1126,7 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
11261126
Py_END_ALLOW_THREADS
11271127

11281128
if (PyErr_Occurred()) {
1129-
/* A collation callback raised. */
1129+
/* A callback left an exception set. */
11301130
return NULL;
11311131
}
11321132
if (rc != SQLITE_OK) {
@@ -1171,7 +1171,7 @@ pysqlite_cursor_iternext(PyObject *op)
11711171
}
11721172
int rc = stmt_step(stmt);
11731173
if (PyErr_Occurred()) {
1174-
/* A collation callback raised. */
1174+
/* A callback left an exception set. */
11751175
(void)stmt_reset(self->statement);
11761176
Py_CLEAR(self->statement);
11771177
Py_DECREF(row);

0 commit comments

Comments
 (0)