[pypy-svn] r15785 - pypy/dist/pypy/module/__builtin__

ale at codespeak.net ale at codespeak.net
Mon Aug 8 20:12:16 CEST 2005


Author: ale
Date: Mon Aug  8 20:12:14 2005
New Revision: 15785

Modified:
   pypy/dist/pypy/module/__builtin__/__init__.py
   pypy/dist/pypy/module/__builtin__/app_functional.py
   pypy/dist/pypy/module/__builtin__/operation.py
Log:
changed xrange and reversed from a generator to an iterator, since test_iterlen expects to get the length of the running iterator.

reversed is moved from app level to interplevel and from app_functional to operation

Modified: pypy/dist/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/__init__.py	(original)
+++ pypy/dist/pypy/module/__builtin__/__init__.py	Mon Aug  8 20:12:14 2005
@@ -28,7 +28,7 @@
         'enumerate'     : 'app_functional.enumerate',
         'xrange'        : 'app_functional.xrange',
         'sorted'        : 'app_functional.sorted',
-        'reversed'      : 'app_functional.reversed',
+        #'reversed'      : 'app_functional.reversed',
 
         'issubclass'    : 'app_inspect.issubclass',
         'isinstance'    : 'app_inspect.isinstance',
@@ -79,6 +79,7 @@
         '_isfake'       : 'special._isfake',
 
         # interp-level function definitions
+        'reversed'      : 'operation.interp_reversed',
         'abs'           : 'operation.abs',
         'chr'           : 'operation.chr',
         'unichr'        : 'operation.unichr',

Modified: pypy/dist/pypy/module/__builtin__/app_functional.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/app_functional.py	(original)
+++ pypy/dist/pypy/module/__builtin__/app_functional.py	Mon Aug  8 20:12:14 2005
@@ -282,7 +282,8 @@
             n = get_len_of_range(stop, start, -step) 
         self.start = start
         self.len = n 
-        self.step = step 
+        self.step = step
+        self.index = 0
 
     def __str__(self): 
         stop = self.start + self.len * self.step 
@@ -296,7 +297,7 @@
     __repr__ = __str__
 
     def __len__(self):
-        return self.len 
+        return self.len - self.index
 
     def __getitem__(self, index):
         # xrange does NOT support slicing
@@ -310,10 +311,16 @@
         raise IndexError, "xrange object index out of range"
 
     def __iter__(self):
-        i = 0
-        while i < self.len:   
-            yield self.start + i * self.step 
-            i += 1 
+        return self
+    
+    def next(self):
+        #i = 0
+        if self.index < self.len:   
+            res = self.start + self.index * self.step 
+            self.index += 1 
+            return res
+        else:
+            raise StopIteration
 
 # ____________________________________________________________
 
@@ -323,16 +330,3 @@
     sorted_lst.sort(cmp, key, reverse)
     return sorted_lst
 
-def reversed(iterable):
-    """reversed(sequence) -> reverse iterator over values of the sequence
-
-    Return a reverse iterator
-    """
-    if hasattr(iterable, '__reversed__'):
-        return iterable.__reversed__()
-    seq = list(iterable)
-    def reversed_gen(local_iterable):
-        len_iterable = len(local_iterable)
-        for index in range(len_iterable-1, -1, -1):
-            yield local_iterable[index]
-    return reversed_gen(seq)

Modified: pypy/dist/pypy/module/__builtin__/operation.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/operation.py	(original)
+++ pypy/dist/pypy/module/__builtin__/operation.py	Mon Aug  8 20:12:14 2005
@@ -7,6 +7,20 @@
 from pypy.interpreter.error import OperationError 
 NoneNotWrapped = gateway.NoneNotWrapped
 
+
+from pypy.objspace.std.iterobject import W_SeqIterObject
+
+def interp_reversed(space,w_iterable):
+    # if obj has __reversed__ call and return  it
+    
+    func = space.lookup(w_iterable,'__reversed__')
+    if func :
+        return space.call_function(func,w_iterable)
+    # else return W_IterObject with reverse set
+    else:
+        w_seq = W_SeqIterObject(space,w_iterable,-1,True)
+        return w_seq
+
 def abs(space, w_val):
     "abs(number) -> number\n\nReturn the absolute value of the argument."
     return space.abs(w_val)



More information about the Pypy-commit mailing list