[pypy-svn] r34022 - in pypy/branch/transparent-proxy/pypy/objspace/std: . test

fijal at codespeak.net fijal at codespeak.net
Wed Nov 1 14:53:13 CET 2006


Author: fijal
Date: Wed Nov  1 14:53:12 2006
New Revision: 34022

Modified:
   pypy/branch/transparent-proxy/pypy/objspace/std/listobject.py
   pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy.py
   pypy/branch/transparent-proxy/pypy/objspace/std/tlistobject.py
Log:
(arigo, fijal, guido, pedronis) - List >= TransparentList and friends.


Modified: pypy/branch/transparent-proxy/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/branch/transparent-proxy/pypy/objspace/std/listobject.py	(original)
+++ pypy/branch/transparent-proxy/pypy/objspace/std/listobject.py	Wed Nov  1 14:53:12 2006
@@ -123,12 +123,10 @@
         return a
     return b
 
-def lt__List_List(space, w_list1, w_list2):
+def lessthan_unwrappeditems(space, items1_w, items2_w):
     # needs to be safe against eq_w() mutating the w_lists behind our back
     # Search for the first index where items are different
     i = 0
-    items1_w = w_list1.wrappeditems
-    items2_w = w_list2.wrappeditems
     while i < len(items1_w) and i < len(items2_w):
         w_item1 = items1_w[i]
         w_item2 = items2_w[i]
@@ -138,13 +136,11 @@
     # No more items to compare -- compare sizes
     return space.newbool(len(items1_w) < len(items2_w))
 
-def gt__List_List(space, w_list1, w_list2):
+def greaterthan_unwrappeditems(space, items1_w, items2_w):
     # needs to be safe against eq_w() mutating the w_lists behind our back
     # Search for the first index where items are different
     i = 0
-    items1_w = w_list1.wrappeditems
-    items2_w = w_list2.wrappeditems
-    while i < len(w_list1.wrappeditems) and i < len(w_list2.wrappeditems):
+    while i < len(items1_w) and i < len(items2_w):
         w_item1 = items1_w[i]
         w_item2 = items2_w[i]
         if not space.eq_w(w_item1, w_item2):
@@ -153,6 +149,29 @@
     # No more items to compare -- compare sizes
     return space.newbool(len(items1_w) > len(items2_w))
 
+def lt__List_List(space, w_list1, w_list2):
+    return lessthan_unwrappeditems(space, w_list1.wrappeditems,
+        w_list2.wrappeditems)
+
+def lt__List_ANY(space, w_list1, w_any):
+    # XXX: Implement it not unpacking all the elements
+    if space.is_true(space.isinstance(w_any, space.w_list)):
+        items1_w = w_list1.wrappeditems
+        items2_w = space.unpackiterable(w_any)
+        return lessthan_unwrappeditems(space, items1_w, items2_w)
+    raise FailedToImplement
+
+def gt__List_List(space, w_list1, w_list2):
+    return greaterthan_unwrappeditems(space, w_list1.wrappeditems,
+        w_list2.wrappeditems)
+
+def gt__List_ANY(space, w_list1, w_any):
+    # XXX: Implement it not unpacking all the elements
+    if space.is_true(space.isinstance(w_any, space.w_list)):
+        items1_w = w_list1.wrappeditems
+        items2_w = space.unpackiterable(w_any)
+        return greaterthan_unwrappeditems(space, items1_w, items2_w)
+    raise FailedToImplement
 
 def delitem__List_ANY(space, w_list, w_idx):
     idx = space.int_w(w_idx)

Modified: pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy.py
==============================================================================
--- pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy.py	(original)
+++ pypy/branch/transparent-proxy/pypy/objspace/std/test/test_proxy.py	Wed Nov  1 14:53:12 2006
@@ -3,6 +3,17 @@
 """
 
 class AppTestProxy(object):
+    def setup_method(self, meth):
+        self.w_Controller = self.space.appexec([], """():
+        class Controller(object):
+            def __init__(self, obj):
+                self.obj = obj
+    
+            def perform(self, name, *args):
+                return getattr(self.obj, name)(*args)
+        return Controller
+        """)
+    
     def test_proxy(self):
         lst = proxy(list, lambda : None)
         assert type(lst) is list
@@ -17,15 +28,19 @@
         assert repr(lst) == repr([1,2,3])
 
     def test_proxy_append(self):
-        class Controller(object):
-            def __init__(self, obj):
-                self.obj = obj
-    
-            def perform(self, name, *args):
-                return getattr(self.obj, name)(*args)
-
-        c = Controller([])
+        c = self.Controller([])
         lst = proxy(list, c.perform)
         lst.append(1)
         lst.append(2)
         assert repr(lst) == repr([1,2])
+
+    def test_gt_lt_list(self):
+        c = self.Controller([])
+        lst = proxy(list, c.perform)
+        lst.append(1)
+        lst.append(2)
+        assert lst < [1,2,3]
+        assert [1,2,3] > lst
+        #assert not ([2,3] < lst)
+        assert [2,3] >= lst
+        assert lst <= [1,2]

Modified: pypy/branch/transparent-proxy/pypy/objspace/std/tlistobject.py
==============================================================================
--- pypy/branch/transparent-proxy/pypy/objspace/std/tlistobject.py	(original)
+++ pypy/branch/transparent-proxy/pypy/objspace/std/tlistobject.py	Wed Nov  1 14:53:12 2006
@@ -23,5 +23,14 @@
     space.call_function(w_transparent_list.controller, space.wrap("extend"), w_any)
     return space.w_None
 
+def lt__TransparentList_ANY(space, w_transparent_list, w_list):
+    return space.call_function(w_transparent_list.controller, space.wrap("__lt__"), w_list)
+
+def gt__TransparentList_ANY(space, w_transparent_list, w_list):
+    return space.call_function(w_transparent_list.controller, space.wrap("__gt__"), w_list)
+
+def iter__TransparentList(space, w_transparent_list):
+    return space.call_function(w_transparent_list.controller, space.wrap("__iter__"))
+
 from pypy.objspace.std import listtype
 register_all(vars(), listtype)



More information about the Pypy-commit mailing list