Skip to content

Commit c6a2320

Browse files
authored
bpo-37406: sqlite3 raises TypeError for wrong operation type (GH-14386)
The sqlite3 module now raises TypeError, rather than ValueError, if operation argument type is not str: execute(), executemany() and calling a connection.
1 parent ed076ed commit c6a2320

File tree

6 files changed

+11
-16
lines changed

6 files changed

+11
-16
lines changed

Lib/sqlite3/test/dbapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def CheckExecuteTooMuchSql3(self):
230230
""")
231231

232232
def CheckExecuteWrongSqlArg(self):
233-
with self.assertRaises(ValueError):
233+
with self.assertRaises(TypeError):
234234
self.cu.execute(42)
235235

236236
def CheckExecuteArgInt(self):
@@ -377,7 +377,7 @@ def mygen():
377377
self.cu.executemany("insert into test(income) values (?)", mygen())
378378

379379
def CheckExecuteManyWrongSqlArg(self):
380-
with self.assertRaises(ValueError):
380+
with self.assertRaises(TypeError):
381381
self.cu.executemany(42, [(3,)])
382382

383383
def CheckExecuteManySelect(self):

Lib/sqlite3/test/regression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def CheckConnectionCall(self):
261261
Call a connection with a non-string SQL request: check error handling
262262
of the statement constructor.
263263
"""
264-
self.assertRaises(sqlite.Warning, self.con, 1)
264+
self.assertRaises(TypeError, self.con, 1)
265265

266266
def CheckCollation(self):
267267
def collation_cb(a, b):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The sqlite3 module now raises TypeError, rather than ValueError, if
2+
operation argument type is not str: execute(), executemany() and calling a
3+
connection.

Modules/_sqlite/connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, Py
12231223
if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
12241224
return NULL;
12251225

1226-
if (!PyArg_ParseTuple(args, "O", &sql))
1226+
if (!PyArg_ParseTuple(args, "U", &sql))
12271227
return NULL;
12281228

12291229
_pysqlite_drop_unused_statement_references(self);

Modules/_sqlite/cursor.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
384384

385385
if (multiple) {
386386
/* executemany() */
387-
if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
388-
goto error;
389-
}
390-
391-
if (!PyUnicode_Check(operation)) {
392-
PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
387+
if (!PyArg_ParseTuple(args, "UO", &operation, &second_argument)) {
393388
goto error;
394389
}
395390

@@ -406,12 +401,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
406401
}
407402
} else {
408403
/* execute() */
409-
if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
410-
goto error;
411-
}
412-
413-
if (!PyUnicode_Check(operation)) {
414-
PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
404+
if (!PyArg_ParseTuple(args, "U|O", &operation, &second_argument)) {
415405
goto error;
416406
}
417407

Modules/_sqlite/statement.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
5959
self->st = NULL;
6060
self->in_use = 0;
6161

62+
assert(PyUnicode_Check(sql));
63+
6264
sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len);
6365
if (sql_cstr == NULL) {
6466
rc = PYSQLITE_SQL_WRONG_TYPE;

0 commit comments

Comments
 (0)