Skip to content

Commit 6733679

Browse files
committed
Fix the other paths where a collation exception stays set
A collation exception also stays set when the step does not fail. Then pysqlite_cursor_iternext() and executescript() return a result with an exception set. Now the collation exception propagates on these paths. executescript() stops after the statement that caused the exception. It also reports the pending exception when a step fails. Report the pending exception with PyErr_FormatUnraisable() instead of PyErr_Print(). The documentation of enable_callback_tracebacks() says this, and the other callbacks do this. Move the tests into the new class CollationErrorTests and use with_tracebacks().
1 parent 9f015dd commit 6733679

3 files changed

Lines changed: 105 additions & 57 deletions

File tree

‎Lib/test/test_sqlite3/test_hooks.py‎

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import sqlite3 as sqlite
2525
import unittest
2626

27-
from test.support import captured_stderr, force_not_colorized, import_helper
27+
from test.support import import_helper
2828
from test.support.os_helper import TESTFN, unlink
2929

3030
from .util import memory_database, cx_limit, with_tracebacks
@@ -106,50 +106,6 @@ def mycoll(x, y):
106106
self.assertEqual(result, [('c',), ('b',), ('a',)],
107107
msg="the expected order was not returned")
108108

109-
@force_not_colorized
110-
def test_collation_error_then_step_failure_while_fetching(self):
111-
# gh-161837: A collation can set an exception. Then a later error can
112-
# make the same sqlite3_step() fail. The fetch must raise
113-
# OperationalError for that failure, not SystemError.
114-
def mycoll(x, y):
115-
if x == "boom":
116-
raise KeyError("collation raised")
117-
return (x > y) - (x < y)
118-
self.con.create_collation("mycoll", mycoll)
119-
self.con.execute("create table t(x, y)")
120-
# abs() of the smallest 64-bit integer causes "integer overflow".
121-
self.con.executemany("insert into t values (?, ?)",
122-
[(1, "a"), (-2**63, "boom")])
123-
sql = """
124-
select case when y = 'q' collate mycoll then abs(x) else 0 end
125-
from t order by rowid
126-
"""
127-
128-
def fetch_with_fetchone():
129-
cur = self.con.execute(sql)
130-
self.assertEqual(cur.fetchone(), (0,))
131-
cur.fetchone()
132-
133-
def fetch_by_iteration():
134-
list(self.con.execute(sql))
135-
136-
for fetch in fetch_with_fetchone, fetch_by_iteration:
137-
for tracebacks in False, True:
138-
with self.subTest(fetch=fetch.__name__, tracebacks=tracebacks):
139-
sqlite.enable_callback_tracebacks(tracebacks)
140-
try:
141-
with captured_stderr() as err:
142-
with self.assertRaisesRegex(sqlite.OperationalError,
143-
"integer overflow"):
144-
fetch()
145-
finally:
146-
sqlite.enable_callback_tracebacks(False)
147-
if tracebacks:
148-
self.assertIn("KeyError: 'collation raised'",
149-
err.getvalue())
150-
else:
151-
self.assertEqual(err.getvalue(), "")
152-
153109
def test_collation_register_twice(self):
154110
"""
155111
Register two different collation functions under the same name.
@@ -192,6 +148,83 @@ def test_deregister_collation(self):
192148
self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
193149

194150

151+
class CollationErrorTests(MemoryDatabaseMixin, unittest.TestCase):
152+
# gh-161837: A collation callback cannot report an error to SQLite, so
153+
# the exception stays set while SQLite continues the step.
154+
155+
# When the collation raises, the comparison is true, so abs() of the
156+
# smallest 64-bit integer causes "integer overflow".
157+
step_fails = """
158+
select case when y = 'q' collate mycoll then abs(x) else 0 end
159+
from t order by rowid
160+
"""
161+
step_succeeds = "select x, y = 'q' collate mycoll from t order by rowid"
162+
# The cursor reads one row ahead, so the first fetch steps to the
163+
# second row, where the collation raises.
164+
165+
def setUp(self):
166+
super().setUp()
167+
def mycoll(x, y):
168+
if x == "boom":
169+
raise KeyError("collation raised")
170+
return (x > y) - (x < y)
171+
self.con.create_collation("mycoll", mycoll)
172+
self.con.execute("create table t(x, y)")
173+
self.con.executemany("insert into t values (?, ?)",
174+
[(1, "a"), (-2**63, "boom")])
175+
176+
@with_tracebacks(KeyError, regex="collation raised",
177+
msg_regex="sqlite3 callback")
178+
def test_step_failure_in_execute(self):
179+
with self.assertRaisesRegex(sqlite.OperationalError,
180+
"integer overflow"):
181+
self.con.execute(self.step_fails + " desc")
182+
183+
@with_tracebacks(KeyError, regex="collation raised",
184+
msg_regex="sqlite3 callback")
185+
def test_step_failure_in_fetchone(self):
186+
cur = self.con.execute(self.step_fails)
187+
with self.assertRaisesRegex(sqlite.OperationalError,
188+
"integer overflow"):
189+
cur.fetchone()
190+
self.assertIsNone(cur.fetchone())
191+
192+
@with_tracebacks(KeyError, regex="collation raised",
193+
msg_regex="sqlite3 callback")
194+
def test_step_failure_in_iteration(self):
195+
with self.assertRaisesRegex(sqlite.OperationalError,
196+
"integer overflow"):
197+
list(self.con.execute(self.step_fails))
198+
199+
@with_tracebacks(KeyError, regex="collation raised",
200+
msg_regex="sqlite3 callback")
201+
def test_step_failure_in_executescript(self):
202+
with self.assertRaisesRegex(sqlite.OperationalError,
203+
"integer overflow"):
204+
self.con.executescript(self.step_fails)
205+
206+
def test_error_in_fetchone(self):
207+
cur = self.con.execute(self.step_succeeds)
208+
with self.assertRaisesRegex(KeyError, "collation raised"):
209+
cur.fetchone()
210+
self.assertIsNone(cur.fetchone())
211+
self.assertEqual(cur.execute("select 1").fetchone(), (1,))
212+
213+
def test_error_in_iteration(self):
214+
rows = []
215+
with self.assertRaisesRegex(KeyError, "collation raised"):
216+
for row in self.con.execute(self.step_succeeds):
217+
rows.append(row)
218+
self.assertEqual(rows, [])
219+
220+
def test_error_in_executescript(self):
221+
with self.assertRaisesRegex(KeyError, "collation raised"):
222+
self.con.executescript(self.step_succeeds + "; create table u(x)")
223+
# The script stops after the statement that caused the error.
224+
with self.assertRaisesRegex(sqlite.OperationalError, "no such table"):
225+
self.con.execute("select * from u")
226+
227+
195228
class AuthorizerTests(MemoryDatabaseMixin, unittest.TestCase):
196229

197230
def assert_not_authorized(self, func, /, *args, **kwargs):
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
Fix a :exc:`SystemError` in :mod:`sqlite3`. The error occurs when a
2-
collation callback raises an exception and the same step then fails during a
3-
fetch. In debug builds, an assertion fails. Now the module raises
4-
:exc:`~sqlite3.OperationalError` for the step failure, as
5-
:meth:`~sqlite3.Cursor.execute` does.
1+
Fix a :exc:`SystemError` in :mod:`sqlite3` when a collation callback raises
2+
an exception. This error occurs when you fetch rows from a cursor and when you
3+
call :meth:`~sqlite3.Cursor.executescript`. Now the exception from the
4+
collation propagates. If the step also fails, the module raises
5+
:exc:`~sqlite3.OperationalError`, as :meth:`~sqlite3.Cursor.execute` does.
6+
When callback tracebacks are enabled, the module now reports the collation
7+
exception with :func:`sys.unraisablehook`, as the documentation says.

‎Modules/_sqlite/cursor.c‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,15 +553,16 @@ stmt_step(sqlite3_stmt *statement)
553553
return rc;
554554
}
555555

556-
/* A user-defined callback (for example, a collation) can set an exception
557-
* during a failed sqlite3_step(). Report that exception, then clear it.
558-
* The caller can then raise the DB-API exception for the failure. */
556+
/* A collation callback cannot report an error to SQLite, so it keeps the
557+
* Python exception set while SQLite continues the step. If the step then
558+
* fails, call this function to report and clear that exception. The caller
559+
* can then raise the DB-API exception for the failure. */
559560
static void
560-
clear_callback_error(pysqlite_state *state)
561+
report_pending_callback_error(pysqlite_state *state)
561562
{
562563
if (PyErr_Occurred()) {
563564
if (state->enable_callback_tracebacks) {
564-
PyErr_Print();
565+
PyErr_FormatUnraisable("Exception ignored on sqlite3 callback");
565566
}
566567
else {
567568
PyErr_Clear();
@@ -927,7 +928,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
927928

928929
rc = stmt_step(self->statement->st);
929930
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
930-
clear_callback_error(state);
931+
report_pending_callback_error(state);
931932
set_error_from_db(state, self->connection->db);
932933
goto error;
933934
}
@@ -1137,6 +1138,10 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
11371138
if (rc != SQLITE_OK) {
11381139
goto error;
11391140
}
1141+
if (PyErr_Occurred()) {
1142+
/* A collation callback raised, but the step did not fail. */
1143+
return NULL;
1144+
}
11401145

11411146
if (*tail == (char)0) {
11421147
break;
@@ -1148,6 +1153,7 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
11481153
return Py_NewRef((PyObject *)self);
11491154

11501155
error:
1156+
report_pending_callback_error(self->connection->state);
11511157
set_error_from_db(self->connection->state, db);
11521158
return NULL;
11531159
}
@@ -1175,6 +1181,13 @@ pysqlite_cursor_iternext(PyObject *op)
11751181
return NULL;
11761182
}
11771183
int rc = stmt_step(stmt);
1184+
if ((rc == SQLITE_ROW || rc == SQLITE_DONE) && PyErr_Occurred()) {
1185+
/* A collation callback raised, but the step did not fail. */
1186+
(void)stmt_reset(self->statement);
1187+
Py_CLEAR(self->statement);
1188+
Py_DECREF(row);
1189+
return NULL;
1190+
}
11781191
if (rc == SQLITE_DONE) {
11791192
if (self->statement->is_dml) {
11801193
self->rowcount = (long)sqlite3_changes(self->connection->db);
@@ -1186,7 +1199,7 @@ pysqlite_cursor_iternext(PyObject *op)
11861199
}
11871200
}
11881201
else if (rc != SQLITE_ROW) {
1189-
clear_callback_error(self->connection->state);
1202+
report_pending_callback_error(self->connection->state);
11901203
rc = set_error_from_db(self->connection->state, self->connection->db);
11911204
int reset_rc = stmt_reset(self->statement);
11921205
Py_CLEAR(self->statement);

0 commit comments

Comments
 (0)