[Python-checkins] bpo-37406: sqlite3 raises TypeError for wrong operation type (GH-14386)

Victor Stinner webhook-mailer at python.org
Tue Jun 25 21:16:28 EDT 2019


https://github.com/python/cpython/commit/c6a2320e876354ee62cf8149b849bcff9492d38a
commit: c6a2320e876354ee62cf8149b849bcff9492d38a
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2019-06-26T03:16:24+02:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2019-06-26-03-00-06.bpo-37406.uovkpq.rst
M Lib/sqlite3/test/dbapi.py
M Lib/sqlite3/test/regression.py
M Modules/_sqlite/connection.c
M Modules/_sqlite/cursor.c
M Modules/_sqlite/statement.c

diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py
index 7c259d2af418..be11337154bd 100644
--- a/Lib/sqlite3/test/dbapi.py
+++ b/Lib/sqlite3/test/dbapi.py
@@ -230,7 +230,7 @@ def CheckExecuteTooMuchSql3(self):
             """)
 
     def CheckExecuteWrongSqlArg(self):
-        with self.assertRaises(ValueError):
+        with self.assertRaises(TypeError):
             self.cu.execute(42)
 
     def CheckExecuteArgInt(self):
@@ -377,7 +377,7 @@ def mygen():
         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):
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index 865bd88f74f1..ee326168bc12 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -261,7 +261,7 @@ def CheckConnectionCall(self):
         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
index 000000000000..9a195ffae218
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-26-03-00-06.bpo-37406.uovkpq.rst
@@ -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.
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 0d6462ef7dc2..5ceeaf98ee80 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -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);
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 01b9dc44cb5d..38d94b2fb590 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -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;
         }
 
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
index 491294b0209c..9de8f9b67228 100644
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -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;



More information about the Python-checkins mailing list