Author: Antonio Cuni <[email protected]>
Branch: unicode-strategies
Changeset: r58458:4c820346ebe3
Date: 2012-10-26 16:04 +0200
http://bitbucket.org/pypy/pypy/changeset/4c820346ebe3/
Log: add a UnicodeListStrategy
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
@@ -32,7 +32,8 @@
storage = strategy.erase(None)
return W_ListObject.from_storage_and_strategy(space, storage, strategy)
[email protected]_inside_iff(lambda space, list_w, sizehint:
jit.isconstant(len(list_w)) and len(list_w) < UNROLL_CUTOFF)
[email protected]_inside_iff(lambda space, list_w, sizehint:
+ jit.isconstant(len(list_w)) and len(list_w) <
UNROLL_CUTOFF)
def get_strategy_from_list_objects(space, list_w, sizehint):
if not list_w:
if sizehint != -1:
@@ -53,6 +54,13 @@
else:
return space.fromcache(StringListStrategy)
+ # check for unicode
+ for w_obj in list_w:
+ if not is_W_UnicodeObject(w_obj):
+ break
+ else:
+ return space.fromcache(UnicodeListStrategy)
+
# check for floats
for w_obj in list_w:
if not is_W_FloatObject(w_obj):
@@ -70,6 +78,10 @@
from pypy.objspace.std.stringobject import W_StringObject
return type(w_object) is W_StringObject
+def is_W_UnicodeObject(w_object):
+ from pypy.objspace.std.unicodeobject import W_UnicodeObject
+ return type(w_object) is W_UnicodeObject
+
def is_W_FloatObject(w_object):
from pypy.objspace.std.floatobject import W_FloatObject
return type(w_object) is W_FloatObject
@@ -419,6 +431,8 @@
strategy = self.space.fromcache(IntegerListStrategy)
elif is_W_StringObject(w_item):
strategy = self.space.fromcache(StringListStrategy)
+ elif is_W_UnicodeObject(w_item):
+ strategy = self.space.fromcache(UnicodeListStrategy)
elif is_W_FloatObject(w_item):
strategy = self.space.fromcache(FloatListStrategy)
else:
@@ -1036,6 +1050,35 @@
def getitems_str(self, w_list):
return self.unerase(w_list.lstorage)
+
+class UnicodeListStrategy(AbstractUnwrappedStrategy, ListStrategy):
+ _none_value = None
+ _applevel_repr = "unicode"
+
+ def wrap(self, stringval):
+ return self.space.wrap(stringval)
+
+ def unwrap(self, w_string):
+ return self.space.unicode_w(w_string)
+
+ erase, unerase = rerased.new_erasing_pair("unicode")
+ erase = staticmethod(erase)
+ unerase = staticmethod(unerase)
+
+ def is_correct_type(self, w_obj):
+ return is_W_UnicodeObject(w_obj)
+
+ def list_is_correct_type(self, w_list):
+ return w_list.strategy is self.space.fromcache(UnicodeListStrategy)
+
+ def sort(self, w_list, reverse):
+ l = self.unerase(w_list.lstorage)
+ sorter = UnicodeSort(l, len(l))
+ sorter.sort()
+ if reverse:
+ l.reverse()
+
+
# _______________________________________________________
init_signature = Signature(['sequence'], None, None)
@@ -1387,6 +1430,7 @@
IntBaseTimSort = make_timsort_class()
FloatBaseTimSort = make_timsort_class()
StringBaseTimSort = make_timsort_class()
+UnicodeBaseTimSort = make_timsort_class()
class KeyContainer(baseobjspace.W_Root):
def __init__(self, w_key, w_item):
@@ -1414,6 +1458,10 @@
def lt(self, a, b):
return a < b
+class UnicodeSort(UnicodeBaseTimSort):
+ def lt(self, a, b):
+ return a < b
+
class CustomCompareSort(SimpleSort):
def lt(self, a, b):
space = self.space
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,5 +1,5 @@
import sys
-from pypy.objspace.std.listobject import W_ListObject, EmptyListStrategy,
ObjectListStrategy, IntegerListStrategy, FloatListStrategy, StringListStrategy,
RangeListStrategy, make_range_list
+from pypy.objspace.std.listobject import W_ListObject, EmptyListStrategy,
ObjectListStrategy, IntegerListStrategy, FloatListStrategy, StringListStrategy,
RangeListStrategy, make_range_list, UnicodeListStrategy
from pypy.objspace.std import listobject
from pypy.objspace.std.test.test_listobject import TestW_ListObject
@@ -8,34 +8,50 @@
class TestW_ListStrategies(TestW_ListObject):
def test_check_strategy(self):
- assert isinstance(W_ListObject(self.space, []).strategy,
EmptyListStrategy)
- assert isinstance(W_ListObject(self.space,
[self.space.wrap(1),self.space.wrap('a')]).strategy, ObjectListStrategy)
- assert isinstance(W_ListObject(self.space,
[self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)]).strategy,
IntegerListStrategy)
- assert isinstance(W_ListObject(self.space, [self.space.wrap('a'),
self.space.wrap('b')]).strategy, StringListStrategy)
-
+ space = self.space
+ w = space.wrap
+ assert isinstance(W_ListObject(space, []).strategy, EmptyListStrategy)
+ assert isinstance(W_ListObject(space, [w(1),w('a')]).strategy,
ObjectListStrategy)
+ assert isinstance(W_ListObject(space, [w(1),w(2),w(3)]).strategy,
+ IntegerListStrategy)
+ assert isinstance(W_ListObject(space, [w('a'), w('b')]).strategy,
+ StringListStrategy)
+ assert isinstance(W_ListObject(space, [w(u'a'), w(u'b')]).strategy,
+ UnicodeListStrategy)
+ assert isinstance(W_ListObject(space, [w(u'a'), w('b')]).strategy,
+ ObjectListStrategy) # mixed unicode and bytes
+
def test_empty_to_any(self):
- l = W_ListObject(self.space, [])
+ space = self.space
+ w = space.wrap
+ l = W_ListObject(space, [])
assert isinstance(l.strategy, EmptyListStrategy)
- l.append(self.space.wrap((1,3)))
+ l.append(w((1,3)))
assert isinstance(l.strategy, ObjectListStrategy)
- l = W_ListObject(self.space, [])
+ l = W_ListObject(space, [])
assert isinstance(l.strategy, EmptyListStrategy)
- l.append(self.space.wrap(1))
+ l.append(w(1))
assert isinstance(l.strategy, IntegerListStrategy)
- l = W_ListObject(self.space, [])
+ l = W_ListObject(space, [])
assert isinstance(l.strategy, EmptyListStrategy)
- l.append(self.space.wrap('a'))
+ l.append(w('a'))
assert isinstance(l.strategy, StringListStrategy)
- l = W_ListObject(self.space, [])
+ l = W_ListObject(space, [])
assert isinstance(l.strategy, EmptyListStrategy)
- l.append(self.space.wrap(1.2))
+ l.append(w(u'a'))
+ assert isinstance(l.strategy, UnicodeListStrategy)
+
+ l = W_ListObject(space, [])
+ assert isinstance(l.strategy, EmptyListStrategy)
+ l.append(w(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)])
+ l = W_ListObject(self.space,
+
[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)
@@ -43,7 +59,17 @@
assert isinstance(l.strategy, ObjectListStrategy)
def test_string_to_any(self):
- l = W_ListObject(self.space,
[self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
+ l = W_ListObject(self.space,
+
[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)
+
+ def test_unicode_to_any(self):
+ l = W_ListObject(self.space,
+
[self.space.wrap(u'a'),self.space.wrap(u'b'),self.space.wrap('c')])
assert isinstance(l.strategy, StringListStrategy)
l.append(self.space.wrap('d'))
assert isinstance(l.strategy, StringListStrategy)
@@ -51,7 +77,8 @@
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)])
+ 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)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit