Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r50151:94f777e3c69a
Date: 2011-12-04 19:30 +0100
http://bitbucket.org/pypy/pypy/changeset/94f777e3c69a/

Log:    Convert StringListStrategy into 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
@@ -45,10 +45,10 @@
 
     # check for strings
     for w_obj in list_w:
-        if not is_W_StringObject(w_obj):
+        if not is_W_UnicodeObject(w_obj):
             break
     else:
-        return space.fromcache(StringListStrategy)
+        return space.fromcache(UnicodeListStrategy)
 
     return space.fromcache(ObjectListStrategy)
 
@@ -56,9 +56,9 @@
     from pypy.objspace.std.intobject import W_IntObject
     return type(w_object) is W_IntObject
 
-def is_W_StringObject(w_object):
-    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
 
 
 
@@ -86,7 +86,7 @@
 
     @staticmethod
     def newlist_str(space, list_s):
-        strategy = space.fromcache(StringListStrategy)
+        strategy = space.fromcache(UnicodeListStrategy)
         storage = strategy.erase(list_s)
         return W_ListObject.from_storage_and_strategy(space, storage, strategy)
 
@@ -362,8 +362,8 @@
     def switch_to_correct_strategy(self, w_list, w_item):
         if is_W_IntObject(w_item):
             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)
         else:
             strategy = self.space.fromcache(ObjectListStrategy)
 
@@ -905,28 +905,28 @@
         if reverse:
             l.reverse()
 
-class StringListStrategy(AbstractUnwrappedStrategy, ListStrategy):
+class UnicodeListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = None
 
     def wrap(self, stringval):
         return self.space.wrap(stringval)
 
     def unwrap(self, w_string):
-        return self.space.str_w(w_string)
+        return self.space.unicode_w(w_string)
 
-    erase, unerase = rerased.new_erasing_pair("string")
+    erase, unerase = rerased.new_erasing_pair("unicode")
     erase = staticmethod(erase)
     unerase = staticmethod(unerase)
 
     def is_correct_type(self, w_obj):
-        return is_W_StringObject(w_obj)
+        return is_W_UnicodeObject(w_obj)
 
     def list_is_correct_type(self, w_list):
-        return w_list.strategy is self.space.fromcache(StringListStrategy)
+        return w_list.strategy is self.space.fromcache(UnicodeListStrategy)
 
     def sort(self, w_list, reverse):
         l = self.unerase(w_list.lstorage)
-        sorter = StringSort(l, len(l))
+        sorter = UnicodeSort(l, len(l))
         sorter.sort()
         if reverse:
             l.reverse()
@@ -1256,7 +1256,7 @@
 
 TimSort = make_timsort_class()
 IntBaseTimSort = make_timsort_class()
-StringBaseTimSort = make_timsort_class()
+UnicodeBaseTimSort = make_timsort_class()
 
 class KeyContainer(baseobjspace.W_Root):
     def __init__(self, w_key, w_item):
@@ -1276,7 +1276,7 @@
     def lt(self, a, b):
         return a < b
 
-class StringSort(StringBaseTimSort):
+class UnicodeSort(UnicodeBaseTimSort):
     def lt(self, a, b):
         return a < b
 
diff --git a/pypy/objspace/std/stringobject.py 
b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -236,7 +236,7 @@
 
 def str_split__String_None_ANY(space, w_self, w_none, w_maxsplit=-1):
     maxsplit = space.int_w(w_maxsplit)
-    res = []
+    res_w = []
     value = w_self._value
     length = len(value)
     i = 0
@@ -259,12 +259,12 @@
             maxsplit -= 1   # NB. if it's already < 0, it stays < 0
 
         # the word is value[i:j]
-        res.append(value[i:j])
+        res_w.append(sliced(space, value, i, j, w_self))
 
         # continue to look from the character following the space after the 
word
         i = j + 1
 
-    return space.newlist_str(res)
+    return space.newlist(res_w)
 
 def str_split__String_ANY_ANY(space, w_self, w_by, w_maxsplit=-1):
     maxsplit = space.int_w(w_maxsplit)
@@ -275,25 +275,32 @@
         raise OperationError(space.w_ValueError, space.wrap("empty separator"))
 
     if bylen == 1 and maxsplit < 0:
-        res = []
         start = 0
         # fast path: uses str.rfind(character) and str.count(character)
         by = by[0]    # annotator hack: string -> char
         count = value.count(by)
-        res = [None] * (count + 1)
+        res_w = [None] * (count + 1)
         end = len(value)
         while count >= 0:
             assert end >= 0
             prev = value.rfind(by, 0, end)
             start = prev + 1
             assert start >= 0
-            res[count] = value[start:end]
+            res_w[count] = sliced(space, value, start, end, w_self)
             count -= 1
             end = prev
     else:
-        res = split(value, by, maxsplit)
+        res_w = []
+        while maxsplit != 0:
+            next = value.find(by, start)
+            if next < 0:
+                break
+            res_w.append(sliced(space, value, start, next, w_self))
+            start = next + bylen
+            maxsplit -= 1   # NB. if it's already < 0, it stays < 0
+        res_w.append(sliced(space, value, start, len(value), w_self))
 
-    return space.newlist_str(res)
+    return space.newlist(res_w)
 
 def str_rsplit__String_None_ANY(space, w_self, w_none, w_maxsplit=-1):
     maxsplit = space.int_w(w_maxsplit)
@@ -368,11 +375,6 @@
     'str_rsplit__String_ANY_ANY', sliced)
 
 def str_join__String_ANY(space, w_self, w_list):
-    l = space.listview_str(w_list)
-    if l is not None:
-        if len(l) == 1:
-            return space.wrap(l[0])
-        return space.wrap(w_self._value.join(l))
     list_w = space.listview(w_list)
     size = len(list_w)
 
@@ -382,8 +384,7 @@
     if size == 1:
         w_s = list_w[0]
         # only one item,  return it if it's not a subclass of str
-        if (space.is_w(space.type(w_s), space.w_str) or
-            space.is_w(space.type(w_s), space.w_unicode)):
+        if space.is_w(space.type(w_s), space.w_str):
             return w_s
 
     return _str_join_many_items(space, w_self, list_w, size)
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, UnicodeListStrategy, 
RangeListStrategy, make_range_list
 from pypy.objspace.std import listobject
 from pypy.objspace.std.test.test_listobject import TestW_ListObject
 
@@ -10,7 +10,7 @@
         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)
+        assert isinstance(W_ListObject(self.space, [self.space.wrap('a'), 
self.space.wrap('b')]).strategy, UnicodeListStrategy)
 
     def test_empty_to_any(self):
         l = W_ListObject(self.space, [])
@@ -26,7 +26,7 @@
         l = W_ListObject(self.space, [])
         assert isinstance(l.strategy, EmptyListStrategy)
         l.append(self.space.wrap('a'))
-        assert isinstance(l.strategy, StringListStrategy)
+        assert isinstance(l.strategy, UnicodeListStrategy)
 
     def test_int_to_any(self):
         l = W_ListObject(self.space, 
[self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
@@ -38,9 +38,9 @@
 
     def test_string_to_any(self):
         l = W_ListObject(self.space, 
[self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
-        assert isinstance(l.strategy, StringListStrategy)
+        assert isinstance(l.strategy, UnicodeListStrategy)
         l.append(self.space.wrap('d'))
-        assert isinstance(l.strategy, StringListStrategy)
+        assert isinstance(l.strategy, UnicodeListStrategy)
         l.append(self.space.wrap(3))
         assert isinstance(l.strategy, ObjectListStrategy)
 
@@ -51,7 +51,7 @@
         l.setitem(0, self.space.wrap('d'))
         assert self.space.eq_w(l.getitem(0), self.space.wrap('d'))
 
-        assert isinstance(l.strategy, StringListStrategy)
+        assert isinstance(l.strategy, UnicodeListStrategy)
 
         # IntStrategy to ObjectStrategy
         l = W_ListObject(self.space, 
[self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
@@ -59,9 +59,9 @@
         l.setitem(0, self.space.wrap('d'))
         assert isinstance(l.strategy, ObjectListStrategy)
 
-        # StringStrategy to ObjectStrategy
+        # UnicodeStrategy to ObjectStrategy
         l = W_ListObject(self.space, 
[self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
-        assert isinstance(l.strategy, StringListStrategy)
+        assert isinstance(l.strategy, UnicodeListStrategy)
         l.setitem(0, self.space.wrap(2))
         assert isinstance(l.strategy, ObjectListStrategy)
 
@@ -72,9 +72,9 @@
         l.insert(3, self.space.wrap(4))
         assert isinstance(l.strategy, IntegerListStrategy)
 
-        # StringStrategy
+        # UnicodeStrategy
         l = W_ListObject(self.space, 
[self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
-        assert isinstance(l.strategy, StringListStrategy)
+        assert isinstance(l.strategy, UnicodeListStrategy)
         l.insert(3, self.space.wrap(2))
         assert isinstance(l.strategy, ObjectListStrategy)
 
@@ -88,7 +88,7 @@
         l = W_ListObject(self.space, [])
         assert isinstance(l.strategy, EmptyListStrategy)
         l.insert(0, self.space.wrap('a'))
-        assert isinstance(l.strategy, StringListStrategy)
+        assert isinstance(l.strategy, UnicodeListStrategy)
 
         l = W_ListObject(self.space, [])
         assert isinstance(l.strategy, EmptyListStrategy)
@@ -158,7 +158,7 @@
         l = W_ListObject(self.space, wrapitems(["a","b","c","d","e"]))
         other = W_ListObject(self.space, wrapitems(["a", "b", "c"]))
         keep_other_strategy(l, 0, 2, other.length(), other)
-        assert l.strategy is self.space.fromcache(StringListStrategy)
+        assert l.strategy is self.space.fromcache(UnicodeListStrategy)
 
         l = W_ListObject(self.space, wrapitems(["a",3,"c",4,"e"]))
         other = W_ListObject(self.space, wrapitems(["a", "b", "c"]))
@@ -203,7 +203,7 @@
         empty = W_ListObject(self.space, [])
         assert isinstance(empty.strategy, EmptyListStrategy)
         empty.extend(W_ListObject(self.space, [self.space.wrap("a"), 
self.space.wrap("b"), self.space.wrap("c")]))
-        assert isinstance(empty.strategy, StringListStrategy)
+        assert isinstance(empty.strategy, UnicodeListStrategy)
 
         empty = W_ListObject(self.space, [])
         assert isinstance(empty.strategy, EmptyListStrategy)
@@ -341,9 +341,11 @@
 
     def test_weird_rangelist_bug(self):
         l = make_range_list(self.space, 1, 1, 3)
-        from pypy.objspace.std.listobject import getslice__List_ANY_ANY
+        from pypy.objspace.std.listobject import getitem__List_Slice
+        w_slice = self.space.newslice(
+            self.space.wrap(15), self.space.wrap(2222), self.space.w_None)
         # should not raise
-        assert getslice__List_ANY_ANY(self.space, l, self.space.wrap(15), 
self.space.wrap(2222)).strategy == self.space.fromcache(EmptyListStrategy)
+        assert getitem__List_Slice(self.space, l, w_slice).strategy == 
self.space.fromcache(EmptyListStrategy)
 
 
     def test_add_to_rangelist(self):
@@ -353,12 +355,12 @@
         l3 = add__List_List(self.space, l1, l2)
         assert self.space.eq_w(l3, W_ListObject(self.space, 
[self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), 
self.space.wrap(4), self.space.wrap(5)]))
 
-    def test_unicode(self):
+    def test_bytes(self):
         l1 = W_ListObject(self.space, [self.space.wrap("eins"), 
self.space.wrap("zwei")])
-        assert isinstance(l1.strategy, StringListStrategy)
-        l2 = W_ListObject(self.space, [self.space.wrap(u"eins"), 
self.space.wrap(u"zwei")])
+        assert isinstance(l1.strategy, UnicodeListStrategy)
+        l2 = W_ListObject(self.space, [self.space.wrapbytes("eins"), 
self.space.wrapbytes("zwei")])
         assert isinstance(l2.strategy, ObjectListStrategy)
-        l3 = W_ListObject(self.space, [self.space.wrap("eins"), 
self.space.wrap(u"zwei")])
+        l3 = W_ListObject(self.space, [self.space.wrap("eins"), 
self.space.wrapbytes("zwei")])
         assert isinstance(l3.strategy, ObjectListStrategy)
 
     def test_listview_str(self):
@@ -384,7 +386,7 @@
         space = self.space
         l = ['a', 'b']
         w_l = self.space.newlist_str(l)
-        assert isinstance(w_l.strategy, StringListStrategy)
+        assert isinstance(w_l.strategy, UnicodeListStrategy)
         assert space.listview_str(w_l) is l
 
     def test_string_uses_newlist_str(self):
diff --git a/pypy/objspace/std/unicodeobject.py 
b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -123,6 +123,12 @@
     return space.newbool(container.find(item) != -1)
 
 def unicode_join__Unicode_ANY(space, w_self, w_list):
+    l = space.listview_str(w_list)
+    if l is not None:
+        if len(l) == 1:
+            return space.wrap(l[0])
+        return space.wrap(w_self._value.join(l))
+
     list_w = space.unpackiterable(w_list)
     size = len(list_w)
 
@@ -555,7 +561,7 @@
 
 def unicode_split__Unicode_None_ANY(space, w_self, w_none, w_maxsplit):
     maxsplit = space.int_w(w_maxsplit)
-    res_w = []
+    res = []
     value = w_self._value
     length = len(value)
     i = 0
@@ -578,12 +584,12 @@
             maxsplit -= 1   # NB. if it's already < 0, it stays < 0
 
         # the word is value[i:j]
-        res_w.append(W_UnicodeObject(value[i:j]))
+        res.append(value[i:j])
 
         # continue to look from the character following the space after the 
word
         i = j + 1
 
-    return space.newlist(res_w)
+    return space.newlist_str(res)
 
 def unicode_split__Unicode_Unicode_ANY(space, w_self, w_delim, w_maxsplit):
     self = w_self._value
@@ -594,7 +600,7 @@
         raise OperationError(space.w_ValueError,
                              space.wrap('empty separator'))
     parts = _split_with(self, delim, maxsplit)
-    return space.newlist([W_UnicodeObject(part) for part in parts])
+    return space.newlist_str(parts)
 
 
 def unicode_rsplit__Unicode_None_ANY(space, w_self, w_none, w_maxsplit):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to