[Python-checkins] r64426 - in python/trunk: Lib/test/test_slice.py Misc/NEWS Objects/sliceobject.c

mark.dickinson python-checkins at python.org
Fri Jun 20 16:53:45 CEST 2008


Author: mark.dickinson
Date: Fri Jun 20 16:53:43 2008
New Revision: 64426

Log:
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).


Modified:
   python/trunk/Lib/test/test_slice.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/sliceobject.c

Modified: python/trunk/Lib/test/test_slice.py
==============================================================================
--- python/trunk/Lib/test/test_slice.py	(original)
+++ python/trunk/Lib/test/test_slice.py	Fri Jun 20 16:53:43 2008
@@ -79,6 +79,20 @@
         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)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Fri Jun 20 16:53:43 2008
@@ -10,6 +10,13 @@
 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().
 
 

Modified: python/trunk/Objects/sliceobject.c
==============================================================================
--- python/trunk/Objects/sliceobject.c	(original)
+++ python/trunk/Objects/sliceobject.c	Fri Jun 20 16:53:43 2008
@@ -169,8 +169,9 @@
 	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) 


More information about the Python-checkins mailing list