[pypy-svn] r57495 - in pypy/dist/pypy/lib: . test2

arigo at codespeak.net arigo at codespeak.net
Wed Aug 20 12:43:38 CEST 2008


Author: arigo
Date: Wed Aug 20 12:43:36 2008
New Revision: 57495

Modified:
   pypy/dist/pypy/lib/itertools.py
   pypy/dist/pypy/lib/test2/test_itertools.py
Log:
Performance issue and fix.


Modified: pypy/dist/pypy/lib/itertools.py
==============================================================================
--- pypy/dist/pypy/lib/itertools.py	(original)
+++ pypy/dist/pypy/lib/itertools.py	Wed Aug 20 12:43:36 2008
@@ -368,14 +368,14 @@
                 self.donext = self.it.next
             except AttributeError:
                 raise TypeError
-        while self.cnt < self.start:
-            self.donext()
+        nextindex = self.start
+        if self.stop is not None and nextindex >= self.stop:
+            raise StopIteration
+        while self.cnt <= nextindex:
+            nextitem = self.donext()
             self.cnt += 1
-        if self.stop is None or self.cnt < self.stop:
-            self.start += self.step 
-            self.cnt += 1
-            return self.donext()
-        raise StopIteration 
+        self.start += self.step 
+        return nextitem
 
 class izip:
     """Make an iterator that aggregates elements from each of the

Modified: pypy/dist/pypy/lib/test2/test_itertools.py
==============================================================================
--- pypy/dist/pypy/lib/test2/test_itertools.py	(original)
+++ pypy/dist/pypy/lib/test2/test_itertools.py	Wed Aug 20 12:43:36 2008
@@ -9,3 +9,15 @@
         it = self.itertools.chain([], [1, 2, 3])
         lst = list(it)
         assert lst == [1, 2, 3]
+
+    def test_islice(self):
+        import sys
+        itertools = self.itertools
+
+        slic = itertools.islice(itertools.count(), 1, 10, sys.maxint)
+        assert len(list(slic)) == 1
+
+        if '__pypy__' not in sys.builtin_module_names:
+            skip("this takes ages on top of CPython's itertools module")
+        slic = itertools.islice(itertools.count(), 1, 10, sys.maxint-20)
+        assert len(list(slic)) == 1



More information about the Pypy-commit mailing list