Author: Lukas Diekmann <[email protected]>
Branch: list-strategies
Changeset: r47495:df95e88b13b3
Date: 2011-04-11 14:32 +0200
http://bitbucket.org/pypy/pypy/changeset/df95e88b13b3/

Log:    Implemented mul on strategies

diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -121,6 +121,10 @@
         return self.strategy.getitems_copy(self)
     # ___________________________________________________
 
+
+    def mul(self, times):
+        return self.strategy.mul(self, times)
+
     def inplace_mul(self, times):
         self.strategy.inplace_mul(self, times)
 
@@ -183,6 +187,9 @@
     def append(self, w_list, w_item):
         raise NotImplementedError
 
+    def mul(self, w_list, times):
+        raise NotImplementedError
+
     def inplace_mul(self, w_list, times):
         raise NotImplementedError
 
@@ -245,6 +252,9 @@
     def append(self, w_list, w_item):
         w_list.__init__(self.space, [w_item])
 
+    def mul(self, w_list, times):
+        return w_list.clone()
+
     def inplace_mul(self, w_list, times):
         return
 
@@ -376,6 +386,12 @@
             w_list.switch_to_object_strategy()
         w_list.append(w_item)
 
+    def mul(self, w_list, times):
+        #XXX maybe faster to get unwrapped items and create new integer list?
+        w_newlist = w_list.clone()
+        w_newlist.inplace_mul(times)
+        return w_newlist
+
     def inplace_mul(self, w_list, times):
         self.switch_to_integer_strategy(w_list)
         w_list.inplace_mul(times)
@@ -655,6 +671,11 @@
         w_list.check_empty_strategy()
         return w_item
 
+    def mul(self, w_list, times):
+        w_newlist = w_list.clone()
+        w_newlist.inplace_mul(times)
+        return w_newlist
+
     def inplace_mul(self, w_list, times):
         l = self.cast_from_void_star(w_list.lstorage)
         l *= times
@@ -834,6 +855,7 @@
         if e.match(space, space.w_TypeError):
             raise FailedToImplement
         raise
+    return w_list.mul(times)
     return W_ListObject(space, w_list.getitems() * times)
 
 def mul__List_ANY(space, w_list, w_times):
diff --git a/pypy/objspace/std/test/test_liststrategies.py 
b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -305,3 +305,17 @@
         l3 = self.space.add(l1, l2)
         l4 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), 
self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
         assert self.space.eq_w(l3, l4)
+
+    def test_mul(self):
+        l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), 
self.space.wrap(3)])
+        l2 = l1.mul(2)
+        l3 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), 
self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
+        assert self.space.eq_w(l2, l3)
+
+    def test_mul_same_strategy_but_different_object(self):
+        l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), 
self.space.wrap(3)])
+        l2 = l1.mul(1)
+        assert self.space.eq_w(l1, l2)
+        l1.setitem(0, self.space.wrap(5))
+        assert not self.space.eq_w(l1, l2)
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to