@@ -150,79 +150,149 @@ def test_deregister_collation(self):
150150
151151class CollationErrorTests (MemoryDatabaseMixin , unittest .TestCase ):
152152 # gh-161837: A collation callback cannot report an error to SQLite, so
153- # the exception stays set while SQLite continues the step.
153+ # the exception stays set while SQLite continues the step. The module
154+ # must propagate that exception, and it must not call other Python code
155+ # while the exception is set.
154156
155- # When the collation raises, the comparison is true, so abs() of the
156- # smallest 64-bit integer causes "integer overflow" .
157+ # When the collation raises, it returns 0 ("equal"). Thus the comparison
158+ # is true, and abs() of the smallest 64-bit integer makes the step fail .
157159 step_fails = """
158160 select case when y = 'q' collate mycoll then abs(x) else 0 end
159161 from t order by rowid
160162 """
161163 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.
164164
165165 def setUp (self ):
166166 super ().setUp ()
167+ self .events = []
167168 def mycoll (x , y ):
168169 if x == "boom" :
170+ self .events .append ("collation raised" )
169171 raise KeyError ("collation raised" )
170172 return (x > y ) - (x < y )
171173 self .con .create_collation ("mycoll" , mycoll )
172174 self .con .execute ("create table t(x, y)" )
173175 self .con .executemany ("insert into t values (?, ?)" ,
174176 [(1 , "a" ), (- 2 ** 63 , "boom" )])
175177
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" )
178+ def assertCollationRaises (self ):
179+ return self .assertRaisesRegex (KeyError , "collation raised" )
180+
181+ def test_execute (self ):
182+ for sql in self .step_fails , self .step_succeeds :
183+ with self .subTest (sql = sql ), self .assertCollationRaises ():
184+ self .con .execute (sql + " desc" )
185+
186+ def test_fetchone (self ):
187+ # The cursor reads one row ahead, so the first fetch steps to the
188+ # row that makes the collation raise.
189+ for sql in self .step_fails , self .step_succeeds :
190+ with self .subTest (sql = sql ):
191+ cur = self .con .execute (sql )
192+ with self .assertCollationRaises ():
193+ cur .fetchone ()
194+ self .assertIsNone (cur .fetchone ())
195+ self .assertEqual (cur .execute ("select 1" ).fetchone (), (1 ,))
196+
197+ def test_iteration (self ):
198+ for sql in self .step_fails , self .step_succeeds :
199+ with self .subTest (sql = sql ):
200+ rows = []
201+ with self .assertCollationRaises ():
202+ for row in self .con .execute (sql ):
203+ rows .append (row )
204+ self .assertEqual (rows , [])
205+
206+ def test_executemany (self ):
207+ def params ():
208+ yield ("boom" ,)
209+ self .fail ("executemany() continued after the exception" )
210+ with self .assertCollationRaises ():
211+ self .con .executemany (
212+ "insert into t(y) values (? = 'q' collate mycoll)" , params ())
213+
214+ def test_executescript (self ):
215+ for sql in self .step_fails , self .step_succeeds :
216+ with self .subTest (sql = sql ):
217+ with self .assertCollationRaises ():
218+ self .con .executescript (sql + "; create table u(x)" )
219+ # The script stops after the statement with the exception.
220+ with self .assertRaisesRegex (sqlite .OperationalError ,
221+ "no such table" ):
222+ self .con .execute ("select * from u" )
223+
224+ def test_converter_not_used (self ):
225+ with memory_database (detect_types = sqlite .PARSE_COLNAMES ) as con :
226+ con .create_collation ("mycoll" , lambda x , y : 1 / 0 )
227+ with self .assertRaises (ZeroDivisionError ):
228+ con .execute ("select 'a' = 'b' collate mycoll as \" x [conv]\" " )
229+
230+ def test_function_not_called (self ):
231+ calls = []
232+ self .con .create_function ("f" , 1 , calls .append )
233+ with self .assertCollationRaises ():
234+ self .con .execute ("select f(y = 'q' collate mycoll) from t "
235+ "order by rowid desc" )
236+ self .assertEqual (calls , [])
237+
238+ def test_aggregate_not_called (self ):
239+ calls = []
240+ class Agg :
241+ def step (self , value ):
242+ calls .append (value )
243+ def finalize (self ):
244+ return len (calls )
245+ self .con .create_aggregate ("agg" , 1 , Agg )
246+ with self .assertCollationRaises ():
247+ self .con .execute ("select agg(y = 'q' collate mycoll) from t "
248+ "where rowid = 2" )
249+ self .assertEqual (calls , [])
250+
251+ def test_window_function_not_called (self ):
252+ calls = []
253+ class Window :
254+ def step (self , value ):
255+ calls .append ("step" )
256+ def inverse (self , value ):
257+ calls .append ("inverse" )
258+ def value (self ):
259+ calls .append ("value" )
260+ return 0
261+ def finalize (self ):
262+ return 0
263+ 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().
267+ with self .assertCollationRaises ():
268+ self .con .execute ("""
269+ select win(x) filter (where y = 'q' collate mycoll)
270+ over (order by rowid) from t
271+ """ ).fetchall ()
272+ self .assertEqual (calls , [])
273+
274+ def test_progress_handler_not_called (self ):
275+ def progress ():
276+ self .events .append ("progress" )
277+ return 0
278+ self .con .set_progress_handler (progress , 1 )
279+ with self .assertCollationRaises ():
280+ self .con .execute (self .step_succeeds + " desc" )
281+ self .con .set_progress_handler (None , 1 )
282+ self .assertEqual (self .events [- 1 ], "collation raised" )
283+ self .assertIn ("progress" , self .events )
284+
285+ def test_trace_callback_not_called (self ):
286+ self .con .execute ("create table u(x)" )
287+ self .con .execute ("create trigger tr after insert on u "
288+ "begin select 1; end" )
289+ traced = []
290+ self .con .set_trace_callback (traced .append )
291+ with self .assertCollationRaises ():
292+ self .con .execute ("insert into u select y = 'q' collate mycoll "
293+ "from t order by rowid desc" )
294+ self .con .set_trace_callback (None )
295+ self .assertNotIn ("-- TRIGGER tr" , traced )
226296
227297
228298class AuthorizerTests (MemoryDatabaseMixin , unittest .TestCase ):
0 commit comments