[pypy-commit] pypy default: added floatstrategy for lists
l.diekmann
noreply at buildbot.pypy.org
Wed Nov 30 15:14:37 CET 2011
Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch:
Changeset: r50010:748818c5a463
Date: 2011-11-30 15:03 +0100
http://bitbucket.org/pypy/pypy/changeset/748818c5a463/
Log: added floatstrategy for lists
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
@@ -50,6 +50,13 @@
else:
return space.fromcache(StringListStrategy)
+ # check for floats
+ for w_obj in list_w:
+ if not is_W_FloatObject(w_obj):
+ break
+ else:
+ return space.fromcache(FloatListStrategy)
+
return space.fromcache(ObjectListStrategy)
def is_W_IntObject(w_object):
@@ -60,7 +67,9 @@
from pypy.objspace.std.stringobject import W_StringObject
return type(w_object) is W_StringObject
-
+def is_W_FloatObject(w_object):
+ from pypy.objspace.std.floatobject import W_FloatObject
+ return type(w_object) is W_FloatObject
class W_ListObject(W_AbstractListObject):
from pypy.objspace.std.listtype import list_typedef as typedef
@@ -364,6 +373,8 @@
strategy = self.space.fromcache(IntegerListStrategy)
elif is_W_StringObject(w_item):
strategy = self.space.fromcache(StringListStrategy)
+ elif is_W_FloatObject(w_item):
+ strategy = self.space.fromcache(FloatListStrategy)
else:
strategy = self.space.fromcache(ObjectListStrategy)
@@ -905,6 +916,32 @@
if reverse:
l.reverse()
+class FloatListStrategy(AbstractUnwrappedStrategy, ListStrategy):
+ _none_value = 0.0
+
+ def wrap(self, floatval):
+ return self.space.wrap(floatval)
+
+ def unwrap(self, w_float):
+ return self.space.float_w(w_float)
+
+ erase, unerase = rerased.new_erasing_pair("float")
+ erase = staticmethod(erase)
+ unerase = staticmethod(unerase)
+
+ def is_correct_type(self, w_obj):
+ return is_W_FloatObject(w_obj)
+
+ def list_is_correct_type(self, w_list):
+ return w_list.strategy is self.space.fromcache(FloatListStrategy)
+
+ def sort(self, w_list, reverse):
+ l = self.unerase(w_list.lstorage)
+ sorter = FloatSort(l, len(l))
+ sorter.sort()
+ if reverse:
+ l.reverse()
+
class StringListStrategy(AbstractUnwrappedStrategy, ListStrategy):
_none_value = None
@@ -934,6 +971,7 @@
def getitems_str(self, w_list):
return self.unerase(w_list.lstorage)
+
# _______________________________________________________
init_signature = Signature(['sequence'], None, None)
@@ -1282,6 +1320,7 @@
TimSort = make_timsort_class()
IntBaseTimSort = make_timsort_class()
+FloatBaseTimSort = make_timsort_class()
StringBaseTimSort = make_timsort_class()
class KeyContainer(baseobjspace.W_Root):
@@ -1302,6 +1341,10 @@
def lt(self, a, b):
return a < b
+class FloatSort(FloatBaseTimSort):
+ def lt(self, a, b):
+ return a < b
+
class StringSort(StringBaseTimSort):
def lt(self, a, b):
return a < b
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
@@ -1,4 +1,4 @@
-from pypy.objspace.std.listobject import W_ListObject, EmptyListStrategy, ObjectListStrategy, IntegerListStrategy, StringListStrategy, RangeListStrategy, make_range_list
+from pypy.objspace.std.listobject import W_ListObject, EmptyListStrategy, ObjectListStrategy, IntegerListStrategy, FloatListStrategy, StringListStrategy, RangeListStrategy, make_range_list
from pypy.objspace.std import listobject
from pypy.objspace.std.test.test_listobject import TestW_ListObject
@@ -15,7 +15,7 @@
def test_empty_to_any(self):
l = W_ListObject(self.space, [])
assert isinstance(l.strategy, EmptyListStrategy)
- l.append(self.space.wrap(1.))
+ l.append(self.space.wrap((1,3)))
assert isinstance(l.strategy, ObjectListStrategy)
l = W_ListObject(self.space, [])
@@ -28,6 +28,11 @@
l.append(self.space.wrap('a'))
assert isinstance(l.strategy, StringListStrategy)
+ l = W_ListObject(self.space, [])
+ assert isinstance(l.strategy, EmptyListStrategy)
+ l.append(self.space.wrap(1.2))
+ assert isinstance(l.strategy, FloatListStrategy)
+
def test_int_to_any(self):
l = W_ListObject(self.space, [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
assert isinstance(l.strategy, IntegerListStrategy)
@@ -44,6 +49,14 @@
l.append(self.space.wrap(3))
assert isinstance(l.strategy, ObjectListStrategy)
+ def test_float_to_any(self):
+ l = W_ListObject(self.space, [self.space.wrap(1.1),self.space.wrap(2.2),self.space.wrap(3.3)])
+ assert isinstance(l.strategy, FloatListStrategy)
+ l.append(self.space.wrap(4.4))
+ assert isinstance(l.strategy, FloatListStrategy)
+ l.append(self.space.wrap("a"))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
def test_setitem(self):
# This should work if test_listobject.py passes
l = W_ListObject(self.space, [self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
@@ -65,6 +78,12 @@
l.setitem(0, self.space.wrap(2))
assert isinstance(l.strategy, ObjectListStrategy)
+ # FloatStrategy to ObjectStrategy
+ l = W_ListObject(self.space, [self.space.wrap(1.2),self.space.wrap(2.3),self.space.wrap(3.4)])
+ assert isinstance(l.strategy, FloatListStrategy)
+ l.setitem(0, self.space.wrap("a"))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
def test_insert(self):
# no change
l = W_ListObject(self.space, [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
@@ -84,6 +103,12 @@
l.insert(3, self.space.wrap('d'))
assert isinstance(l.strategy, ObjectListStrategy)
+ # FloatStrategy
+ l = W_ListObject(self.space, [self.space.wrap(1.1),self.space.wrap(2.2),self.space.wrap(3.3)])
+ assert isinstance(l.strategy, FloatListStrategy)
+ l.insert(3, self.space.wrap('d'))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
# EmptyStrategy
l = W_ListObject(self.space, [])
assert isinstance(l.strategy, EmptyListStrategy)
@@ -95,7 +120,9 @@
l.insert(0, self.space.wrap(2))
assert isinstance(l.strategy, IntegerListStrategy)
- def notest_list_empty_after_delete(self):
+ def test_list_empty_after_delete(self):
+ import py
+ py.test.skip("return to emptyliststrategy is not supported anymore")
l = W_ListObject(self.space, [self.space.wrap(3)])
assert isinstance(l.strategy, IntegerListStrategy)
l.deleteitem(0)
@@ -117,21 +144,36 @@
l.setslice(0, 1, 2, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]))
assert isinstance(l.strategy, IntegerListStrategy)
+ # IntegerStrategy to IntegerStrategy
l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
assert isinstance(l.strategy, IntegerListStrategy)
l.setslice(0, 1, 2, W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5), self.space.wrap(6)]))
assert isinstance(l.strategy, IntegerListStrategy)
+ # ObjectStrategy to ObjectStrategy
l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap('b'), self.space.wrap(3)])
assert isinstance(l.strategy, ObjectListStrategy)
l.setslice(0, 1, 2, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]))
assert isinstance(l.strategy, ObjectListStrategy)
+ # IntegerStrategy to ObjectStrategy
l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
assert isinstance(l.strategy, IntegerListStrategy)
l.setslice(0, 1, 2, W_ListObject(self.space, [self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c')]))
assert isinstance(l.strategy, ObjectListStrategy)
+ # StringStrategy to ObjectStrategy
+ l = W_ListObject(self.space, [self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c')])
+ assert isinstance(l.strategy, StringListStrategy)
+ l.setslice(0, 1, 2, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
+ # FloatStrategy to ObjectStrategy
+ l = W_ListObject(self.space, [self.space.wrap(1.1), self.space.wrap(2.2), self.space.wrap(3.3)])
+ assert isinstance(l.strategy, FloatListStrategy)
+ l.setslice(0, 1, 2, W_ListObject(self.space, [self.space.wrap('a'), self.space.wrap(2), self.space.wrap(3)]))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
def test_setslice_List(self):
def wrapitems(items):
@@ -160,6 +202,11 @@
keep_other_strategy(l, 0, 2, other.length(), other)
assert l.strategy is self.space.fromcache(StringListStrategy)
+ l = W_ListObject(self.space, wrapitems([1.1, 2.2, 3.3, 4.4, 5.5]))
+ other = W_ListObject(self.space, [])
+ keep_other_strategy(l, 0, 1, l.length(), other)
+ assert l.strategy is self.space.fromcache(FloatListStrategy)
+
l = W_ListObject(self.space, wrapitems(["a",3,"c",4,"e"]))
other = W_ListObject(self.space, wrapitems(["a", "b", "c"]))
keep_other_strategy(l, 0, 2, other.length(), other)
@@ -194,6 +241,11 @@
l.extend(W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5), self.space.wrap(6)]))
assert isinstance(l.strategy, IntegerListStrategy)
+ l = W_ListObject(self.space, [self.space.wrap(1.1), self.space.wrap(2.2), self.space.wrap(3.3)])
+ assert isinstance(l.strategy, FloatListStrategy)
+ l.extend(W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5), self.space.wrap(6)]))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
def test_empty_extend_with_any(self):
empty = W_ListObject(self.space, [])
assert isinstance(empty.strategy, EmptyListStrategy)
@@ -220,6 +272,11 @@
empty = W_ListObject(self.space, [])
assert isinstance(empty.strategy, EmptyListStrategy)
+ empty.extend(W_ListObject(self.space, [self.space.wrap(1.1), self.space.wrap(2.2), self.space.wrap(3.3)]))
+ assert isinstance(empty.strategy, FloatListStrategy)
+
+ empty = W_ListObject(self.space, [])
+ assert isinstance(empty.strategy, EmptyListStrategy)
empty.extend(W_ListObject(self.space, []))
assert isinstance(empty.strategy, EmptyListStrategy)
@@ -293,12 +350,13 @@
l.setslice(0, 1, 3, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]))
assert isinstance(l.strategy, IntegerListStrategy)
- def test_get_items_copy(self):
+ def test_copy_list(self):
l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
- l2 = l1.getitems()
+ l2 = l1.clone()
l2.append(self.space.wrap(4))
assert not l2 == l1.getitems()
+ def test_getitems_does_not_copy_object_list(self):
l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap("two"), self.space.wrap(3)])
l2 = l1.getitems()
l2.append(self.space.wrap("four"))
@@ -345,7 +403,6 @@
# should not raise
assert getslice__List_ANY_ANY(self.space, l, self.space.wrap(15), self.space.wrap(2222)).strategy == self.space.fromcache(EmptyListStrategy)
-
def test_add_to_rangelist(self):
l1 = make_range_list(self.space, 1, 1, 3)
l2 = W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5)])
More information about the pypy-commit
mailing list