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

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Feb 2 15:28:54 CET 2007


Author: cfbolz
Date: Fri Feb  2 15:28:52 2007
New Revision: 37810

Modified:
   pypy/dist/pypy/objspace/std/rangeobject.py
   pypy/dist/pypy/objspace/std/test/test_rangeobject.py
Log:
use FailedToImplement instead of constructing the bound method and calling
that, if the rangelist is forced. implement rangelist.pop(0) and
rangelist.pop(-1) without forcing the list.


Modified: pypy/dist/pypy/objspace/std/rangeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/rangeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/rangeobject.py	Fri Feb  2 15:28:52 2007
@@ -40,17 +40,16 @@
             w_self.w_list = space.newlist([])
             return w_self.w_list
         
-        arr = [0] * length  # this is to avoid using append.
+        arr = [None] * length  # this is to avoid using append.
 
         i = start
         n = 0
         while n < length:
-            arr[n] = i
+            arr[n] = wrapint(space, i)
             i += step
             n += 1
 
-        w_self.w_list = space.newlist([wrapint(space, element)
-                                           for element in arr])
+        w_self.w_list = space.newlist(arr)
         return w_self.w_list
 
     def getitem(w_self, i):
@@ -114,12 +113,33 @@
         n += 1
     return space.wrap("[" + ", ".join(result) + "]")
 
+
+def list_pop__RangeList_ANY(space, w_rangelist, w_idx=-1):
+    if w_rangelist.w_list is not None:
+        raise FailedToImplement
+    length = w_rangelist.length
+    if length == 0:
+        raise OperationError(space.w_IndexError,
+                             space.wrap("pop from empty list"))
+    idx = space.int_w(w_idx)
+    if idx == 0:
+        result = w_rangelist.start
+        w_rangelist.start += w_rangelist.step
+        w_rangelist.length -= 1
+        return wrapint(space, result)
+    if idx == -1 or idx == length - 1:
+        w_rangelist.length -= 1
+        return wrapint(
+            space, w_rangelist.start + (length - 1) * w_rangelist.step)
+    if idx >= w_rangelist.length:
+        raise OperationError(space.w_IndexError,
+                             space.wrap("pop index out of range"))
+    raise FailedToImplement
+
 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)
+        raise FailedToImplement
     w_rangelist.start = w_rangelist.getitem(-1)
     w_rangelist.step = -w_rangelist.step
 
@@ -128,8 +148,7 @@
     # 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_cmp, w_keyfunc, w_reverse)
+        raise FailedToImplement
     if has_reverse:
         factor = -1
     else:

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 Feb  2 15:28:52 2007
@@ -46,6 +46,11 @@
         r.reverse()
         assert self.not_forced(r)
         assert r == range(9, -1, -1)
+        r = range(3)
+        r[0] = 1
+        assert r == [1, 1, 2]
+        r.reverse()
+        assert r == [2, 1, 1]
 
     def test_sort(self):
         r = range(10, -1, -1)
@@ -61,3 +66,27 @@
         assert not self.not_forced(r)
         r.sort()
         assert r == range(1, 100) + [999]
+
+    def test_pop(self):
+        r = range(10)
+        res = r.pop()
+        assert res == 9
+        assert self.not_forced(r)
+        assert repr(r) == repr(range(9))
+        res = r.pop(0)
+        assert res == 0
+        assert self.not_forced(r)
+        assert repr(r) == repr(range(1, 9))
+        res = r.pop(len(r) - 1)
+        assert res == 8
+        assert self.not_forced(r)
+        assert repr(r) == repr(range(1, 8))
+        res = r.pop(2)
+        assert res == 3
+        assert not self.not_forced(r)
+        assert r == [1, 2, 4, 5, 6, 7]
+        res = r.pop(2)
+        assert res == 4
+        assert not self.not_forced(r)
+        assert r == [1, 2, 5, 6, 7]
+       



More information about the Pypy-commit mailing list