[pypy-svn] r57496 - in pypy/dist/pypy/module/itertools: . test

arigo at codespeak.net arigo at codespeak.net
Wed Aug 20 13:34:05 CEST 2008


Author: arigo
Date: Wed Aug 20 13:34:04 2008
New Revision: 57496

Modified:
   pypy/dist/pypy/module/itertools/interp_itertools.py
   pypy/dist/pypy/module/itertools/test/test_itertools.py
Log:
Tweak our interp itertools tests until they pass in CPython 2.5.
Apply a few fixes to interp_itertools to make them pass too.


Modified: pypy/dist/pypy/module/itertools/interp_itertools.py
==============================================================================
--- pypy/dist/pypy/module/itertools/interp_itertools.py	(original)
+++ pypy/dist/pypy/module/itertools/interp_itertools.py	Wed Aug 20 13:34:04 2008
@@ -9,21 +9,17 @@
     def __init__(self, space, firstval):
         self.space = space
         self.c = firstval
-        self.overflowed = False
 
     def iter_w(self):
         return self.space.wrap(self)
 
     def next_w(self):
-        if self.overflowed:
-            raise OperationError(self.space.w_OverflowError,
-                    self.space.wrap("cannot count beyond sys.maxint"))
-
         c = self.c
         try:
             self.c = ovfcheck(self.c + 1)
         except OverflowError:
-            self.overflowed = True
+            raise OperationError(self.space.w_OverflowError,
+                    self.space.wrap("cannot count beyond sys.maxint"))
 
         return self.space.wrap(c)
 
@@ -289,7 +285,10 @@
             start = 0
             w_stop = w_startstop
         elif num_args <= 2:
-            start = space.int_w(w_startstop)
+            if space.is_w(w_startstop, space.w_None):
+                start = 0
+            else:
+                start = space.int_w(w_startstop)
             w_stop = args_w[0]
         else:
             raise OperationError(space.w_TypeError, space.wrap("islice() takes at most 4 arguments (" + str(num_args) + " given)"))
@@ -302,7 +301,11 @@
             stoppable = True
 
         if num_args == 2:
-            step = space.int_w(args_w[1])
+            w_step = args_w[1]
+            if space.is_w(w_step, space.w_None):
+                step = 1
+            else:
+                step = space.int_w(w_step)
         else:
             step = 1
 

Modified: pypy/dist/pypy/module/itertools/test/test_itertools.py
==============================================================================
--- pypy/dist/pypy/module/itertools/test/test_itertools.py	(original)
+++ pypy/dist/pypy/module/itertools/test/test_itertools.py	Wed Aug 20 13:34:04 2008
@@ -21,10 +21,11 @@
     def test_count_overflow(self):
         import itertools, sys
 
-        it = itertools.count(sys.maxint)
-        assert it.next() == sys.maxint
-        raises(OverflowError, it.next) 
-        raises(OverflowError, it.next) 
+        # this checks for exact implementation details... that's 2.5 behavior
+        it = itertools.count(sys.maxint - 1)
+        assert it.next() == sys.maxint - 1
+        raises(OverflowError, it.next)
+        raises(OverflowError, it.next)
 
         raises(OverflowError, itertools.count, sys.maxint + 1)
 
@@ -41,20 +42,21 @@
         import itertools
 
         times = 10
-        it = itertools.repeat(None, times=times)
+        it = itertools.repeat(None, times)
         for i in range(times):
             it.next()
         raises(StopIteration, it.next)
 
-        it = itertools.repeat(None, times=None)
-        for x in range(10):
-            it.next()    # Should be no StopIteration
+        #---does not work in CPython 2.5
+        #it = itertools.repeat(None, None)
+        #for x in range(10):
+        #    it.next()    # Should be no StopIteration
 
-        it = itertools.repeat(None, times=0)
+        it = itertools.repeat(None, 0)
         raises(StopIteration, it.next)
         raises(StopIteration, it.next)
 
-        it = itertools.repeat(None, times=-1)
+        it = itertools.repeat(None, -1)
         raises(StopIteration, it.next)
         raises(StopIteration, it.next)
 
@@ -198,10 +200,16 @@
 
         assert list(itertools.islice(xrange(100), 10, 3)) == []
 
+        # new in 2.5: start=None or step=None
+        assert list(itertools.islice(xrange(10), None)) == range(10)
+        assert list(itertools.islice(xrange(10), None,None)) == range(10)
+        assert list(itertools.islice(xrange(10), None,None,None)) == range(10)
+
     def test_islice_overflow(self):
         import itertools
         import sys
-
+        if '__pypy__' not in sys.builtin_module_names:
+            skip("CPython 2.5 gives a strange ValueError")
         raises(OverflowError, itertools.islice, [], sys.maxint + 1)
 
     def test_islice_wrongargs(self):
@@ -216,7 +224,6 @@
 
         raises(ValueError, itertools.islice, [], 0, 0, -1)
         raises(ValueError, itertools.islice, [], 0, 0, 0)
-        raises(TypeError, itertools.islice, [], 0, 0, None)
 
         raises(TypeError, itertools.islice, [], 0, 0, 0, 0)
 
@@ -260,8 +267,9 @@
     def test_imap(self):
         import itertools
 
-        it = itertools.imap(None)
-        raises(StopIteration, it.next)
+        #---does not work in CPython 2.5
+        #it = itertools.imap(None)
+        #raises(StopIteration, it.next)
 
         obj_list = [object(), object(), object()]
         it = itertools.imap(None, obj_list)
@@ -291,8 +299,6 @@
         import itertools
         
         # Duplicate python 2.4 behaviour for invalid arguments
-        it = itertools.imap(0)
-        raises(StopIteration, it.next)
         it = itertools.imap(0, [])
         raises(StopIteration, it.next)
         it = itertools.imap(0, [0])
@@ -325,9 +331,10 @@
         for x in [(1, 5), (2, 6)]:
             assert it.next() == x
         raises(StopIteration, it.next)
-        assert it1.next() == 4
-        raises(StopIteration, it.next)
-        assert it1.next() == 5
+        assert it1.next() in [3, 4]
+        #---does not work in CPython 2.5
+        #raises(StopIteration, it.next)
+        #assert it1.next() in [4, 5]
 
     def test_izip_wrongargs(self):
         import itertools, re
@@ -512,7 +519,7 @@
             itertools.groupby([]),
             itertools.ifilter(None, []),
             itertools.ifilterfalse(None, []),
-            itertools.imap(None),
+            itertools.imap(None, []),
             itertools.islice([], 0),
             itertools.izip(),
             itertools.repeat(None),
@@ -553,7 +560,13 @@
     
     def test_subclassing(self):
         import itertools
-        # Although (mostly) implemented as classes, the itertools functions should not be subclassable
+        # Although (mostly) implemented as classes, the itertools functions
+        # should probably not be subclassable.  They are in CPython but
+        # subclassing them is insane if you ask me (and really forbidden
+        # by the docs, that pretend that they are functions).
+        import sys
+        if '__pypy__' not in sys.builtin_module_names:
+            skip("itertools types are subclassable in CPython")
         iterables = [
             itertools.chain,
             itertools.count,



More information about the Pypy-commit mailing list