classification
Title: Add start and stop parameters to the array.index()
Type: enhancement Stage: patch review
Components: Extension Modules Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Phaqui, niki.spahiev, rhettinger, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2017-11-06 11:13 by niki.spahiev, last changed 2017-11-17 07:27 by python-dev.

Files
File name Uploaded Description Edit
WIP-issue-31956 Phaqui, 2017-11-12 23:05
Pull Requests
URL Status Linked Edit
PR 4435 open python-dev, 2017-11-17 07:27
Messages (8)
msg305629 - (view) Author: Николай Спахиев (niki.spahiev) Date: 2017-11-06 11:13
Sequence protocol specifies 2 optional argument for index method:
seq.index(value, [start, [stop]])

array.index(value) needs start and stop arguments too.
msg305643 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-11-06 15:12
+1
msg305650 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-06 16:22
Николай Спахиев: Are you only asking for the feature, or are you interested to work on a concrete patch?
msg305758 - (view) Author: Николай Спахиев (niki.spahiev) Date: 2017-11-07 13:42
I plan to work on a patch.
msg306133 - (view) Author: Anders Lorentsen (Phaqui) * Date: 2017-11-12 23:05
I decided to work on this, and I would like some review, as this would be my second contribution to cpython. Also, a general question:

As I defined the start and end arguments Py_ssize_t, bigger indexes (more negative or more positive) than what can fit in Py_ssize_t will trigger an overflow error. This should be OK, though, as other functions with index arguments has them as Py_ssize_t - and getarrayitem() itself accepts a Py_ssize_t. Or?
msg306143 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-13 06:57
Just take list.index as an example.
msg306150 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-13 08:34
Anders Lorentsen: Py_ssize_t is the correct type for an index in the C language. It's not technically possible to create an array object larger than PY_SSIZE_T_MAX items :-)

Serhiy> Just take list.index as an example.

I concur with Serhiy :-)
msg306178 - (view) Author: Anders Lorentsen (Phaqui) * Date: 2017-11-14 00:00
Writing my tests, I originally looked at Lib/test/seq_tests.py. One test case uses indexes that are (+-)4*sys.maxsize. This does not fit in Py_ssize_t, and so these tests cause my array implementation to raise an overflow exception.

A solution is of course to have the function take general objects instead, and then truncate them down to a number that can fit in Py_ssize_t if it's too negative or positive).

But I concur. It seems more reasonable to stay consistent with the rest of the module, too.

I'll look over the test code to make sure I test for every given scenario (or as many as I can think of), and prepare a PR for this, then :)
History
Date User Action Args
2017-11-17 07:27:58python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request4379
2017-11-14 00:00:39Phaquisetmessages: + msg306178
2017-11-13 08:34:00vstinnersetmessages: + msg306150
2017-11-13 06:57:36serhiy.storchakasetversions: + Python 3.7, - Python 3.8
nosy: + serhiy.storchaka

messages: + msg306143

components: + Extension Modules, - Library (Lib)
type: enhancement
2017-11-12 23:05:19Phaquisetfiles: + WIP-issue-31956
nosy: + Phaqui
messages: + msg306133

2017-11-07 13:42:39niki.spahievsetmessages: + msg305758
2017-11-06 16:22:06vstinnersetnosy: + vstinner
messages: + msg305650
2017-11-06 15:12:03rhettingersetnosy: + rhettinger
messages: + msg305643
2017-11-06 11:13:47niki.spahievcreate