[Python-checkins] r86877 - in python/branches/release27-maint: Lib/test/test_itertools.py Misc/NEWS Modules/itertoolsmodule.c

raymond.hettinger python-checkins at python.org
Tue Nov 30 04:15:35 CET 2010


Author: raymond.hettinger
Date: Tue Nov 30 04:15:35 2010
New Revision: 86877

Log:
Issue #10323: Predictable final state for slice().

Modified:
   python/branches/release27-maint/Lib/test/test_itertools.py
   python/branches/release27-maint/Misc/NEWS
   python/branches/release27-maint/Modules/itertoolsmodule.c

Modified: python/branches/release27-maint/Lib/test/test_itertools.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_itertools.py	(original)
+++ python/branches/release27-maint/Lib/test/test_itertools.py	Tue Nov 30 04:15:35 2010
@@ -778,6 +778,11 @@
         self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1)
         self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
 
+        # Issue #10323:  Less islice in a predictable state
+        c = count()
+        self.assertEqual(list(islice(c, 1, 3, 50)), [1])
+        self.assertEqual(next(c), 3)
+
     def test_takewhile(self):
         data = [1, 3, 5, 20, 2, 4, 6, 8]
         underten = lambda x: x<10

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Tue Nov 30 04:15:35 2010
@@ -22,6 +22,10 @@
 Library
 -------
 
+- Issue #10323: itertools.islice() now consumes the minimum number of
+  inputs before stopping.  Formerly, the final state of the underlying
+  iterator was undefined.
+
 - Issue #10565: The collections.Iterator ABC now checks for both
   ``__iter__`` and ``next``.
 

Modified: python/branches/release27-maint/Modules/itertoolsmodule.c
==============================================================================
--- python/branches/release27-maint/Modules/itertoolsmodule.c	(original)
+++ python/branches/release27-maint/Modules/itertoolsmodule.c	Tue Nov 30 04:15:35 2010
@@ -1215,6 +1215,7 @@
 {
     PyObject *item;
     PyObject *it = lz->it;
+    Py_ssize_t stop = lz->stop;
     Py_ssize_t oldnext;
     PyObject *(*iternext)(PyObject *);
 
@@ -1226,7 +1227,7 @@
         Py_DECREF(item);
         lz->cnt++;
     }
-    if (lz->stop != -1 && lz->cnt >= lz->stop)
+    if (stop != -1 && lz->cnt >= stop)
         return NULL;
     item = iternext(it);
     if (item == NULL)
@@ -1234,8 +1235,8 @@
     lz->cnt++;
     oldnext = lz->next;
     lz->next += lz->step;
-    if (lz->next < oldnext)     /* Check for overflow */
-        lz->next = lz->stop;
+    if (lz->next < oldnext || (stop != -1 && lz->next > stop))
+        lz->next = stop;
     return item;
 }
 


More information about the Python-checkins mailing list