[pypy-commit] pypy default: fix an overlow bug

hakanardo noreply at buildbot.pypy.org
Thu Nov 3 21:15:29 CET 2011


Author: Hakan Ardo <hakan at debian.org>
Branch: 
Changeset: r48723:7202b0d9cb70
Date: 2011-11-03 21:14 +0100
http://bitbucket.org/pypy/pypy/changeset/7202b0d9cb70/

Log:	fix an overlow bug

diff --git a/pypy/module/__builtin__/functional.py b/pypy/module/__builtin__/functional.py
--- a/pypy/module/__builtin__/functional.py
+++ b/pypy/module/__builtin__/functional.py
@@ -362,7 +362,7 @@
     def descr_reversed(self):
         lastitem = self.start + (self.len-1) * self.step
         return self.space.wrap(W_XRangeIterator(self.space, lastitem,
-                                                self.start - 1, -self.step))
+                                                self.start, -self.step, True))
 
     def descr_reduce(self):
         space = self.space
@@ -389,21 +389,26 @@
 )
 
 class W_XRangeIterator(Wrappable):
-    def __init__(self, space, start, stop, step):
+    def __init__(self, space, start, stop, step, inclusive=False):
         self.space = space
         self.current = start
         self.stop = stop
         self.step = step
+        self.inclusive = inclusive
 
     def descr_iter(self):
         return self.space.wrap(self)
 
     def descr_next(self):
-        if (self.step > 0 and self.current < self.stop) or (self.step < 0 and self.current > self.stop):
-            item = self.current
-            self.current = item + self.step
-            return self.space.wrap(item)
-        raise OperationError(self.space.w_StopIteration, self.space.w_None)
+        if self.inclusive:
+            if not ((self.step > 0 and self.current <= self.stop) or (self.step < 0 and self.current >= self.stop)):
+                raise OperationError(self.space.w_StopIteration, self.space.w_None)
+        else:
+            if not ((self.step > 0 and self.current < self.stop) or (self.step < 0 and self.current > self.stop)):
+                raise OperationError(self.space.w_StopIteration, self.space.w_None)
+        item = self.current
+        self.current = item + self.step
+        return self.space.wrap(item)
 
     #def descr_len(self):
     #    return self.space.wrap(self.remaining)
diff --git a/pypy/module/__builtin__/test/test_functional.py b/pypy/module/__builtin__/test/test_functional.py
--- a/pypy/module/__builtin__/test/test_functional.py
+++ b/pypy/module/__builtin__/test/test_functional.py
@@ -157,7 +157,8 @@
        raises(OverflowError, xrange, a)
        raises(OverflowError, xrange, 0, a)
        raises(OverflowError, xrange, 0, 1, a)
-
+       assert list(reversed(xrange(-sys.maxint-1, -sys.maxint-1, -2))) == []
+       
    def test_xrange_reduce(self):
       x = xrange(2, 9, 3)
       callable, args = x.__reduce__()


More information about the pypy-commit mailing list