[pypy-svn] r8604 - in pypy/dist/pypy: module module/test objspace/std

adim at codespeak.net adim at codespeak.net
Wed Jan 26 13:24:11 CET 2005


Author: adim
Date: Wed Jan 26 13:24:11 2005
New Revision: 8604

Modified:
   pypy/dist/pypy/module/__builtin__module.py
   pypy/dist/pypy/module/test/test_builtin.py
   pypy/dist/pypy/objspace/std/listtype.py
Log:
added sorted and reversed builtins (+ tests)
also added list.__reversed__ definition / implementation in listtype.py
 (assuming that, for now, we don't want to have several possible
  implementation of list.__reversed__)
Side Note : reversed() is implemented as a builtin function, not as a
            type like in Python2.4



Modified: pypy/dist/pypy/module/__builtin__module.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__module.py	(original)
+++ pypy/dist/pypy/module/__builtin__module.py	Wed Jan 26 13:24:11 2005
@@ -924,5 +924,25 @@
     def __init__(self, object, offset=None, size=None):
         raise NotImplementedError, "XXX nobody needs this anyway"
 
+def sorted(lst):
+    "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
+    sorted_lst = list(lst)
+    sorted_lst.sort()
+    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)
+
 from _file import file_ as file
 open = file

Modified: pypy/dist/pypy/module/test/test_builtin.py
==============================================================================
--- pypy/dist/pypy/module/test/test_builtin.py	(original)
+++ pypy/dist/pypy/module/test/test_builtin.py	Wed Jan 26 13:24:11 2005
@@ -180,6 +180,32 @@
         raises(IndexError, x.__getitem__, -18)
         raises(TypeError, x.__getitem__, slice(0,3,1))
 
+    def test_sorted(self):
+        l = []
+        sorted_l = sorted(l)
+        assert sorted_l is not l
+        assert sorted_l == l
+        l = [1, 5, 2, 3]
+        sorted_l = sorted(l)
+        assert sorted_l == [1, 2, 3, 5]
+        
+    def test_reversed_simple_sequences(self):
+        l = range(5)
+        rev = reversed(l)
+        assert list(rev) == [4, 3, 2, 1, 0]
+        assert list(l.__reversed__()) == [4, 3, 2, 1, 0]
+        s = "abcd"
+        assert list(reversed(s)) == ['d', 'c', 'b', 'a']
+
+    def test_reversed_custom_objects(self):
+        """make sure __reversed__ is called when defined"""
+        class SomeClass:
+            def __reversed__(self):
+                return 42
+        obj = SomeClass()
+        assert reversed(obj) == 42
+    
+        
     def test_cmp(self):
         assert cmp(9,9) == 0
         assert cmp(0,9) < 0

Modified: pypy/dist/pypy/objspace/std/listtype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/listtype.py	(original)
+++ pypy/dist/pypy/objspace/std/listtype.py	Wed Jan 26 13:24:11 2005
@@ -1,15 +1,28 @@
 from pypy.objspace.std.stdtypedef import *
+from pypy.objspace.std.register_all import register_all
 from sys import maxint
 
-list_append = MultiMethod('append', 2)
-list_insert = MultiMethod('insert', 3)
-list_extend = MultiMethod('extend', 2)
-list_pop    = MultiMethod('pop',    2, defaults=(-1,))
-list_remove = MultiMethod('remove', 2)
-list_index  = MultiMethod('index',  4, defaults=(0,maxint))
-list_count  = MultiMethod('count',  2)
-list_reverse= MultiMethod('reverse',1)
-list_sort   = MultiMethod('sort',   2, defaults=(None,))
+list_append   = MultiMethod('append', 2)
+list_insert   = MultiMethod('insert', 3)
+list_extend   = MultiMethod('extend', 2)
+list_pop      = MultiMethod('pop',    2, defaults=(-1,))
+list_remove   = MultiMethod('remove', 2)
+list_index    = MultiMethod('index',  4, defaults=(0,maxint))
+list_count    = MultiMethod('count',  2)
+list_reverse  = MultiMethod('reverse',1)
+list_sort     = MultiMethod('sort',   2, defaults=(None,))
+list_reversed = MultiMethod('__reversed__', 1)
+
+def app_list_reversed__ANY(lst):
+    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(lst)
+
+# gateway is imported in the stdtypedef module
+gateway.importall(globals())
+register_all(vars(), globals())
 
 # ____________________________________________________________
 



More information about the Pypy-commit mailing list