Author: Carl Friedrich Bolz <cfb...@gmx.de> Branch: Changeset: r64900:de52390077e2 Date: 2013-06-13 18:20 +0200 http://bitbucket.org/pypy/pypy/changeset/de52390077e2/
Log: introduce newlist_unicode for wrapping lists of unicode diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py --- a/pypy/interpreter/baseobjspace.py +++ b/pypy/interpreter/baseobjspace.py @@ -904,6 +904,9 @@ def newlist_str(self, list_s): return self.newlist([self.wrap(s) for s in list_s]) + def newlist_unicode(self, list_u): + return self.newlist([self.wrap(u) for u in list_u]) + def newlist_hint(self, sizehint): from pypy.objspace.std.listobject import make_empty_list_with_size return make_empty_list_with_size(self, sizehint) 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 @@ -166,6 +166,12 @@ storage = strategy.erase(list_s) return W_ListObject.from_storage_and_strategy(space, storage, strategy) + @staticmethod + def newlist_unicode(space, list_u): + strategy = space.fromcache(UnicodeListStrategy) + storage = strategy.erase(list_u) + return W_ListObject.from_storage_and_strategy(space, storage, strategy) + def __repr__(self): """ representation for debugging purposes """ return "%s(%s, %s)" % (self.__class__.__name__, self.strategy, diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py --- a/pypy/objspace/std/objspace.py +++ b/pypy/objspace/std/objspace.py @@ -294,6 +294,9 @@ def newlist_str(self, list_s): return W_ListObject.newlist_str(self, list_s) + def newlist_unicode(self, list_u): + return W_ListObject.newlist_unicode(self, list_u) + def newdict(self, module=False, instance=False, kwargs=False, strdict=False): return W_DictMultiObject.allocate_and_init_instance( 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 @@ -560,6 +560,18 @@ assert space.listview_str(w_l) == ["a", "b", "c"] assert space.listview_str(w_l2) == ["a", "b", "c"] + def test_unicode_uses_newlist_unicode(self): + space = self.space + w_u = space.wrap(u"a b c") + space.newlist = None + try: + w_l = space.call_method(w_u, "split") + w_l2 = space.call_method(w_u, "split", space.wrap(" ")) + finally: + del space.newlist + assert space.listview_unicode(w_l) == [u"a", u"b", u"c"] + assert space.listview_unicode(w_l2) == [u"a", u"b", u"c"] + def test_pop_without_argument_is_fast(self): space = self.space w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)]) 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 @@ -650,7 +650,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 @@ -673,12 +673,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_unicode(res) def unicode_split__Unicode_Unicode_ANY(space, w_self, w_delim, w_maxsplit): self = w_self._value @@ -689,12 +689,12 @@ 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_unicode(parts) def unicode_rsplit__Unicode_None_ANY(space, w_self, w_none, w_maxsplit): maxsplit = space.int_w(w_maxsplit) - res_w = [] + res = [] value = w_self._value i = len(value)-1 while True: @@ -719,13 +719,13 @@ # the word is value[j+1:i+1] j1 = j + 1 assert j1 >= 0 - res_w.append(W_UnicodeObject(value[j1:i+1])) + res.append(value[j1:i+1]) # continue to look from the character before the space before the word i = j - 1 - res_w.reverse() - return space.newlist(res_w) + res.reverse() + return space.newlist_unicode(res) def sliced(space, s, start, stop, orig_obj): assert start >= 0 _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit