Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r62178:9a1dee0e3eb5
Date: 2013-03-07 14:38 -0500
http://bitbucket.org/pypy/pypy/changeset/9a1dee0e3eb5/

Log:    improve sqlite3 statement arg checking, test

diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -800,6 +800,8 @@
         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)
 
@@ -845,6 +847,8 @@
         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)
 
@@ -878,6 +882,8 @@
         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()
diff --git a/pypy/module/test_lib_pypy/test_sqlite3.py 
b/pypy/module/test_lib_pypy/test_sqlite3.py
--- a/pypy/module/test_lib_pypy/test_sqlite3.py
+++ b/pypy/module/test_lib_pypy/test_sqlite3.py
@@ -127,6 +127,21 @@
     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)')
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to