Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47a9813

Browse files
Issue #10203: sqlite3.Row now truly supports sequence protocol. In particulr
it supports reverse() and negative indices. Original patch by Claudiu Popa.
1 parent 432810f commit 47a9813

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

Lib/sqlite3/dbapi2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import datetime
2424
import time
25+
import collections.abc
2526

2627
from _sqlite3 import *
2728

@@ -50,6 +51,7 @@ def TimestampFromTicks(ticks):
5051
sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
5152

5253
Binary = memoryview
54+
collections.abc.Sequence.register(Row)
5355

5456
def register_adapters_and_converters():
5557
def adapt_date(val):

Lib/sqlite3/test/factory.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import unittest
2525
import sqlite3 as sqlite
26+
from collections.abc import Sequence
2627

2728
class MyConnection(sqlite.Connection):
2829
def __init__(self, *args, **kwargs):
@@ -96,9 +97,19 @@ def CheckSqliteRowIndex(self):
9697
self.assertEqual(col1, 1, "by name: wrong result for column 'A'")
9798
self.assertEqual(col2, 2, "by name: wrong result for column 'B'")
9899

99-
col1, col2 = row[0], row[1]
100-
self.assertEqual(col1, 1, "by index: wrong result for column 0")
101-
self.assertEqual(col2, 2, "by index: wrong result for column 1")
100+
self.assertEqual(row[0], 1, "by index: wrong result for column 0")
101+
self.assertEqual(row[1], 2, "by index: wrong result for column 1")
102+
self.assertEqual(row[-1], 2, "by index: wrong result for column -1")
103+
self.assertEqual(row[-2], 1, "by index: wrong result for column -2")
104+
105+
with self.assertRaises(IndexError):
106+
row['c']
107+
with self.assertRaises(IndexError):
108+
row[2]
109+
with self.assertRaises(IndexError):
110+
row[-3]
111+
with self.assertRaises(IndexError):
112+
row[2**1000]
102113

103114
def CheckSqliteRowIter(self):
104115
"""Checks if the row object is iterable"""
@@ -142,6 +153,15 @@ def CheckSqliteRowHashCmp(self):
142153
self.assertNotEqual(row_1, row_3)
143154
self.assertNotEqual(hash(row_1), hash(row_3))
144155

156+
def CheckSqliteRowAsSequence(self):
157+
""" Checks if the row object can act like a sequence """
158+
self.con.row_factory = sqlite.Row
159+
row = self.con.execute("select 1 as a, 2 as b").fetchone()
160+
161+
as_tuple = tuple(row)
162+
self.assertEqual(list(reversed(row)), list(reversed(as_tuple)))
163+
self.assertIsInstance(row, Sequence)
164+
145165
def tearDown(self):
146166
self.con.close()
147167

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #10203: sqlite3.Row now truly supports sequence protocol. In particulr
22+
it supports reverse() and negative indices. Original patch by Claudiu Popa.
23+
2124
- Issue #18807: If copying (no symlinks) specified for a venv, then the python
2225
interpreter aliases (python, python3) are now created by copying rather than
2326
symlinking.

Modules/_sqlite/row.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
6363
return 0;
6464
}
6565

66+
PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
67+
{
68+
PyObject* item = PyTuple_GetItem(self->data, idx);
69+
Py_XINCREF(item);
70+
return item;
71+
}
72+
6673
PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
6774
{
68-
long _idx;
75+
Py_ssize_t _idx;
6976
char* key;
7077
Py_ssize_t nitems, i;
7178
char* compare_key;
@@ -76,7 +83,11 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
7683
PyObject* item;
7784

7885
if (PyLong_Check(idx)) {
79-
_idx = PyLong_AsLong(idx);
86+
_idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
87+
if (_idx == -1 && PyErr_Occurred())
88+
return NULL;
89+
if (_idx < 0)
90+
_idx += PyTuple_GET_SIZE(self->data);
8091
item = PyTuple_GetItem(self->data, _idx);
8192
Py_XINCREF(item);
8293
return item;
@@ -198,6 +209,14 @@ PyMappingMethods pysqlite_row_as_mapping = {
198209
/* mp_ass_subscript */ (objobjargproc)0,
199210
};
200211

212+
static PySequenceMethods pysqlite_row_as_sequence = {
213+
/* sq_length */ (lenfunc)pysqlite_row_length,
214+
/* sq_concat */ 0,
215+
/* sq_repeat */ 0,
216+
/* sq_item */ (ssizeargfunc)pysqlite_row_item,
217+
};
218+
219+
201220
static PyMethodDef pysqlite_row_methods[] = {
202221
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
203222
PyDoc_STR("Returns the keys of the row.")},
@@ -251,5 +270,6 @@ extern int pysqlite_row_setup_types(void)
251270
{
252271
pysqlite_RowType.tp_new = PyType_GenericNew;
253272
pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
273+
pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
254274
return PyType_Ready(&pysqlite_RowType);
255275
}

0 commit comments

Comments
 (0)