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
list.index does not accept None as start or stop #57549
Comments
The list.index method does not accept None as start and stop, which makes the error message quite confusing: >>> [1, 2, 3].index(2, None, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method I checked this in 3.2.2 and 2.7.2. Seems similar to bpo-12163. |
The same issue exists for tuples: >>> (1, 2, 3).index(2, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method |
New changeset 0f0eda4daac7 by Petri Lehtinen in branch '2.7': New changeset 5c1fcaf3cf1c by Petri Lehtinen in branch '3.2': New changeset c33aa14f4edb by Petri Lehtinen in branch 'default': |
The relevant code is in _PyEval_SliceIndex() in Python/ceval.c. |
I think this "fix" was too hastily committed.
Be careful. Don't rush to commit. Especially for backports. |
I saw that other code used the same approach as I used in the fix. The comment above _PyEval_SliceIndex() suggests it's used in other What do you suggest? Doing it in _PyEval_SliceIndex() in 2.7 is |
The API in 2.7 shouldn't be changed. Also, it is not clear that the API should change even in 3.3. The list.index() method is not required to accept None. It is not different than other APIs that use PyArg_ParseTuple() with an "i" field for a start, stop, or step argument. |
str.index does accept None, though |
Eh? Don't we already have this? Python 2.7.2 (default, Aug 22 2011, 13:53:27)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> range(5)[None:2]
[0, 1] Or am I misunderstanding? |
Ah, no, sorry. I wasn't aware of this. Now the error message set by _PyEval_SliceIndex() makes sense. It doesn't itself accept None, but apply_slice() and assign_slice() handle the None case. There's still the question whether {list,tuple}.index() should accept None or not. |
The API should not be changed for Py2.7 and Py3.2. Those changesets should be reverted. For Py3.3, it is open to discussion, but we probably don't need the change (making every other implementation also change for nearly zero benefit). |
One other thought: the API for list.index() doesn't exist in isolation. There is also str.index, the sequence abstract base class, and tons of code that has been written to emulate lists. This is an ancient API (approx 20 years) and should only be changed with care. IOW, I don't think this change should have been made at all, at least not without a discussion on python-dev and motivating use cases. |
New changeset 19ffa12ffdd4 by Petri Lehtinen in branch '2.7': New changeset ed0e85efac47 by Petri Lehtinen in branch '3.2': New changeset 106f9e1ad7ab by Petri Lehtinen in branch 'default': |
It's now reverted on all branches. I posted to python-dev alreay earlier. |
Per discussion in the issue, I'm changing this to a bugfix on the message text. If the python-dev discussion resulted (or results in the future) in a desire to change the API, that should be a new issue. |
See http://thread.gmane.org/gmane.comp.python.devel/127502 for the python-dev thread. |
The error message was fixed under bpo-29935, so I think this issue can now be closed. |
Thank you for the reminder Irit. Your comments for issues and PRs are really helpful. Thank you for all this! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: