[pypy-svn] rev 694 - pypy/trunk/src/pypy/objspace/std

tismer at codespeak.net tismer at codespeak.net
Thu May 29 17:14:36 CEST 2003


Author: tismer
Date: Thu May 29 17:14:36 2003
New Revision: 694

Modified:
   pypy/trunk/src/pypy/objspace/std/listobject.py
Log:
list.reverse works

Modified: pypy/trunk/src/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/listobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/listobject.py	Thu May 29 17:14:36 2003
@@ -46,6 +46,9 @@
     def count(w_self, w_any):
         return list_count(w_self.space, w_self, w_any)
 
+    def reverse(w_self):
+        return list_reverse(w_self.space, w_self)
+
 def list_unwrap(space, w_list):
     items = [space.unwrap(w_item) for w_item in w_list.ob_item[:w_list.ob_size]]
     return list(items)
@@ -211,6 +214,9 @@
     if space.is_true(space.eq(w_attr, space.wrap('count'))):
         w_builtinfn = make_builtin_func(space, W_ListObject.count)
         return W_InstMethObject(space, w_list, w_builtinfn)
+    if space.is_true(space.eq(w_attr, space.wrap('reverse'))):
+        w_builtinfn = make_builtin_func(space, W_ListObject.reverse)
+        return W_InstMethObject(space, w_list, w_builtinfn)
     raise FailedToImplement(space.w_AttributeError)
 
 StdObjSpace.getattr.register(getattr_list, W_ListObject, W_ANY)
@@ -370,6 +376,23 @@
             count += 1
     return space.wrap(count)
 
+# Reverse a slice of a list in place, from lo up to (exclusive) hi.
+# (also used in sort, later)
+
+def _reverse_slice(lis, lo, hi):
+    hi -= 1
+    while lo < hi:
+        t = lis[lo]
+        lis[lo] = lis[hi]
+        lis[hi] = t
+        lo += 1
+        hi -= 1
+
+def list_reverse(space, w_list):
+    if w_list.ob_size > 1:
+        _reverse_slice(w_list.ob_item, 0, w_list.ob_size)
+    return space.w_None
+
 """
 static PyMethodDef list_methods[] = {
     {"append",  (PyCFunction)listappend,  METH_O, append_doc},


More information about the Pypy-commit mailing list