diff -r 90b56ec318b6 Lib/sqlite3/dbapi2.py --- a/Lib/sqlite3/dbapi2.py Thu Nov 28 06:53:05 2013 -0800 +++ b/Lib/sqlite3/dbapi2.py Sat Nov 30 10:57:50 2013 +0200 @@ -22,6 +22,7 @@ import datetime import time +from collections.abc import Sequence from _sqlite3 import * @@ -50,6 +51,7 @@ sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")]) Binary = memoryview +Sequence.register(Row) def register_adapters_and_converters(): def adapt_date(val): diff -r 90b56ec318b6 Lib/sqlite3/test/factory.py --- a/Lib/sqlite3/test/factory.py Thu Nov 28 06:53:05 2013 -0800 +++ b/Lib/sqlite3/test/factory.py Sat Nov 30 10:57:50 2013 +0200 @@ -23,6 +23,7 @@ import unittest import sqlite3 as sqlite +from collections.abc import Sequence class MyConnection(sqlite.Connection): def __init__(self, *args, **kwargs): @@ -141,6 +142,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 -r 90b56ec318b6 Modules/_sqlite/row.c --- a/Modules/_sqlite/row.c Thu Nov 28 06:53:05 2013 -0800 +++ b/Modules/_sqlite/row.c Sat Nov 30 10:57:50 2013 +0200 @@ -63,6 +63,13 @@ 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; @@ -198,6 +205,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 */ (unaryfunc)pysqlite_row_item, +}; + + static PyMethodDef pysqlite_row_methods[] = { {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, PyDoc_STR("Returns the keys of the row.")}, @@ -251,5 +266,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); }