]> granicus.if.org Git - python/commitdiff
bpo-37406: sqlite3 raises TypeError for wrong operation type (GH-14386)
authorVictor Stinner <vstinner@redhat.com>
Wed, 26 Jun 2019 01:16:24 +0000 (03:16 +0200)
committerGitHub <noreply@github.com>
Wed, 26 Jun 2019 01:16:24 +0000 (03:16 +0200)
The sqlite3 module now raises TypeError, rather than ValueError, if
operation argument type is not str: execute(), executemany() and
calling a connection.

Lib/sqlite3/test/dbapi.py
Lib/sqlite3/test/regression.py
Misc/NEWS.d/next/Library/2019-06-26-03-00-06.bpo-37406.uovkpq.rst [new file with mode: 0644]
Modules/_sqlite/connection.c
Modules/_sqlite/cursor.c
Modules/_sqlite/statement.c

index 7c259d2af418febcf8293723990cec38d077cca0..be11337154bdd23ec839e7fb9dd7b448a7301c3c 100644 (file)
@@ -230,7 +230,7 @@ class CursorTests(unittest.TestCase):
             """)
 
     def CheckExecuteWrongSqlArg(self):
-        with self.assertRaises(ValueError):
+        with self.assertRaises(TypeError):
             self.cu.execute(42)
 
     def CheckExecuteArgInt(self):
@@ -377,7 +377,7 @@ class CursorTests(unittest.TestCase):
         self.cu.executemany("insert into test(income) values (?)", mygen())
 
     def CheckExecuteManyWrongSqlArg(self):
-        with self.assertRaises(ValueError):
+        with self.assertRaises(TypeError):
             self.cu.executemany(42, [(3,)])
 
     def CheckExecuteManySelect(self):
index 865bd88f74f10a4947f7338b328d8fb372a316d0..ee326168bc125a6c4761be7fe91b6d67db782e98 100644 (file)
@@ -261,7 +261,7 @@ class RegressionTests(unittest.TestCase):
         Call a connection with a non-string SQL request: check error handling
         of the statement constructor.
         """
-        self.assertRaises(sqlite.Warning, self.con, 1)
+        self.assertRaises(TypeError, self.con, 1)
 
     def CheckCollation(self):
         def collation_cb(a, b):
diff --git a/Misc/NEWS.d/next/Library/2019-06-26-03-00-06.bpo-37406.uovkpq.rst b/Misc/NEWS.d/next/Library/2019-06-26-03-00-06.bpo-37406.uovkpq.rst
new file mode 100644 (file)
index 0000000..9a195ff
--- /dev/null
@@ -0,0 +1,3 @@
+The sqlite3 module now raises TypeError, rather than ValueError, if
+operation argument type is not str: execute(), executemany() and calling a
+connection.
index 0d6462ef7dc290543104eabf424d27753254ed57..5ceeaf98ee80d729bb06a02a3d38c5ce02c88354 100644 (file)
@@ -1223,7 +1223,7 @@ PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, Py
     if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
         return NULL;
 
-    if (!PyArg_ParseTuple(args, "O", &sql))
+    if (!PyArg_ParseTuple(args, "U", &sql))
         return NULL;
 
     _pysqlite_drop_unused_statement_references(self);
index 01b9dc44cb5da50376ef668eafd8e8fc4e5044b5..38d94b2fb590febffb074f878b2067204bb3592e 100644 (file)
@@ -384,12 +384,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
 
     if (multiple) {
         /* executemany() */
-        if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
-            goto error;
-        }
-
-        if (!PyUnicode_Check(operation)) {
-            PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
+        if (!PyArg_ParseTuple(args, "UO", &operation, &second_argument)) {
             goto error;
         }
 
@@ -406,12 +401,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
         }
     } else {
         /* execute() */
-        if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
-            goto error;
-        }
-
-        if (!PyUnicode_Check(operation)) {
-            PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
+        if (!PyArg_ParseTuple(args, "U|O", &operation, &second_argument)) {
             goto error;
         }
 
index 491294b0209cca99b8cd31ae492b6cad85c597c3..9de8f9b67228f5c13f6e402706d204061ab843e7 100644 (file)
@@ -59,6 +59,8 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
     self->st = NULL;
     self->in_use = 0;
 
+    assert(PyUnicode_Check(sql));
+
     sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len);
     if (sql_cstr == NULL) {
         rc = PYSQLITE_SQL_WRONG_TYPE;