[pypy-svn] r33497 - in pypy/dist/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Oct 20 14:06:37 CEST 2006


Author: cfbolz
Date: Fri Oct 20 14:06:35 2006
New Revision: 33497

Modified:
   pypy/dist/pypy/objspace/std/rangeobject.py
   pypy/dist/pypy/objspace/std/test/test_rangeobject.py
Log:
fix annotation problems. Make sure in the tests that the results are not
forced. Implement special versions of reverse and sort for range list (somewhat
useless, but fun).


Modified: pypy/dist/pypy/objspace/std/rangeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/rangeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/rangeobject.py	Fri Oct 20 14:06:35 2006
@@ -1,7 +1,9 @@
 from pypy.objspace.std.objspace import *
+from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.inttype import wrapint
 from pypy.objspace.std.sliceobject import W_SliceObject
 from pypy.objspace.std.listobject import W_ListObject
+from pypy.objspace.std import listtype
 
 from pypy.objspace.std import slicetype
 from pypy.interpreter import gateway, baseobjspace
@@ -19,7 +21,7 @@
 
 
 class W_RangeListObject(W_Object):
-    from pypy.objspace.std.listtype import list_typedef as typedef
+    typedef = listtype.list_typedef
     
     def __init__(w_self, start, step, length):
         assert step != 0
@@ -35,7 +37,7 @@
         step = w_self.step
         length = w_self.length
         if not length:
-            w_self.w_list = space.newlist()
+            w_self.w_list = space.newlist([])
             return w_self.w_list
         
         arr = [0] * length  # this is to avoid using append.
@@ -79,7 +81,7 @@
         return space.getitem(w_rangelist.w_list, w_index)
     idx = space.int_w(w_index)
     try:
-        return w_rangelist.getitem(idx)
+        return wrapint(space, w_rangelist.getitem(idx))
     except IndexError:
         raise OperationError(space.w_IndexError,
                              space.wrap("list index out of range"))
@@ -112,6 +114,32 @@
         n += 1
     return space.wrap("[" + ", ".join(result) + "]")
 
+def list_reverse__RangeList(space, w_rangelist):
+    # probably somewhat useless, but well...
+    if w_rangelist.w_list is not None:
+        w_boundmethod = space.getattr(w_rangelist.w_list,
+                                      space.wrap("reverse"))
+        return space.call_function(w_boundmethod)
+    w_rangelist.start = w_rangelist.getitem(-1)
+    w_rangelist.step = -w_rangelist.step
+
+def list_sort__RangeList_None_None_ANY(space, w_rangelist, w_cmp,
+                                       w_keyfunc, w_reverse):
+    # even more useless but fun
+    has_reverse = space.is_true(w_reverse)
+    if w_rangelist.w_list is not None:
+        w_sort = space.getattr(w_rangelist.w_list, space.wrap("sort"))
+        return space.call_function(w_sort, w_cmd, w_keyfunc, w_reverse)
+    if has_reverse:
+        factor = -1
+    else:
+        factor = 1
+    reverse = w_rangelist.step * factor < 0
+    if reverse:
+        w_rangelist.start = w_rangelist.getitem(-1)
+        w_rangelist.step = -w_rangelist.step
+    return space.w_None
+
 class W_RangeIterObject(W_Object):
     from pypy.objspace.std.itertype import iter_typedef as typedef
 
@@ -158,4 +186,4 @@
 registerimplementation(W_RangeListObject)
 registerimplementation(W_RangeIterObject)
 
-register_all(vars())
+register_all(vars(), listtype)

Modified: pypy/dist/pypy/objspace/std/test/test_rangeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_rangeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_rangeobject.py	Fri Oct 20 14:06:35 2006
@@ -6,23 +6,53 @@
 
     def setup_class(cls):
         cls.space = gettestobjspace(**{"objspace.std.withrangelist": True})
+        cls.w_not_forced = cls.space.appexec([], """():
+            import sys
+            def f(r):
+                return (isinstance(r, list) and
+                        "W_ListObject" not in sys.pypy_repr(r))
+            return f
+        """)
 
     def test_simple(self):
         result = []
-        for i in range(1, 8, 2):
+        r = range(1, 8, 2)
+        for i in r:
             result.append(i)
         assert result == [1, 3, 5, 7]
+        assert self.not_forced(r)
 
     def test_getitem_slice(self):
         result = []
-        for i in range(1, 100, 2)[40:30:-2]:
+        r = range(1, 100, 2)
+        for i in r[40:30:-2]:
             result.append(i)
         assert result == [81, 77, 73, 69, 65]
+        assert self.not_forced(r)
 
     def test_repr(self):
-        assert repr(range(5)) == "[0, 1, 2, 3, 4]"
+        r = range(5)
+        assert repr(r) == "[0, 1, 2, 3, 4]"
+        assert self.not_forced(r)
 
     def test_force(self):
         r = range(10)
         r[0] = 42
+        assert not self.not_forced(r)
         assert r == [42, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+    def test_reverse(self):
+        r = range(10)
+        r.reverse()
+        assert self.not_forced(r)
+        assert r == range(9, -1, -1)
+
+    def test_sort(self):
+        r = range(10, -1, -1)
+        r.sort()
+        assert self.not_forced(r)
+        assert r == range(11)
+        r = range(11)
+        r.sort(reverse=True)
+        assert self.not_forced(r)
+        assert r == range(10, -1, -1)



More information about the Pypy-commit mailing list