Issue #3004: Minor fix to slice.indices(). slice(-10).indices(9) now

returns (0, 0, 1) instead of (0, -1, 1), and slice(None, 10, -1).indices(10)
returns (9, 9, -1) instead of (9, 10, -1).
This commit is contained in:
Mark Dickinson 2008-06-20 14:53:43 +00:00
parent 7b2e2df740
commit 1ec2fcd16e
3 changed files with 24 additions and 2 deletions

View File

@ -79,6 +79,20 @@ class SliceTest(unittest.TestCase):
self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1))
self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2))
self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2))
# issue 3004 tests
self.assertEqual(slice(None, -9).indices(10), (0, 1, 1))
self.assertEqual(slice(None, -10).indices(10), (0, 0, 1))
self.assertEqual(slice(None, -11).indices(10), (0, 0, 1))
self.assertEqual(slice(None, -10, -1).indices(10), (9, 0, -1))
self.assertEqual(slice(None, -11, -1).indices(10), (9, -1, -1))
self.assertEqual(slice(None, -12, -1).indices(10), (9, -1, -1))
self.assertEqual(slice(None, 9).indices(10), (0, 9, 1))
self.assertEqual(slice(None, 10).indices(10), (0, 10, 1))
self.assertEqual(slice(None, 11).indices(10), (0, 10, 1))
self.assertEqual(slice(None, 8, -1).indices(10), (9, 8, -1))
self.assertEqual(slice(None, 9, -1).indices(10), (9, 9, -1))
self.assertEqual(slice(None, 10, -1).indices(10), (9, 9, -1))
self.assertEqual(
slice(-100, 100 ).indices(10),
slice(None).indices(10)

View File

@ -10,6 +10,13 @@ What's New in Python 2.6 beta 2?
Core and Builtins
-----------------
- Issue #3004: Minor change to slice.indices(): the start and stop
arguments are now treated identically, making the behaviour easier
to describe and understand. For example, slice(None, -10,
1).indices(9) now returns (0, 0, 1) instead of (0, -1, 1), and
slice(None, 10, -1).indices(10) returns (9, 9, -1) instead of (9,
10, -1).
- Make bin() implementation parallel oct() and hex().

View File

@ -169,8 +169,9 @@ PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
else {
if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
if (*stop < 0) *stop += length;
if (*stop < 0) *stop = -1;
if (*stop > length) *stop = length;
if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
if (*stop >= length)
*stop = (*step < 0) ? length - 1 : length;
}
if ((*step < 0 && *stop >= *start)