]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
SF patch #1171417: bug fix for islice() in docs
authorRaymond Hettinger <python@rcn.com>
Sun, 27 Mar 2005 20:11:44 +0000 (20:11 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 27 Mar 2005 20:11:44 +0000 (20:11 +0000)
Doc/lib/libitertools.tex
Lib/test/test_itertools.py

index 780ed5502372f28bdd2ce9f041dd1b56dca2ec29..9d7f7ca9c4f5f1c56f086887fc40bb451e717a95 100644 (file)
@@ -250,16 +250,14 @@ by functions or loops that truncate the stream.
   third line).  Equivalent to:
 
   \begin{verbatim}
-     def islice(iterable, *args):
+     def islice(iterable, *args):    
          s = slice(*args)
-         next, stop, step = s.start or 0, s.stop, s.step or 1
-         for cnt, element in enumerate(iterable):
-             if cnt < next:
-                 continue
-             if stop is not None and cnt >= stop:
-                 break
-             yield element
-             next += step             
+         it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
+         nexti = it.next()
+         for i, element in enumerate(iterable):
+             if i == nexti:
+                 yield element
+                 nexti = it.next()          
   \end{verbatim}
 
   If \var{start} is \code{None}, then iteration starts at zero.
index 577082bd398e3503e86f29766040ad8510fb1232..5db128cda145539a0a3cd6a04a534122cacdbc3d 100644 (file)
@@ -265,6 +265,11 @@ class TestBasicOps(unittest.TestCase):
         self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10))
         self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2))
 
+        # Test number of items consumed     SF #1171417
+        it = iter(range(10))
+        self.assertEqual(list(islice(it, 3)), range(3))
+        self.assertEqual(list(it), range(3, 10))
+
         # Test invalid arguments
         self.assertRaises(TypeError, islice, xrange(10))
         self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4)