diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-05-28 09:58:34 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-05-28 09:58:34 (GMT) |
commit | 47a981337a8f3898482a1c0c51ad2d91c7a6bc2f (patch) | |
tree | 88aef37e02ac0120825904fa8befdc588e028cf2 /Modules | |
parent | 432810f9f35c014afecbd24edda6b26a3cf04fd9 (diff) | |
download | cpython-47a981337a8f3898482a1c0c51ad2d91c7a6bc2f.zip cpython-47a981337a8f3898482a1c0c51ad2d91c7a6bc2f.tar.gz cpython-47a981337a8f3898482a1c0c51ad2d91c7a6bc2f.tar.bz2 |
Issue #10203: sqlite3.Row now truly supports sequence protocol. In particulr
it supports reverse() and negative indices. Original patch by Claudiu Popa.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_sqlite/row.c | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index 4a87a04..d10bd59 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -63,9 +63,16 @@ int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs) 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; Py_ssize_t nitems, i; char* compare_key; @@ -76,7 +83,11 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx) PyObject* item; 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; @@ -198,6 +209,14 @@ PyMappingMethods pysqlite_row_as_mapping = { /* 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.")}, @@ -251,5 +270,6 @@ extern int pysqlite_row_setup_types(void) { 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); } |