Skip to content

Commit f76a388

Browse files
bpo-41662: Fix bugs in binding parameters in sqlite3 (pythonGH-21998)
* When the parameters argument is a list, correctly handle the case of changing it during iteration. * When the parameters argument is a custom sequence, no longer override an exception raised in ``__len__()``. (cherry picked from commit 0b419b7) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent a9ba8ba commit f76a388

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

Lib/sqlite3/test/dbapi.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def CheckExecuteParamList(self):
276276
self.assertEqual(row[0], "foo")
277277

278278
def CheckExecuteParamSequence(self):
279-
class L(object):
279+
class L:
280280
def __len__(self):
281281
return 1
282282
def __getitem__(self, x):
@@ -288,6 +288,18 @@ def __getitem__(self, x):
288288
row = self.cu.fetchone()
289289
self.assertEqual(row[0], "foo")
290290

291+
def CheckExecuteParamSequenceBadLen(self):
292+
# Issue41662: Error in __len__() was overridden with ProgrammingError.
293+
class L:
294+
def __len__(self):
295+
1/0
296+
def __getitem__(slf, x):
297+
raise AssertionError
298+
299+
self.cu.execute("insert into test(name) values ('foo')")
300+
with self.assertRaises(ZeroDivisionError):
301+
self.cu.execute("select name from test where name=?", L())
302+
291303
def CheckExecuteDictMapping(self):
292304
self.cu.execute("insert into test(name) values ('foo')")
293305
self.cu.execute("select name from test where name=:name", {"name": "foo"})

Lib/sqlite3/test/regression.py

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ def CheckTypeMapUsage(self):
133133
con.execute("insert into foo(bar) values (5)")
134134
con.execute(SELECT)
135135

136+
def CheckBindMutatingList(self):
137+
# Issue41662: Crash when mutate a list of parameters during iteration.
138+
class X:
139+
def __conform__(self, protocol):
140+
parameters.clear()
141+
return "..."
142+
parameters = [X(), 0]
143+
con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES)
144+
con.execute("create table foo(bar X, baz integer)")
145+
# Should not crash
146+
with self.assertRaises(IndexError):
147+
con.execute("insert into foo(bar, baz) values (?, ?)", parameters)
148+
136149
def CheckErrorMsgDecodeError(self):
137150
# When porting the module to Python 3.0, the error message about
138151
# decoding errors disappeared. This verifies they're back again.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed crash when mutate list of parameters during iteration in :mod:`sqlite3`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
No longer override exceptions raised in ``__len__()`` of a sequence of
2+
parameters in :mod:`sqlite3` with :exc:`~sqlite3.ProgrammingError`.

Modules/_sqlite/statement.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
227227
num_params = PyList_GET_SIZE(parameters);
228228
} else {
229229
num_params = PySequence_Size(parameters);
230+
if (num_params == -1) {
231+
return;
232+
}
230233
}
231234
if (num_params != num_params_needed) {
232235
PyErr_Format(pysqlite_ProgrammingError,
@@ -238,9 +241,9 @@ void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* para
238241
for (i = 0; i < num_params; i++) {
239242
if (PyTuple_CheckExact(parameters)) {
240243
current_param = PyTuple_GET_ITEM(parameters, i);
241-
Py_XINCREF(current_param);
244+
Py_INCREF(current_param);
242245
} else if (PyList_CheckExact(parameters)) {
243-
current_param = PyList_GET_ITEM(parameters, i);
246+
current_param = PyList_GetItem(parameters, i);
244247
Py_XINCREF(current_param);
245248
} else {
246249
current_param = PySequence_GetItem(parameters, i);

0 commit comments

Comments
 (0)