summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-31 10:33:11 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-31 10:33:11 (GMT)
commit72e731cc03f29cdb8bf17bd9ea34c8448954c798 (patch)
tree02c4ac35bf8bdce758dea1e363912fea0ed3a277
parent80d84c89ee8f10cd3a5d1dfa2ea1cd51f810ec33 (diff)
downloadcpython-72e731cc03f29cdb8bf17bd9ea34c8448954c798.zip
cpython-72e731cc03f29cdb8bf17bd9ea34c8448954c798.tar.gz
cpython-72e731cc03f29cdb8bf17bd9ea34c8448954c798.tar.bz2
Issue #13583: sqlite3.Row now supports slice indexing.
Tests by Jessica McKellar.
-rw-r--r--Doc/library/sqlite3.rst3
-rw-r--r--Lib/sqlite3/test/factory.py18
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/_sqlite/row.c3
4 files changed, 24 insertions, 2 deletions
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
index 6097e7a..fc69a80 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -649,6 +649,9 @@ Row Objects
This method returns a list of column names. Immediately after a query,
it is the first member of each tuple in :attr:`Cursor.description`.
+ .. versionchanged:: 3.5
+ Added support of slicing.
+
Let's assume we initialize a table as in the example given above::
conn = sqlite3.connect(":memory:")
diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py
index 98dcae5..3d4eb0b 100644
--- a/Lib/sqlite3/test/factory.py
+++ b/Lib/sqlite3/test/factory.py
@@ -111,6 +111,24 @@ class RowFactoryTests(unittest.TestCase):
with self.assertRaises(IndexError):
row[2**1000]
+ def CheckSqliteRowSlice(self):
+ # A sqlite.Row can be sliced like a list.
+ self.con.row_factory = sqlite.Row
+ row = self.con.execute("select 1, 2, 3, 4").fetchone()
+ self.assertEqual(row[0:0], ())
+ self.assertEqual(row[0:1], (1,))
+ self.assertEqual(row[1:3], (2, 3))
+ self.assertEqual(row[3:1], ())
+ # Explicit bounds are optional.
+ self.assertEqual(row[1:], (2, 3, 4))
+ self.assertEqual(row[:3], (1, 2, 3))
+ # Slices can use negative indices.
+ self.assertEqual(row[-2:-1], (3,))
+ self.assertEqual(row[-2:], (3, 4))
+ # Slicing supports steps.
+ self.assertEqual(row[0:4:2], (1, 3))
+ self.assertEqual(row[3:0:-2], (4, 2))
+
def CheckSqliteRowIter(self):
"""Checks if the row object is iterable"""
self.con.row_factory = sqlite.Row
diff --git a/Misc/NEWS b/Misc/NEWS
index 5fb96f7..bb67d79 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,8 @@ Core and Builtins
Library
-------
+- Issue #13583: sqlite3.Row now supports slice indexing.
+
- Issue #18473: Fixed 2to3 and 3to2 compatible pickle mappings. Fixed
ambigious reverse mappings. Added many new mappings. Import mapping is no
longer applied to modules already mapped with full name mapping.
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c
index ed8ad47..d863643 100644
--- a/Modules/_sqlite/row.c
+++ b/Modules/_sqlite/row.c
@@ -142,8 +142,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
return NULL;
}
else if (PySlice_Check(idx)) {
- PyErr_SetString(PyExc_ValueError, "slices not implemented, yet");
- return NULL;
+ return PyObject_GetItem(self->data, idx);
}
else {
PyErr_SetString(PyExc_IndexError, "Index must be int or string");