[pypy-svn] rev 2666 - pypy/trunk/src/pypy/module

alex at codespeak.net alex at codespeak.net
Mon Dec 22 00:58:11 CET 2003


Author: alex
Date: Mon Dec 22 00:58:10 2003
New Revision: 2666

Modified:
   pypy/trunk/src/pypy/module/builtin.py
Log:
added implementation of __len__ to xrange



Modified: pypy/trunk/src/pypy/module/builtin.py
==============================================================================
--- pypy/trunk/src/pypy/module/builtin.py	(original)
+++ pypy/trunk/src/pypy/module/builtin.py	Mon Dec 22 00:58:10 2003
@@ -742,6 +742,17 @@
                 raise ValueError, 'xrange() step-argument (arg 3) must not be zero'
             self.step = step
 
+        def __len__(self):
+            if not hasattr(self, '_len'):
+                slicelength = self.stop - self.start
+                lengthsign = cmp(slicelength, 0)
+                stepsign = cmp(self.step, 0)
+                if stepsign == lengthsign:
+                    self._len = (slicelength - lengthsign) // self.step + 1
+                else:
+                    self._len = 0
+            return self._len
+
         def __iter__(self):
             def gen(self):
                 start, stop, step = self.start, self.stop, self.step


More information about the Pypy-commit mailing list