[pypy-svn] r15846 - in pypy/dist/pypy/module/__builtin__: . test
arigo at codespeak.net
arigo at codespeak.net
Tue Aug 9 18:20:07 CEST 2005
Author: arigo
Date: Tue Aug 9 18:20:03 2005
New Revision: 15846
Modified:
pypy/dist/pypy/module/__builtin__/__init__.py
pypy/dist/pypy/module/__builtin__/app_functional.py
pypy/dist/pypy/module/__builtin__/operation.py
pypy/dist/pypy/module/__builtin__/test/test_functional.py
Log:
Reverted rev 15785:
* xrange must not be its own iterator. The reason is made explicit in a new
test. Note that xrange iterators don't need to have a __len__; they don't
in CPython.
* pypy.objspace.std cannot be imported from module/, which must not depend
on a particular object space. Similarily, space.lookup() is not a general-
purpose space operation, it is an internal detail of DescrOperation object
spaces.
(I'll try to provide a __len__ for reversed()
iterators directly at app-level, in the next check-in.)
Modified: pypy/dist/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/__init__.py (original)
+++ pypy/dist/pypy/module/__builtin__/__init__.py Tue Aug 9 18:20:03 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,7 +79,6 @@
'_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 Tue Aug 9 18:20:03 2005
@@ -282,8 +282,7 @@
n = get_len_of_range(stop, start, -step)
self.start = start
self.len = n
- self.step = step
- self.index = 0
+ self.step = step
def __str__(self):
stop = self.start + self.len * self.step
@@ -297,7 +296,7 @@
__repr__ = __str__
def __len__(self):
- return self.len - self.index
+ return self.len
def __getitem__(self, index):
# xrange does NOT support slicing
@@ -311,16 +310,10 @@
raise IndexError, "xrange object index out of range"
def __iter__(self):
- 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
+ i = 0
+ while i < self.len:
+ yield self.start + i * self.step
+ i += 1
# ____________________________________________________________
@@ -330,3 +323,16 @@
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 Tue Aug 9 18:20:03 2005
@@ -7,25 +7,6 @@
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:
-
- getitem = space.lookup(w_iterable,'__getitem__')
- len = space.lookup(w_iterable,'__len__')
- if getitem and len :
- w_seq = W_SeqIterObject(space,w_iterable,-1,True)
- return w_seq
- raise OperationError(space.wrap(TypeError),space.wrap('argument to reversed() must be a sequence'))
-
def abs(space, w_val):
"abs(number) -> number\n\nReturn the absolute value of the argument."
return space.abs(w_val)
Modified: pypy/dist/pypy/module/__builtin__/test/test_functional.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/test/test_functional.py (original)
+++ pypy/dist/pypy/module/__builtin__/test/test_functional.py Tue Aug 9 18:20:03 2005
@@ -89,3 +89,12 @@
def test_function(self):
assert filter(lambda x: x != "a", "a small text") == " smll text"
+
+class AppTestXRange:
+ def test_xrange(self):
+ x = xrange(2, 9, 3)
+ assert x[1] == 5
+ assert len(x) == 3
+ assert list(x) == [2, 5, 8]
+ # test again, to make sure that xrange() is not its own iterator
+ assert list(x) == [2, 5, 8]
More information about the Pypy-commit
mailing list