Commits

Brian Kearns committed 9a1dee0

improve sqlite3 statement arg checking, test

Comments (0)

Files changed (2)

lib_pypy/_sqlite3.py

         try:
             self.__description = None
             self._reset = False
+            if not isinstance(sql, (str, unicode)):
+                raise ValueError("operation parameter must be str or unicode")
             self.__statement = self.__connection._statement_cache.get(
                 sql, self.row_factory)
 
         try:
             self.__description = None
             self._reset = False
+            if not isinstance(sql, (str, unicode)):
+                raise ValueError("operation parameter must be str or unicode")
             self.__statement = self.__connection._statement_cache.get(
                 sql, self.row_factory)
 
         statement = c_void_p()
         if isinstance(sql, unicode):
             sql = sql.encode('utf-8')
+        elif not isinstance(sql, str):
+            raise ValueError("script argument must be unicode or string.")
         c_sql = c_char_p(sql)
 
         self.__connection.commit()

pypy/module/test_lib_pypy/test_sqlite3.py

     except _sqlite3.OperationalError:
         pytest.fail("_sqlite3 knew nothing about the implicit ROLLBACK")
 
+def test_statement_arg_checking():
+    con = _sqlite3.connect(':memory:')
+    with pytest.raises(_sqlite3.Warning) as e:
+        con(123)
+    assert str(e.value) == 'SQL is of wrong type. Must be string or unicode.'
+    with pytest.raises(ValueError) as e:
+        con.execute(123)
+    assert str(e.value) == 'operation parameter must be str or unicode'
+    with pytest.raises(ValueError) as e:
+        con.executemany(123, 123)
+    assert str(e.value) == 'operation parameter must be str or unicode'
+    with pytest.raises(ValueError) as e:
+        con.executescript(123)
+    assert str(e.value) == 'script argument must be unicode or string.'
+
 def test_statement_param_checking():
     con = _sqlite3.connect(':memory:')
     con.execute('create table foo(x)')