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
Bug in slice.indices() #47254
Comments
When calling the indices method of a slice object with a negative stop Current behavior: >>> slice(-10).indices(11)
(0, 1, 1)
>>> slice(-10).indices(10)
(0, 0, 1)
>>> slice(-10).indices(9)
(0, -1, 1)
>>> slice(-10).indices(8)
(0, -1, 1) Expected behavior: >>> slice(-10).indices(11)
(0, 1, 1)
>>> slice(-10).indices(10)
(0, 0, 1)
>>> slice(-10).indices(9)
(0, 0, 1)
>>> slice(-10).indices(8)
(0, 0, 1) The patch I submit trivially fixes this while preserving the expected -1 >>> slice(None, -10, -1).indices(8)
(7, -1, -1) This issue affects python 2.5 and 2.6 at least. I did not test with |
I agree that those -1s should really be 0s. Do you have any examples of real-life code |
It's for code that I am developping. I developped a class to allow I agree that for the common usage of computing the limits of a for In fact, I think this bug should really be more of a documentation bug
Documentation for the method will be submitted later tomorrow. Should |
Don't blame me for the delay, I have long days (yes, really up to 96 As for the documentation patch, I'm not certain anymore about it. So unless I'm just a really bad writer, I think the current doc could be |
Could you provide some tests for the fixed behaviour? I'll try to check this in (with appropriate tests) after the beta. |
Would these do? self.assertEqual(slice(None, -10 ).indices(10), (0, 0, 1)) If yes, test_slice.patch adds them. |
On Wed, Jun 18, 2008 at 4:56 PM, Arnaud Bergeron <report@bugs.python.org>
Perfect. Thank you. If this is changed, then I think the following should also be (9, 10, -1) I believe the second index here should be 9, not 10. With these two changes the code, while marginally |
Sorry: looks like I messed up that last post. The example should be: >>> slice(10, 10, -1).indices(10) # expect (9, 9, -1)
(9, 10, -1) |
Here's a new patch that incorporates Arnaud's fix and tests, together with While I expect that this change will affect very little code, I think it's
start, stop, step = my_slice.indices(length) it's guaranteed that 0 <= start <= stop <= length if step is positive, and However, I'd like a second opinion from another core developer before |
That should be: 0 <= start <= length and 0 <= stop <= length (step > 0), and it's not guaranteed that start <= stop always holds in the first case, |
Looks like a straight-forward patch. |
Committed, r64426. Thanks for the report, Arnaud. |
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: