diff options
author | Zackery Spytz <zspytz@gmail.com> | 2021-04-02 15:28:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-02 15:28:35 (GMT) |
commit | afd12650580725ac598b2845384771c14c4f952e (patch) | |
tree | 2290e54eaa5fdb75d0b0513f0e575c440150fafd /Modules/arraymodule.c | |
parent | 240bcf82a11fe7433a61da70605e924c53b88096 (diff) | |
download | cpython-afd12650580725ac598b2845384771c14c4f952e.zip cpython-afd12650580725ac598b2845384771c14c4f952e.tar.gz cpython-afd12650580725ac598b2845384771c14c4f952e.tar.bz2 |
bpo-31956: Add start and stop parameters to array.index() (GH-25059)
Co-Authored-By: Anders Lorentsen <Phaqui@gmail.com>
Diffstat (limited to 'Modules/arraymodule.c')
-rw-r--r-- | Modules/arraymodule.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index e7d5ab7..fb9ebbe 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1136,18 +1136,32 @@ array_array_count(arrayobject *self, PyObject *v) array.array.index v: object + start: slice_index(accept={int}) = 0 + stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize / Return index of first occurrence of v in the array. + +Raise ValueError if the value is not present. [clinic start generated code]*/ static PyObject * -array_array_index(arrayobject *self, PyObject *v) -/*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/ -{ - Py_ssize_t i; - - for (i = 0; i < Py_SIZE(self); i++) { +array_array_index_impl(arrayobject *self, PyObject *v, Py_ssize_t start, + Py_ssize_t stop) +/*[clinic end generated code: output=c45e777880c99f52 input=089dff7baa7e5a7e]*/ +{ + if (start < 0) { + start += Py_SIZE(self); + if (start < 0) { + start = 0; + } + } + if (stop < 0) { + stop += Py_SIZE(self); + } + // Use Py_SIZE() for every iteration in case the array is mutated + // during PyObject_RichCompareBool() + for (Py_ssize_t i = start; i < stop && i < Py_SIZE(self); i++) { PyObject *selfi; int cmp; |