diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2008-06-20 14:53:43 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2008-06-20 14:53:43 (GMT) |
commit | 1ec2fcd16e3c28e1532829d9a4f2cf29f8ebf559 (patch) | |
tree | 2acd565c41af554d9a7ab7b60630d7d456b902d5 | |
parent | 7b2e2df7406e8d6d29e98388964145b9a5de8316 (diff) | |
download | cpython-1ec2fcd16e3c28e1532829d9a4f2cf29f8ebf559.zip cpython-1ec2fcd16e3c28e1532829d9a4f2cf29f8ebf559.tar.gz cpython-1ec2fcd16e3c28e1532829d9a4f2cf29f8ebf559.tar.bz2 |
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).
-rw-r--r-- | Lib/test/test_slice.py | 14 | ||||
-rw-r--r-- | Misc/NEWS | 7 | ||||
-rw-r--r-- | Objects/sliceobject.c | 5 |
3 files changed, 24 insertions, 2 deletions
diff --git a/Lib/test/test_slice.py b/Lib/test/test_slice.py index 83f051f..8c90c10 100644 --- a/Lib/test/test_slice.py +++ b/Lib/test/test_slice.py @@ -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) @@ -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(). diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 075418e..8748fed 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -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) |