[pypy-commit] pypy list-strategies: Convert ListStrategies to ObjectListStrategy when a differen element is added

l.diekmann noreply at buildbot.pypy.org
Fri Sep 23 13:11:16 CEST 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47417:79361ae2fe98
Date: 2011-02-02 11:14 +0100
http://bitbucket.org/pypy/pypy/changeset/79361ae2fe98/

Log:	Convert ListStrategies to ObjectListStrategy when a differen element
	is added

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
@@ -160,9 +160,6 @@
 
     def append(self,  w_list, w_item):
         w_list.wrappeditems.append(w_item)
-        #list_w = cast_from_void_star(w_list.storage, 'object')
-        #list_w.append(w_item)
-        #w_list.storage = cast_to_void_star(list_w, 'object')
 
 class IntegerListStrategy(ListStrategy):
 
@@ -191,6 +188,12 @@
     def append(self,  w_list, w_item):
         w_list.wrappeditems.append(w_item)
 
+        if is_W_IntObject(w_item):
+            return
+
+        w_list.strategy = ObjectListStrategy()
+        w_list.strategy.init_from_list_w(w_list, w_list.wrappeditems)
+
 class StringListStrategy(ListStrategy):
 
     def init_from_list_w(self, w_list, list_w):
@@ -218,6 +221,12 @@
     def append(self, w_list, w_item):
         w_list.wrappeditems.append(w_item)
 
+        if is_W_StringObject(w_item):
+            return
+
+        w_list.strategy = ObjectListStrategy()
+        w_list.strategy.init_from_list_w(w_list, w_list.wrappeditems)
+
 init_signature = Signature(['sequence'], None, None)
 init_defaults = [None]
 
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
@@ -24,3 +24,22 @@
         assert isinstance(l.strategy, EmptyListStrategy)
         l.append(self.space.wrap('a'))
         assert isinstance(l.strategy, StringListStrategy)
+
+    def test_int_to_any(self):
+        l = W_ListObject([self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
+        assert isinstance(l.strategy, IntegerListStrategy)
+        l.append(self.space.wrap(4))
+        assert isinstance(l.strategy, IntegerListStrategy)
+        l.append(self.space.wrap('a'))
+        assert isinstance(l.strategy, ObjectListStrategy)
+
+    def test_string_to_any(self):
+        l = W_ListObject([self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
+        assert isinstance(l.strategy, StringListStrategy)
+        l.append(self.space.wrap('d'))
+        assert isinstance(l.strategy, StringListStrategy)
+        l.append(self.space.wrap(3))
+        assert isinstance(l.strategy, ObjectListStrategy)
+
+
+


More information about the pypy-commit mailing list