Author: Armin Rigo <[email protected]>
Branch: int-float-list-strategy
Changeset: r78389:4ed327cd6401
Date: 2015-07-01 23:03 +0200
http://bitbucket.org/pypy/pypy/changeset/4ed327cd6401/

Log:    int -> int-or-float

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
@@ -1667,6 +1667,30 @@
                     self.space, storage, self)
         return self._base_setslice(w_list, start, step, slicelength, w_other)
 
+
+    def switch_to_next_strategy(self, w_list, w_sample_item):
+        if type(w_sample_item) is W_FloatObject:
+            l = self.unerase(w_list.lstorage)
+            for intval in l:
+                if not longlong2float.can_encode_int32(intval):
+                    break
+            else:
+                floatval = self.space.float_w(w_sample_item)
+                if longlong2float.can_encode_float(floatval):
+                    # yes, we can switch to IntOrFloatListStrategy
+                    generalized_list = [
+                        longlong2float.encode_int32_into_longlong_nan(intval)
+                        for intval in l]
+                    generalized_list.append(
+                        longlong2float.float2longlong(floatval))
+                    strategy = self.space.fromcache(IntOrFloatListStrategy)
+                    w_list.strategy = strategy
+                    w_list.lstorage = strategy.erase(generalized_list)
+                    return
+        # no, fall back to ObjectListStrategy
+        w_list.switch_to_object_strategy()
+
+
 class FloatListStrategy(ListStrategy):
     import_from_mixin(AbstractUnwrappedStrategy)
 
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
@@ -759,7 +759,7 @@
         w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ok2)])
         assert isinstance(w_l.strategy, IntOrFloatListStrategy)
 
-    def test_int_or_float(self):
+    def test_int_or_float_base(self):
         from rpython.rlib.rfloat import INFINITY, NAN
         space = self.space
         w = space.wrap
@@ -769,6 +769,8 @@
         assert isinstance(w_l.strategy, IntOrFloatListStrategy)
         w_l.append(w(-5.1))
         assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        assert space.int_w(w_l.getitem(2)) == 2**31-1
+        assert space.float_w(w_l.getitem(3)) == -5.1
         w_l.append(w(INFINITY))
         assert isinstance(w_l.strategy, IntOrFloatListStrategy)
         w_l.append(w(NAN))
@@ -778,6 +780,16 @@
         w_l.append(space.newlist([]))
         assert isinstance(w_l.strategy, ObjectListStrategy)
 
+    def test_int_or_float_from_integer(self):
+        space = self.space
+        w = space.wrap
+        w_l = W_ListObject(space, [space.wrap(int(-2**31))])
+        assert isinstance(w_l.strategy, IntegerListStrategy)
+        w_l.append(w(-5.1))
+        assert isinstance(w_l.strategy, IntOrFloatListStrategy)
+        assert space.int_w(w_l.getitem(0)) == -2**31
+        assert space.float_w(w_l.getitem(1)) == -5.1
+
 
 class TestW_ListStrategiesDisabled:
     spaceconfig = {"objspace.std.withliststrategies": False}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to