-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
bpo-31956: Add start and stop parameters to array.index() #4435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
55d18ca
f9ddd58
94118b2
34692ba
a34e960
3ca98de
e6d75ae
d2b2f9f
d2755af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| array.index() now supports optional start and stop arguments. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1115,18 +1115,31 @@ 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to not document the default value at Python level? Try to remove "= sys.maxsize". Maybe you should put "/" (currently the following line) between start and stop?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to not make stop the keyword argument until other index() methods accept keyword arguments. The signature of array.index() matches the signature of list.index() and this is the best that we can to do now. Due to limitations of pydoc, inspect and Argument Clinic the default value for stop is displayed as a concrete number, but this is a separate issue.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, right, I didn't notice that other methods don't accept keywords. I like the idea of accepting keyword arguments in other index() methods :-)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is an open issue for this. But first I want unify the code for all index() methods. Currently index() methods of str/bytes/bytearray use slightly different code. |
||
| / | ||
|
|
||
| Return index of first occurrence of v in the array. | ||
|
|
||
| Raises ValueError if the value is not present. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually we don't document the exact exception raised by a method. Please remove this addition. Maybe we should start to document them, but I would prefer to have a discussion about that, before starting to document them.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstrings of other index() methods document this. |
||
| [clinic start generated code]*/ | ||
|
|
||
| static PyObject * | ||
| array_array_index(arrayobject *self, PyObject *v) | ||
| /*[clinic end generated code: output=d48498d325602167 input=cf619898c6649d08]*/ | ||
| array_array_index_impl(arrayobject *self, PyObject *v, Py_ssize_t start, | ||
| Py_ssize_t stop) | ||
| /*[clinic end generated code: output=c45e777880c99f52 input=6efdb3dae558556c]*/ | ||
| { | ||
| Py_ssize_t i; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove redundant empty lines. |
||
| for (i = 0; i < Py_SIZE(self); i++) { | ||
| if (start < 0) { | ||
| start += Py_SIZE(self); | ||
| if (start < 0) | ||
| start = 0; | ||
| } | ||
| if (stop < 0) { | ||
| stop += Py_SIZE(self); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is stop is still negative here? Maybe you can use PySlice_AdjustIndices() here with step=1? What do you think @serhiy-storchaka?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All correct if stop is still negative. The following loop just will not be executed. |
||
| } | ||
| for (i = start; i < stop && i < Py_SIZE(self); i++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that you kept the "< Py_SIZE()" check if the array is mutated during the PyObject_RichCompareBool() call? If yes, please add a short comment to explain that, since it can be surprising.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggested to do this. This is an exact copy from list.index(). Do you want to add a comment in list.index()? |
||
| PyObject *selfi; | ||
| int cmp; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can document the default start value:
array.index(x, start=0[, stop])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to specify
start=0, stop=sys.maxsize, but there were objections against this, because this looks like start and stop are keyword parameters. There were issues about changing the documentation back to[, start[, stop]].