Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r62155:f22a27ebaeed
Date: 2013-03-06 19:52 -0500
http://bitbucket.org/pypy/pypy/changeset/f22a27ebaeed/

Log:    improve sqlite parameter type checking, test

diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -1099,7 +1099,9 @@
         self._in_use = True
 
         num_params_needed = 
sqlite.sqlite3_bind_parameter_count(self._statement)
-        if not isinstance(params, dict):
+        if isinstance(params, (tuple, list)) or \
+                not isinstance(params, dict) and \
+                hasattr(params, '__len__') and hasattr(params, '__getitem__'):
             num_params = len(params)
             if num_params != num_params_needed:
                 raise ProgrammingError("Incorrect number of bindings supplied. 
"
@@ -1111,7 +1113,7 @@
                 if rc != SQLITE_OK:
                     raise InterfaceError("Error binding parameter %d - "
                                          "probably unsupported type." % i)
-        else:
+        elif isinstance(params, dict):
             for i in range(1, num_params_needed + 1):
                 param_name = 
sqlite.sqlite3_bind_parameter_name(self._statement, i)
                 if param_name is None:
@@ -1129,6 +1131,8 @@
                     raise InterfaceError("Error binding parameter :%s - "
                                          "probably unsupported type." %
                                          param_name)
+        else:
+            raise ValueError("parameters are of unsupported type")
 
     def _next(self, cursor):
         if self._exhausted:
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
@@ -126,3 +126,20 @@
         con.commit()
     except _sqlite3.OperationalError:
         pytest.fail("_sqlite3 knew nothing about the implicit ROLLBACK")
+
+def test_statement_param_checking():
+    con = _sqlite3.connect(':memory:')
+    con.execute('create table foo(x)')
+    con.execute('insert into foo(x) values (?)', [2])
+    con.execute('insert into foo(x) values (?)', (2,))
+    class seq(object):
+        def __len__(self):
+            return 1
+        def __getitem__(self, key):
+            return 2
+    con.execute('insert into foo(x) values (?)', seq())
+    with pytest.raises(_sqlite3.ProgrammingError):
+        con.execute('insert into foo(x) values (?)', {2:2})
+    with pytest.raises(ValueError) as e:
+        con.execute('insert into foo(x) values (?)', 2)
+    assert str(e.value) == 'parameters are of unsupported type'
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to