This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sqlite3.Row doesn't support slice indexes
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: ghaering, jesstess, pitrou, python-dev, serhiy.storchaka, xapple
Priority: normal Keywords: needs review, patch

Created on 2011-12-11 21:45 by xapple, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sqlrowslice.patch xapple, 2011-12-11 21:45 Patch for implementing slices in sqlite3.Row review
issue13583.patch jesstess, 2014-04-28 06:03 review
sqlite3_slicing_demo.py jesstess, 2014-04-28 06:04
sqlite3_row_slice.patch serhiy.storchaka, 2015-03-07 17:33 review
Messages (9)
msg149251 - (view) Author: Lucas Sinclair (xapple) Date: 2011-12-11 21:45
When using the sqlite3.Row object as a row factory, one can access the resulting rows by index (such as row[1]) or by name (such as row['b']). However, the slice functionality is lost, as doing row[0:2] raises the error:

"slices not implemented, yet"

Here is a patch that fixes this, I implemented it and I added the corresponding unit test.
msg149871 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-19 15:17
Thanks for the patch. Two things:

- there is a compilation warning using gcc:

/home/antoine/cpython/default/Modules/_sqlite/row.c: In function ‘pysqlite_row_subscript’:
/home/antoine/cpython/default/Modules/_sqlite/row.c:128:26: attention : passing argument 1 of ‘PySlice_GetIndicesEx’ from incompatible pointer type

- you can use assertEqual to avoid defining the error message yourself
msg217343 - (view) Author: Jessica McKellar (jesstess) * (Python triager) Date: 2014-04-28 06:03
Thanks for the ticket and patch, xapple!

I updated the patch to address the compiler warning and use assertEqual.

While testing, I noticed that slicing with steps wasn't supported, so I expanded the sqlite3.Row slicing code to support steps, and added some additional tests.

The slicing code in Modules/_sqlite/row.c:pysqlite_row_subscript is unfortunately pretty redundant with the slicing code in Objects/tupleobject.c. It'd be better to either be able to factor the code from both into a function (but I couldn't see how to do this without making it part of the public API), or have tuple, sqlite.Row, etc.  implement a shared slicing interface. Perhaps we should defer that decision to a future ticket, though.

Note that even after this patch, sqlite.Row instances don't support negative indices.

* This patch passes `make patchcheck`.
* The full test suite passes with this patch.
* There are no build warnings related to the patch.
msg217344 - (view) Author: Jessica McKellar (jesstess) * (Python triager) Date: 2014-04-28 06:04
I've also uploaded a short script that sets up an in-memory sqlite database that fetches Rows, for easy manual testing.
msg233840 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-01-11 08:12
May be just use PyObject_GetItem(self->data, idx)?
msg237462 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-07 17:33
Here is a patch with much simpler implementation.
msg239172 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-24 20:01
Could you look at the patch Gerhard?
msg239680 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-03-31 10:34
New changeset e47f520eb756 by Serhiy Storchaka in branch 'default':
Issue #13583: sqlite3.Row now supports slice indexing.
https://hg.python.org/cpython/rev/e47f520eb756
msg239681 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-31 10:36
The implementation can be much simpler, but in any case thank you for your patches Lucas and Jessica.
History
Date User Action Args
2022-04-11 14:57:24adminsetgithub: 57792
2015-03-31 10:36:54serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg239681

stage: patch review -> resolved
2015-03-31 10:34:18python-devsetnosy: + python-dev
messages: + msg239680
2015-03-31 10:25:01serhiy.storchakasetassignee: ghaering -> serhiy.storchaka
2015-03-24 20:01:26serhiy.storchakasetmessages: + msg239172
2015-03-07 17:33:56serhiy.storchakasetfiles: + sqlite3_row_slice.patch

messages: + msg237462
components: + Extension Modules
2015-01-11 08:12:51serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg233840
2015-01-11 02:01:02ghaeringsetassignee: ghaering
2014-04-28 06:04:55jesstesssetfiles: + sqlite3_slicing_demo.py

messages: + msg217344
2014-04-28 06:03:24jesstesssetfiles: + issue13583.patch
versions: + Python 3.5, - Python 3.3
nosy: + jesstess

messages: + msg217343

keywords: + needs review
2011-12-19 15:17:40pitrousetversions: - Python 2.7, Python 3.2
nosy: + pitrou

messages: + msg149871

stage: patch review
2011-12-16 23:55:54terry.reedysetnosy: + ghaering

versions: - Python 2.6
2011-12-11 21:45:47xapplecreate