Commits

Antonio Cuni  committed 4c82034

add a UnicodeListStrategy

  • Participants
  • Parent commits 01af5d1
  • Branches unicode-strategies

Comments (0)

Files changed (2)

File pypy/objspace/std/listobject.py

     storage = strategy.erase(None)
     return W_ListObject.from_storage_and_strategy(space, storage, strategy)
 
-@jit.look_inside_iff(lambda space, list_w, sizehint: jit.isconstant(len(list_w)) and len(list_w) < UNROLL_CUTOFF)
+@jit.look_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:
     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):
     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
             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:
     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)
 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):
     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

File pypy/objspace/std/test/test_liststrategies.py

 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
 
 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)
         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)
         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)