[Python-checkins] cpython (2.7): Issue #10203: sqlite3.Row now truly supports sequence protocol. In particular

serhiy.storchaka python-checkins at python.org
Wed May 28 12:02:59 CEST 2014


http://hg.python.org/cpython/rev/6e2833ae1718
changeset:   90875:6e2833ae1718
branch:      2.7
parent:      90868:e770d8c4291c
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed May 28 12:57:38 2014 +0300
summary:
  Issue #10203: sqlite3.Row now truly supports sequence protocol.  In particular
it supports reverse() and negative indices.  Original patch by Claudiu Popa.

files:
  Lib/sqlite3/dbapi2.py       |   2 +
  Lib/sqlite3/test/factory.py |  34 ++++++++++++++++++++++--
  Misc/NEWS                   |   3 ++
  Modules/_sqlite/row.c       |  26 +++++++++++++++++-
  4 files changed, 60 insertions(+), 5 deletions(-)


diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py
--- a/Lib/sqlite3/dbapi2.py
+++ b/Lib/sqlite3/dbapi2.py
@@ -21,6 +21,7 @@
 #    misrepresented as being the original software.
 # 3. This notice may not be removed or altered from any source distribution.
 
+import collections
 import datetime
 import time
 
@@ -51,6 +52,7 @@
 sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
 
 Binary = buffer
+collections.Sequence.register(Row)
 
 def register_adapters_and_converters():
     def adapt_date(val):
diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py
--- a/Lib/sqlite3/test/factory.py
+++ b/Lib/sqlite3/test/factory.py
@@ -23,6 +23,7 @@
 
 import unittest
 import sqlite3 as sqlite
+from collections import Sequence
 
 class MyConnection(sqlite.Connection):
     def __init__(self, *args, **kwargs):
@@ -96,9 +97,27 @@
         self.assertEqual(col1, 1, "by name: wrong result for column 'A'")
         self.assertEqual(col2, 2, "by name: wrong result for column 'B'")
 
-        col1, col2 = row[0], row[1]
-        self.assertEqual(col1, 1, "by index: wrong result for column 0")
-        self.assertEqual(col2, 2, "by index: wrong result for column 1")
+        self.assertEqual(row[0], 1, "by index: wrong result for column 0")
+        self.assertEqual(row[0L], 1, "by index: wrong result for column 0")
+        self.assertEqual(row[1], 2, "by index: wrong result for column 1")
+        self.assertEqual(row[1L], 2, "by index: wrong result for column 1")
+        self.assertEqual(row[-1], 2, "by index: wrong result for column -1")
+        self.assertEqual(row[-1L], 2, "by index: wrong result for column -1")
+        self.assertEqual(row[-2], 1, "by index: wrong result for column -2")
+        self.assertEqual(row[-2L], 1, "by index: wrong result for column -2")
+
+        with self.assertRaises(IndexError):
+            row['c']
+        with self.assertRaises(IndexError):
+            row[2]
+        with self.assertRaises(IndexError):
+            row[2L]
+        with self.assertRaises(IndexError):
+            row[-3]
+        with self.assertRaises(IndexError):
+            row[-3L]
+        with self.assertRaises(IndexError):
+            row[2**1000]
 
     def CheckSqliteRowIter(self):
         """Checks if the row object is iterable"""
@@ -142,6 +161,15 @@
         self.assertNotEqual(row_1, row_3)
         self.assertNotEqual(hash(row_1), hash(row_3))
 
+    def CheckSqliteRowAsSequence(self):
+        """ Checks if the row object can act like a sequence """
+        self.con.row_factory = sqlite.Row
+        row = self.con.execute("select 1 as a, 2 as b").fetchone()
+
+        as_tuple = tuple(row)
+        self.assertEqual(list(reversed(row)), list(reversed(as_tuple)))
+        self.assertIsInstance(row, Sequence)
+
     def tearDown(self):
         self.con.close()
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@
 Library
 -------
 
+- Issue #10203: sqlite3.Row now truly supports sequence protocol.  In particulr
+  it supports reverse() and negative indices.  Original patch by Claudiu Popa.
+
 - Issue #8743: Fix interoperability between set objects and the
   collections.Set() abstract base class.
 
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c
--- a/Modules/_sqlite/row.c
+++ b/Modules/_sqlite/row.c
@@ -64,9 +64,16 @@
     return 0;
 }
 
+PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
+{
+   PyObject* item = PyTuple_GetItem(self->data, idx);
+   Py_XINCREF(item);
+   return item;
+}
+
 PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
 {
-    long _idx;
+    Py_ssize_t _idx;
     char* key;
     int nitems, i;
     char* compare_key;
@@ -78,11 +85,17 @@
 
     if (PyInt_Check(idx)) {
         _idx = PyInt_AsLong(idx);
+        if (_idx < 0)
+           _idx += PyTuple_GET_SIZE(self->data);
         item = PyTuple_GetItem(self->data, _idx);
         Py_XINCREF(item);
         return item;
     } else if (PyLong_Check(idx)) {
-        _idx = PyLong_AsLong(idx);
+        _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
+        if (_idx == -1 && PyErr_Occurred())
+            return NULL;
+        if (_idx < 0)
+           _idx += PyTuple_GET_SIZE(self->data);
         item = PyTuple_GetItem(self->data, _idx);
         Py_XINCREF(item);
         return item;
@@ -199,6 +212,14 @@
     /* mp_ass_subscript */ (objobjargproc)0,
 };
 
+static PySequenceMethods pysqlite_row_as_sequence = {
+   /* sq_length */         (lenfunc)pysqlite_row_length,
+   /* sq_concat */         0,
+   /* sq_repeat */         0,
+   /* sq_item */           (ssizeargfunc)pysqlite_row_item,
+};
+
+
 static PyMethodDef pysqlite_row_methods[] = {
     {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
         PyDoc_STR("Returns the keys of the row.")},
@@ -252,5 +273,6 @@
 {
     pysqlite_RowType.tp_new = PyType_GenericNew;
     pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
+    pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
     return PyType_Ready(&pysqlite_RowType);
 }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list